Submission #265503

# Submission time Handle Problem Language Result Execution time Memory
265503 2020-08-14T22:40:01 Z ajpiano Fireworks (APIO16_fireworks) C++14
19 / 100
7 ms 7552 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

using namespace std;

#define FOR(a,b) for(int a=0;a<b;a++)
#define F0R(a,b,c) for(int a = b; a<=c; a++)
#define f first
#define s second
#define m0(x) memset(x,0,sizeof(x))
#define all(x) x.begin(), x.end()

typedef pair<int,int> pi;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pi> vpi;

struct hull{
    priority_queue<int> lhull;
    priority_queue<int,vi,greater<int>> rhull;
    int height = 0;
    void putpar(int len){
        if(lhull.empty()){
            lhull.push(len);
            rhull.push(len);
            return;
        }
        int a = lhull.top(); lhull.pop(); a += len;
        lhull.push(a);
        a = rhull.top();
        while(!rhull.empty()) rhull.pop();
        rhull.push(a+len);
    }
    void plu(int num){
        lhull.push(num);
        int a = lhull.top(); lhull.pop();
        height += a-num;
        rhull.push(a);
    }
    void pld(int num){
        rhull.push(num);
        int a = rhull.top(); rhull.pop();
        height += num - a;
        lhull.push(a);
    }
    void ins(int num){
        plu(num);
        pld(num);
    }
    void combine(hull & other){
        while(!other.lhull.empty()){
            int a = other.lhull.top();
            other.lhull.pop();
            pld(a);
        }
        while(!other.rhull.empty()){
            int a = other.rhull.top();
            other.rhull.pop();
            plu(a);
        }
        height += other.height;
    }
};

vi children, root;

const int large = 3e5+1;

vi edges[large];

int dfs1(int child){
    int ccnt = 1;
    for(auto &a: edges[child]) ccnt += dfs1(a);
    children[child] = ccnt;
    return ccnt;
}

void dfs2(int child, hull &ans){
    const int sz = edges[child].size();
    vi &e = edges[child];
    FOR(i,sz){
        if(children[e[i]] > children[e[0]])
            swap(e[i], e[0]);
    }
    if(sz != 0){
        dfs2(e[0],ans);
    }
    F0R(i,1,sz-1){
        hull cur;
        dfs2(e[i],cur);
        ans.combine(cur);
    }
    ans.putpar(root[child]);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n,m; cin >> n >> m;
    hull ans;
    children.resize(n+m+1);
    root.resize(n+m+1);
    F0R(i,2,n+m){
        int par;
        cin >> par >> root[i];
        edges[par].push_back(i);
    }
    dfs1(1);
    dfs2(1,ans);
    cout << ans.height << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7552 KB Output is correct
2 Correct 6 ms 7424 KB Output is correct
3 Incorrect 5 ms 7440 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 6 ms 7424 KB Output is correct
3 Correct 6 ms 7424 KB Output is correct
4 Correct 6 ms 7424 KB Output is correct
5 Correct 5 ms 7424 KB Output is correct
6 Correct 7 ms 7424 KB Output is correct
7 Correct 5 ms 7424 KB Output is correct
8 Correct 7 ms 7424 KB Output is correct
9 Correct 6 ms 7552 KB Output is correct
10 Correct 5 ms 7424 KB Output is correct
11 Correct 5 ms 7424 KB Output is correct
12 Correct 5 ms 7424 KB Output is correct
13 Correct 6 ms 7424 KB Output is correct
14 Correct 6 ms 7424 KB Output is correct
15 Correct 5 ms 7424 KB Output is correct
16 Correct 6 ms 7424 KB Output is correct
17 Correct 5 ms 7328 KB Output is correct
18 Correct 7 ms 7424 KB Output is correct
19 Correct 6 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7552 KB Output is correct
2 Correct 6 ms 7424 KB Output is correct
3 Incorrect 5 ms 7440 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7552 KB Output is correct
2 Correct 6 ms 7424 KB Output is correct
3 Incorrect 5 ms 7440 KB Output isn't correct
4 Halted 0 ms 0 KB -