Submission #679151

# Submission time Handle Problem Language Result Execution time Memory
679151 2023-01-07T15:07:56 Z socpite Fireworks (APIO16_fireworks) C++17
100 / 100
236 ms 59184 KB
#include<bits/stdc++.h>
using namespace std;

#define f first
#define s second

typedef long long ll;

const int maxn = 3e5+5;
const int mod = 998244353;
const ll inf = 1e18;

int n, m;

ll W[maxn];

int hh[maxn], sz[maxn];

priority_queue<ll> pq[maxn];

vector<int> graph[maxn];

pair<ll, ll> func[maxn];

void ph(int x){
    func[x].s+=pq[x].top();
    pq[x].pop();
    func[x].f--;
}

void add(int x, ll val){
    func[x].s -= val;
    pq[x].push(val);
    func[x].f++;
}

void Union(int x, int d){
    func[x].f += func[d].f;
    func[x].s += func[d].s;
    while(!pq[d].empty()){
        pq[x].push(pq[d].top());
        pq[d].pop();
    }
}

void dfs(int x){
    sz[x]++;
    if(x > n){
        func[x] = {1, -W[x]};
        hh[x]=x;
        pq[x].push(W[x]);
        pq[x].push(W[x]);
    }
    else{
        int bc = 0;
        for(auto v: graph[x]){
            dfs(v);
            sz[x]+=sz[v];
            if(sz[bc] < sz[v])bc = v;
        }
        int id = hh[bc];
        hh[x] = id;
        for(auto v: graph[x]){
            if(v == bc)continue;
            Union(id, hh[v]);
        }
        func[id].s+=W[x];
        while(func[id].f > 1)ph(id);
        ll p1 = pq[id].top();
        ph(id);
        ll p2 = pq[id].top();
        ph(id);
        add(id, p1+W[x]);
        add(id, p2+W[x]);
    }
    //cout << x << " " << func[hh[x]].f << " " << func[hh[x]].s << "\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 2; i <= n+m; i++){
        int x;
        cin >> x >> W[i];
        graph[x].push_back(i);
    }
    dfs(1);
    ph(hh[1]);
    cout << func[hh[1]].s;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16748 KB Output is correct
