Submission #542572

# Submission time Handle Problem Language Result Execution time Memory
542572 2022-03-26T23:46:24 Z czhang2718 Fireworks (APIO16_fireworks) C++17
100 / 100
406 ms 129708 KB
#include "bits/stdc++.h"
using namespace std;

typedef vector<int> vi;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
#define pb push_back
#define f first
#define s second
#define sz(x) (int) x.size()
#define rep(i,a,b) for(int i=a; i<=b; i++)

const int N=600001;
int n;
vector<pii> adj[N]; 
multiset<int> s[N];
pair<ll, ll> mb[N]; //mx+b
int par[N];

int find(int x){ return (par[x]==x?x:par[x]=find(par[x])); }

void merge(int x, int y){
    int a=find(x), b=find(y);
    if(sz(s[a])<sz(s[b])) swap(a,b);
    for(int x:s[b]){
        s[a].insert(x);
    }
    mb[a].f+=mb[b].f;
    mb[a].s+=mb[b].s;
    par[b]=a;
}

void dfs(int x, int c){
    if(!sz(adj[x])){
        mb[x]={1, -c};
        s[x]={c, c};
        return;
    }
    for(auto e:adj[x]){
        dfs(e.f, e.s);
        merge(e.f, x);
    }
    x=find(x);
    while(mb[x].f>1){
        int x2=*prev(s[x].end());
        // int x1=*prev(prev(s[x].end()));
        mb[x]={mb[x].f-1, mb[x].s+(ll)x2};
        s[x].erase(--s[x].end());
    }
    int x2=*prev(s[x].end());
    int x1=*prev(prev(s[x].end()));
    s[x].erase(s[x].find(x1)); s[x].erase(s[x].find(x2));
    s[x].insert(x2+c); s[x].insert(x1+c);
    mb[x].s-=ll(c)*mb[x].f;
}

