Submission #1241903

#TimeUsernameProblemLanguageResultExecution timeMemory
1241903Zero_OPFireworks (APIO16_fireworks)C++17
7 / 100
3 ms7496 KiB
//CAUTION : this template is only suitable with C++17 (above) and home training, do not abuse when practicing offline
#include <bits/stdc++.h>

using namespace std;

//Benq inspires me lol
#define tcT template<class T
#define tcTU tcT, class U

//pairs
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second

//vectors
#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define pb push_back
#define eb emplace_back
#define compact(v) v.erase(unique(all(v)), end(v))

tcT> int lwb(const vector<T>& a, const T& b){ return int(lower_bound(all(a), b) - begin(a)); }
tcT> int upb(const vector<T>& a, const T& b){ return int(upper_bound(all(a), b) - begin(a)); }

//loops (warning : ONLY for int)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)

#ifdef LOCAL
    #include "debug.h"
#else 
    #define debug(...) 42
#endif //LOCAL

//data types
using ll = long long;
using db = double;
using ld = long double;

using pi = pair<int, int>;
using pl = pair<ll, ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using vd = vector<db>;
using vc = vector<char>;
using vstr = vector<string>;

using vpi = vector<pi>;
using vpl = vector<pl>;

tcT> using min_heap = priority_queue<T, vector<T>, greater<T>>;
tcT> using max_heap = priority_queue<T>;

//bitwise operations
#define popcount(x) __builtin_popcountll(x) //count bits
#define BIT(k) (1LL << k) //bit k-th

//functions
tcT> bool minimize(T& a, const T& b){ if(a > b) return a = b, true; return false; } 
tcT> bool maximize(T& a, const T& b){ if(a < b) return a = b, true; return false; } 

tcT> T ceil_div(T a, T b){ return (a / b) + ((a ^ b) > 0 && a % b); }
tcT> T floor_div(T a, T b){ return (a / b) - ((a ^ b) < 0 && a % b); }

tcT> void safe_erase(vector<T>& a, T x){
    auto it = find(all(a), x);
    if(it != a.end()) a.erase(it);
}

#ifdef LOCAL //for checking time elapsed
const auto start_time = std::chrono::high_resolution_clock::now();
db time_elapsed(){ return chrono::duration<db>(std::chrono::high_resolution_clock::now() - 
                                               start_time).count(); }
#endif //LOCAL

void setIO(){
    ios_base::sync_with_stdio(0); cin.tie(0);
#define task "task"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        // freopen(task".out", "w", stdout);
    }
}

const int MAX = 3e5 + 5;

int N, M;
vpi adj[MAX];

ll min_f[MAX];
pl optimum[MAX];

void dfs(int u, int p){
    max_heap<ll> L; min_heap<ll> R;
    for(auto [v, w] : adj[u]) if(v != p){
        if(N + 1 <= v){
            //leaf
            optimum[v] = {0, 0};
            min_f[v] = 0;
        } else{
            dfs(v, u);
        }
        
        min_f[u] += min_f[v];
        L.push(optimum[v].ff + w);
        R.push(optimum[v].ss + w);
    }

    assert(!L.empty() && !R.empty());
    while(L.top() > R.top()){
        ll l = L.top(); L.pop();;
        ll r = R.top(); R.pop();
        min_f[u] += l - r;
        L.push(r);
        R.push(l);
    }

    optimum[u] = {L.top(), R.top()};
}

void testcase(int n_case){
    cin >> N >> M;
    for(int i = 2; i <= N + M; ++i){
        int p, w;
        cin >> p >> w;
        adj[p].eb(i, w);
    }

    dfs(1, -1);
    cout << min_f[1] << '\n';
}

int main(){
    setIO();
    int T = 1; 
    // cin >> T;
    rep(i, 0, T) testcase(i);
#ifdef LOCAL
    cerr << '\n' << "Execution time : " << (time_elapsed() * 1000.0) << " ms";
#endif //LOCAL
    return 0;
}

Compilation message (stderr)

fireworks.cpp: In function 'void setIO()':
fireworks.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...