2 Correct 8 ms 16752 KB Output is correct
3 Correct 8 ms 16780 KB Output is correct
4 Correct 8 ms 16776 KB Output is correct
5 Correct 8 ms 16724 KB Output is correct
6 Correct 8 ms 16772 KB Output is correct
7 Correct 8 ms 16724 KB Output is correct
8 Correct 9 ms 16776 KB Output is correct
9 Correct 9 ms 16724 KB Output is correct
10 Correct 9 ms 16716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16724 KB Output is correct
2 Correct 10 ms 16724 KB Output is correct
3 Correct 9 ms 16724 KB Output is correct
4 Correct 14 ms 16768 KB Output is correct
5 Correct 9 ms 16788 KB Output is correct
6 Correct 14 ms 16776 KB Output is correct
7 Correct 9 ms 16760 KB Output is correct
8 Correct 8 ms 16772 KB Output is correct
9 Correct 10 ms 16724 KB Output is correct
10 Correct 9 ms 16772 KB Output is correct
11 Correct 9 ms 16724 KB Output is correct
12 Correct 9 ms 16768 KB Output is correct
13 Correct 9 ms 16724 KB Output is correct
14 Correct 9 ms 16776 KB Output is correct
15 Correct 9 ms 16724 KB Output is correct
16 Correct 9 ms 16724 KB Output is correct
17 Correct 9 ms 16724 KB Output is correct
18 Correct 9 ms 16696 KB Output is correct
19 Correct 9 ms 16724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16748 KB Output is correct
2 Correct 8 ms 16752 KB Output is correct
3 Correct 8 ms 16780 KB Output is correct
4 Correct 8 ms 16776 KB Output is correct
5 Correct 8 ms 16724 KB Output is correct
6 Correct 8 ms 16772 KB Output is correct
7 Correct 8 ms 16724 KB Output is correct
8 Correct 9 ms 16776 KB Output is correct
9 Correct 9 ms 16724 KB Output is correct
10 Correct 9 ms 16716 KB Output is correct
11 Correct 9 ms 16724 KB Output is correct
12 Correct 10 ms 16724 KB Output is correct
13 Correct 9 ms 16724 KB Output is correct
14 Correct 14 ms 16768 KB Output is correct
15 Correct 9 ms 16788 KB Output is correct
16 Correct 14 ms 16776 KB Output is correct
17 Correct 9 ms 16760 KB Output is correct
18 Correct 8 ms 16772 KB Output is correct
19 Correct 10 ms 16724 KB Output is correct
20 Correct 9 ms 16772 KB Output is correct
21 Correct 9 ms 16724 KB Output is correct
22 Correct 9 ms 16768 KB Output is correct
23 Correct 9 ms 16724 KB Output is correct
24 Correct 9 ms 16776 KB Output is correct
25 Correct 9 ms 16724 KB Output is correct
26 Correct 9 ms 16724 KB Output is correct
27 Correct 9 ms 16724 KB Output is correct
28 Correct 9 ms 16696 KB Output is correct
29 Correct 9 ms 16724 KB Output is correct
30 Correct 13 ms 16724 KB Output is correct
31 Correct 9 ms 16852 KB Output is correct
32 Correct 10 ms 16892 KB Output is correct
33 Correct 10 ms 16980 KB Output is correct
34 Correct 10 ms 16920 KB Output is correct
35 Correct 10 ms 17040 KB Output is correct
36 Correct 10 ms 17116 KB Output is correct
37 Correct 11 ms 17052 KB Output is correct
38 Correct 11 ms 17108 KB Output is correct
39 Correct 11 ms 17168 KB Output is correct
40 Correct 16 ms 17492 KB Output is correct
41 Correct 15 ms 17480 KB Output is correct
42 Correct 12 ms 16980 KB Output is correct
43 Correct 11 ms 17236 KB Output is correct
44 Correct 11 ms 17236 KB Output is correct
45 Correct 10 ms 17236 KB Output is correct
46 Correct 11 ms 17300 KB Output is correct
47 Correct 11 ms 17288 KB Output is correct
48 Correct 10 ms 17212 KB Output is correct
49 Correct 11 ms 17236 KB Output is correct
50 Correct 10 ms 17108 KB Output is correct
51 Correct 10 ms 17108 KB Output is correct
52 Correct 16 ms 17236 KB Output is correct
53 Correct 17 ms 17288 KB Output is correct
54 Correct 11 ms 17364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16748 KB Output is correct
2 Correct 8 ms 16752 KB Output is correct
3 Correct 8 ms 16780 KB Output is correct
4 Correct 8 ms 16776 KB Output is correct
5 Correct 8 ms 16724 KB Output is correct
6 Correct 8 ms 16772 KB Output is correct
7 Correct 8 ms 16724 KB Output is correct
8 Correct 9 ms 16776 KB Output is correct
9 Correct 9 ms 16724 KB Output is correct
10 Correct 9 ms 16716 KB Output is correct
11 Correct 9 ms 16724 KB Output is correct
12 Correct 10 ms 16724 KB Output is correct
13 Correct 9 ms 16724 KB Output is correct
14 Correct 14 ms 16768 KB Output is correct
15 Correct 9 ms 16788 KB Output is correct
16 Correct 14 ms 16776 KB Output is correct
17 Correct 9 ms 16760 KB Output is correct
18 Correct 8 ms 16772 KB Output is correct
19 Correct 10 ms 16724 KB Output is correct
20 Correct 9 ms 16772 KB Output is correct
21 Correct 9 ms 16724 KB Output is correct
22 Correct 9 ms 16768 KB Output is correct
23 Correct 9 ms 16724 KB Output is correct
24 Correct 9 ms 16776 KB Output is correct
25 Correct 9 ms 16724 KB Output is correct
26 Correct 9 ms 16724 KB Output is correct
27 Correct 9 ms 16724 KB Output is correct
28 Correct 9 ms 16696 KB Output is correct
29 Correct 9 ms 16724 KB Output is correct
30 Correct 13 ms 16724 KB Output is correct
31 Correct 9 ms 16852 KB Output is correct
32 Correct 10 ms 16892 KB Output is correct
33 Correct 10 ms 16980 KB Output is correct
34 Correct 10 ms 16920 KB Output is correct
35 Correct 10 ms 17040 KB Output is correct
36 Correct 10 ms 17116 KB Output is correct
37 Correct 11 ms 17052 KB Output is correct
38 Correct 11 ms 17108 KB Output is correct
39 Correct 11 ms 17168 KB Output is correct
40 Correct 16 ms 17492 KB Output is correct
41 Correct 15 ms 17480 KB Output is correct
42 Correct 12 ms 16980 KB Output is correct
43 Correct 11 ms 17236 KB Output is correct
44 Correct 11 ms 17236 KB Output is correct
45 Correct 10 ms 17236 KB Output is correct
46 Correct 11 ms 17300 KB Output is correct
47 Correct 11 ms 17288 KB Output is correct
48 Correct 10 ms 17212 KB Output is correct
49 Correct 11 ms 17236 KB Output is correct
50 Correct 10 ms 17108 KB Output is correct
51 Correct 10 ms 17108 KB Output is correct
52 Correct 16 ms 17236 KB Output is correct
53 Correct 17 ms 17288 KB Output is correct
54 Correct 11 ms 17364 KB Output is correct
55 Correct 14 ms 17816 KB Output is correct
56 Correct 36 ms 20892 KB Output is correct
57 Correct 57 ms 23756 KB Output is correct
58 Correct 70 ms 25644 KB Output is correct
59 Correct 113 ms 28668 KB Output is correct
60 Correct 138 ms 31684 KB Output is correct
61 Correct 147 ms 34064 KB Output is correct
62 Correct 169 ms 35780 KB Output is correct
63 Correct 236 ms 39340 KB Output is correct
64 Correct 208 ms 40332 KB Output is correct
65 Correct 99 ms 59148 KB Output is correct
66 Correct 114 ms 59184 KB Output is correct
67 Correct 136 ms 35732 KB Output is correct
68 Correct 138 ms 50736 KB Output is correct
69 Correct 168 ms 48916 KB Output is correct
70 Correct 195 ms 48980 KB Output is correct
71 Correct 177 ms 54304 KB Output is correct
72 Correct 170 ms 54360 KB Output is correct
73 Correct 153 ms 51424 KB Output is correct
74 Correct 148 ms 51296 KB Output is correct
75 Correct 154 ms 50240 KB Output is correct
76 Correct 145 ms 50200 KB Output is correct
77 Correct 163 ms 48840 KB Output is correct
78 Correct 156 ms 46464 KB Output is correct
79 Correct 160 ms 48552 KB Output is correct