signed main(){
    cin.tie(0)->sync_with_stdio(0);

    int a,b; cin >> a >> b;
    n=a+b;
    rep(i,1,n) par[i]=i;
    rep(i,2,n){
        int par, c; cin >> par >> c;
        adj[par].pb({i, c});
    }
    dfs(1, 0);
    int y=find(1);
    int x=*prev(s[y].end());
    cout << mb[y].f*x+mb[y].s;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 42580 KB Output is correct
2 Correct 21 ms 42512 KB Output is correct
3 Correct 22 ms 42604 KB Output is correct
4 Correct 24 ms 42580 KB Output is correct
5 Correct 23 ms 42588 KB Output is correct
6 Correct 25 ms 42520 KB Output is correct
7 Correct 23 ms 42544 KB Output is correct
8 Correct 22 ms 42580 KB Output is correct
9 Correct 22 ms 42616 KB Output is correct
10 Correct 25 ms 42580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 42580 KB Output is correct
2 Correct 25 ms 42560 KB Output is correct
3 Correct 22 ms 42524 KB Output is correct
4 Correct 24 ms 42544 KB Output is correct
5 Correct 21 ms 42604 KB Output is correct
6 Correct 21 ms 42616 KB Output is correct
7 Correct 20 ms 42580 KB Output is correct
8 Correct 20 ms 42524 KB Output is correct
9 Correct 21 ms 42580 KB Output is correct
10 Correct 21 ms 42608 KB Output is correct
11 Correct 20 ms 42600 KB Output is correct
12 Correct 23 ms 42580 KB Output is correct
13 Correct 19 ms 42532 KB Output is correct
14 Correct 20 ms 42580 KB Output is correct
15 Correct 20 ms 42600 KB Output is correct
16 Correct 21 ms 42660 KB Output is correct
17 Correct 22 ms 42584 KB Output is correct
18 Correct 21 ms 42580 KB Output is correct
19 Correct 21 ms 42600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 42580 KB Output is correct
2 Correct 21 ms 42512 KB Output is correct
3 Correct 22 ms 42604 KB Output is correct
4 Correct 24 ms 42580 KB Output is correct
5 Correct 23 ms 42588 KB Output is correct
6 Correct 25 ms 42520 KB Output is correct
7 Correct 23 ms 42544 KB Output is correct
8 Correct 22 ms 42580 KB Output is correct
9 Correct 22 ms 42616 KB Output is correct
10 Correct 25 ms 42580 KB Output is correct
11 Correct 22 ms 42580 KB Output is correct
12 Correct 25 ms 42560 KB Output is correct
13 Correct 22 ms 42524 KB Output is correct
14 Correct 24 ms 42544 KB Output is correct
15 Correct 21 ms 42604 KB Output is correct
16 Correct 21 ms 42616 KB Output is correct
17 Correct 20 ms 42580 KB Output is correct
18 Correct 20 ms 42524 KB Output is correct
19 Correct 21 ms 42580 KB Output is correct
20 Correct 21 ms 42608 KB Output is correct
21 Correct 20 ms 42600 KB Output is correct
22 Correct 23 ms 42580 KB Output is correct
23 Correct 19 ms 42532 KB Output is correct
24 Correct 20 ms 42580 KB Output is correct
25 Correct 20 ms 42600 KB Output is correct
26 Correct 21 ms 42660 KB Output is correct
27 Correct 22 ms 42584 KB Output is correct
28 Correct 21 ms 42580 KB Output is correct
29 Correct 21 ms 42600 KB Output is correct
30 Correct 22 ms 42556 KB Output is correct
31 Correct 22 ms 42748 KB Output is correct
32 Correct 23 ms 42732 KB Output is correct
33 Correct 23 ms 42868 KB Output is correct
34 Correct 25 ms 42956 KB Output is correct
35 Correct 24 ms 42996 KB Output is correct
36 Correct 25 ms 43084 KB Output is correct
37 Correct 27 ms 43240 KB Output is correct
38 Correct 28 ms 43220 KB Output is correct
39 Correct 24 ms 43252 KB Output is correct
40 Correct 24 ms 43484 KB Output is correct
41 Correct 24 ms 43396 KB Output is correct
42 Correct 23 ms 42768 KB Output is correct
43 Correct 24 ms 43492 KB Output is correct
44 Correct 25 ms 43452 KB Output is correct
45 Correct 25 ms 43436 KB Output is correct
46 Correct 26 ms 43764 KB Output is correct
47 Correct 28 ms 43720 KB Output is correct
48 Correct 26 ms 43632 KB Output is correct
49 Correct 26 ms 43768 KB Output is correct
50 Correct 25 ms 43452 KB Output is correct
51 Correct 26 ms 43496 KB Output is correct
52 Correct 27 ms 43752 KB Output is correct
53 Correct 25 ms 43536 KB Output is correct
54 Correct 25 ms 43788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 42580 KB Output is correct
2 Correct 21 ms 42512 KB Output is correct
3 Correct 22 ms 42604 KB Output is correct
4 Correct 24 ms 42580 KB Output is correct
5 Correct 23 ms 42588 KB Output is correct
6 Correct 25 ms 42520 KB Output is correct
7 Correct 23 ms 42544 KB Output is correct
8 Correct 22 ms 42580 KB Output is correct
9 Correct 22 ms 42616 KB Output is correct
10 Correct 25 ms 42580 KB Output is correct
11 Correct 22 ms 42580 KB Output is correct
12 Correct 25 ms 42560 KB Output is correct
13 Correct 22 ms 42524 KB Output is correct
14 Correct 24 ms 42544 KB Output is correct
15 Correct 21 ms 42604 KB Output is correct
16 Correct 21 ms 42616 KB Output is correct
17 Correct 20 ms 42580 KB Output is correct
18 Correct 20 ms 42524 KB Output is correct
19 Correct 21 ms 42580 KB Output is correct
20 Correct 21 ms 42608 KB Output is correct
21 Correct 20 ms 42600 KB Output is correct
22 Correct 23 ms 42580 KB Output is correct
23 Correct 19 ms 42532 KB Output is correct
24 Correct 20 ms 42580 KB Output is correct
25 Correct 20 ms 42600 KB Output is correct
26 Correct 21 ms 42660 KB Output is correct
27 Correct 22 ms 42584 KB Output is correct
28 Correct 21 ms 42580 KB Output is correct
29 Correct 21 ms 42600 KB Output is correct
30 Correct 22 ms 42556 KB Output is correct
31 Correct 22 ms 42748 KB Output is correct
32 Correct 23 ms 42732 KB Output is correct
33 Correct 23 ms 42868 KB Output is correct
34 Correct 25 ms 42956 KB Output is correct
35 Correct 24 ms 42996 KB Output is correct
36 Correct 25 ms 43084 KB Output is correct
37 Correct 27 ms 43240 KB Output is correct
38 Correct 28 ms 43220 KB Output is correct
39 Correct 24 ms 43252 KB Output is correct
40 Correct 24 ms 43484 KB Output is correct
41 Correct 24 ms 43396 KB Output is correct
42 Correct 23 ms 42768 KB Output is correct
43 Correct 24 ms 43492 KB Output is correct
44 Correct 25 ms 43452 KB Output is correct
45 Correct 25 ms 43436 KB Output is correct
46 Correct 26 ms 43764 KB Output is correct
47 Correct 28 ms 43720 KB Output is correct
48 Correct 26 ms 43632 KB Output is correct
49 Correct 26 ms 43768 KB Output is correct
50 Correct 25 ms 43452 KB Output is correct
51 Correct 26 ms 43496 KB Output is correct
52 Correct 27 ms 43752 KB Output is correct
53 Correct 25 ms 43536 KB Output is correct
54 Correct 25 ms 43788 KB Output is correct
55 Correct 31 ms 44472 KB Output is correct
56 Correct 59 ms 50048 KB Output is correct
57 Correct 99 ms 55708 KB Output is correct
58 Correct 123 ms 59504 KB Output is correct
59 Correct 159 ms 65064 KB Output is correct
60 Correct 194 ms 70860 KB Output is correct
61 Correct 230 ms 74756 KB Output is correct
62 Correct 252 ms 78596 KB Output is correct
63 Correct 298 ms 86476 KB Output is correct
64 Correct 320 ms 87216 KB Output is correct
65 Correct 135 ms 96872 KB Output is correct
66 Correct 137 ms 96752 KB Output is correct
67 Correct 156 ms 59448 KB Output is correct
68 Correct 234 ms 98916 KB Output is correct
69 Correct 279 ms 97008 KB Output is correct
70 Correct 289 ms 96888 KB Output is correct
71 Correct 343 ms 129708 KB Output is correct
72 Correct 359 ms 129260 KB Output is correct
73 Correct 362 ms 115712 KB Output is correct
74 Correct 398 ms 115920 KB Output is correct
75 Correct 386 ms 114708 KB Output is correct
76 Correct 370 ms 114832 KB Output is correct
77 Correct 387 ms 112532 KB Output is correct
78 Correct 406 ms 109600 KB Output is correct
79 Correct 376 ms 114180 KB Output is correct