QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#674602 | #3. Fireworks | SimonLJK | 26 | 8ms | 33228kb | C++17 | 1.6kb | 2024-10-25 16:50:38 | 2024-10-25 16:50:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+999;
int n,m;
vector<pair<int,int> > to[N];
ll val[N],dep[N];
int rt[N],son[N][2],cntnode;
ll k[N],b[N];
int merge(int x,int y){
if(!x||!y) return x+y;
if(val[x]<val[y]) swap(x,y);
son[x][1]=merge(son[x][1],y);
if(dep[son[x][1]]>dep[son[x][0]]) swap(son[x][0],son[x][1]);
dep[x]=dep[son[x][1]]+1;
return x;
}
void search(int u){
if(!u) return;
cout<<val[u]<<" ";
search(son[u][0]);
search(son[u][1]);
return;
}
void dfs(int u){
int v,w,up;
for(pair<int,int> now:to[u]){
v=now.first; w=now.second;
dfs(v); b[v]+=w;
if(to[v].empty()){
k[v]--;
val[++cntnode]=w;
rt[v]=merge(cntnode,rt[v]);
val[++cntnode]=w;
rt[v]=merge(cntnode,rt[v]);
}
else{
up=val[rt[v]]; up+=w;
rt[v]=merge(son[rt[v]][0],son[rt[v]][1]);
w=val[rt[v]]+w;
rt[v]=merge(son[rt[v]][0],son[rt[v]][1]);
val[++cntnode]=w;
rt[v]=merge(cntnode,rt[v]);
val[++cntnode]=up;
rt[v]=merge(cntnode,rt[v]);
}
rt[u]=merge(rt[u],rt[v]);
k[u]+=k[v]; b[u]+=b[v];
}
for(int i=1;i<to[u].size();i++)
rt[u]=merge(son[rt[u]][0],son[rt[u]][1]);
return;
}
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m; n+=m;
int u,w;
for(int i=2;i<=n;i++){
cin>>u>>w;
to[u].push_back(make_pair(i,w));
}
dfs(1); u=1;
ll ans=b[1];
rt[u]=merge(son[rt[u]][0],son[rt[u]][1]);
ll ls=val[rt[u]],nk=1;
rt[u]=merge(son[rt[u]][0],son[rt[u]][1]);
while(rt[u]){
ans-=(ls-val[rt[u]])*nk; nk++; ls=val[rt[u]];
rt[u]=merge(son[rt[u]][0],son[rt[u]][1]);
}
ans-=ls*nk;
cout<<ans;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 7
Accepted
Test #1:
score: 7
Accepted
time: 8ms
memory: 31852kb
input:
1 1 1 594911537
output:
0
result:
ok single line: '0'
Test #2:
score: 7
Accepted
time: 0ms
memory: 32556kb
input:
1 9 1 825879648 1 544663269 1 523587648 1 265820061 1 715816315 1 46975056 1 181647299 1 679235405 1 657226464
output:
1860127768
result:
ok single line: '1860127768'
Test #3:
score: 7
Accepted
time: 0ms
memory: 32972kb
input:
1 91 1 385900550 1 26102977 1 667546315 1 172015997 1 279290345 1 307280944 1 617208234 1 356267123 1 736729319 1 625308102 1 760176838 1 58322561 1 791921572 1 668621731 1 363109524 1 158699815 1 131392063 1 584992804 1 691545613 1 974934420 1 997953539 1 876137720 1 564678478 1 746582187 1 8674083...
output:
22110262696
result:
ok single line: '22110262696'
Test #4:
score: 7
Accepted
time: 2ms
memory: 31724kb
input:
1 89 1 82978004 1 725546925 1 824854855 1 134535238 1 182588700 1 121005200 1 274161085 1 287432242 1 765301085 1 762002231 1 14627523 1 160221425 1 835613649 1 891489782 1 963032877 1 232634649 1 551893622 1 78012995 1 298144433 1 402762010 1 455684005 1 921659880 1 244327108 1 567279640 1 40490786...
output:
22880340918
result:
ok single line: '22880340918'
Test #5:
score: 7
Accepted
time: 0ms
memory: 33112kb
input:
1 84 1 887742276 1 580594619 1 950555150 1 771426100 1 465681669 1 469566475 1 651054119 1 538349774 1 623278516 1 795673458 1 647508726 1 643240398 1 182322569 1 832568327 1 160387299 1 114691531 1 329968342 1 620479904 1 652768604 1 98506590 1 198564634 1 538477498 1 754322865 1 94590693 1 9102737...
output:
17356166508
result:
ok single line: '17356166508'
Test #6:
score: 7
Accepted
time: 2ms
memory: 32812kb
input:
1 91 1 162454772 1 470082830 1 143610614 1 831070014 1 146983411 1 789045826 1 82582863 1 652480352 1 119027154 1 612678289 1 544551347 1 937361107 1 925273426 1 143655813 1 9085899 1 588299483 1 859313272 1 294149571 1 735356175 1 55633143 1 366991221 1 687012588 1 773929825 1 737277036 1 229399700...
output:
22730104544
result:
ok single line: '22730104544'
Test #7:
score: 7
Accepted
time: 0ms
memory: 32840kb
input:
1 98 1 492984845 1 603301504 1 827779013 1 543424271 1 353910948 1 498019066 1 188352400 1 421972590 1 541331626 1 790810535 1 384601943 1 691547921 1 617300120 1 647962561 1 650218429 1 425228920 1 274801429 1 550676594 1 641040777 1 380212377 1 634255275 1 713589024 1 320089450 1 691185086 1 94179...
output:
22934390057
result:
ok single line: '22934390057'
Test #8:
score: 7
Accepted
time: 0ms
memory: 32612kb
input:
1 96 1 676422399 1 383144101 1 908806402 1 195899706 1 858455939 1 680490408 1 612812422 1 205243456 1 937363104 1 467111199 1 445555414 1 213343027 1 415956505 1 963661454 1 937156337 1 556569608 1 344720216 1 835918329 1 893856969 1 184325381 1 53110184 1 524825668 1 356433261 1 686051960 1 263022...
output:
22860762666
result:
ok single line: '22860762666'
Test #9:
score: 7
Accepted
time: 0ms
memory: 31724kb
input:
1 87 1 411199713 1 407170309 1 411199713 1 690858790 1 411199713 1 411199713 1 411199713 1 407170309 1 973231939 1 108775390 1 108775390 1 973231939 1 973231939 1 973231939 1 176781105 1 690858790 1 407170309 1 176781105 1 411199713 1 176781105 1 493892183 1 411199713 1 493892183 1 411199713 1 69085...
output:
15512296902
result:
ok single line: '15512296902'
Test #10:
score: 7
Accepted
time: 7ms
memory: 32208kb
input:
1 95 1 556207110 1 813583629 1 263051757 1 263051757 1 556207110 1 263051757 1 813583629 1 263051757 1 813583629 1 263051757 1 813583629 1 813583629 1 556207110 1 263051757 1 263051757 1 556207110 1 556207110 1 813583629 1 813583629 1 263051757 1 813583629 1 556207110 1 556207110 1 813583629 1 81358...
output:
18060215274
result:
ok single line: '18060215274'
Subtask #2:
score: 19
Accepted
Test #11:
score: 19
Accepted
time: 6ms
memory: 33228kb
input:
6 7 1 65 1 115 2 33 1 298 1 42 3 33 4 6 1 180 5 2 6 126 5 2 2 177
output:
302
result:
ok single line: '302'
Test #12:
score: 19
Accepted
time: 6ms
memory: 32836kb
input:
13 23 1 48 2 167 1 248 2 48 3 61 1 194 1 126 4 33 4 47 5 138 1 262 1 299 6 24 6 7 4 18 6 7 7 49 8 53 4 11 9 3 10 2 4 41 11 39 6 5 12 2 2 172 5 123 10 4 8 63 6 5 13 1 4 47 13 1 13 1 13 1
output:
374
result:
ok single line: '374'
Test #13:
score: 19
Accepted
time: 3ms
memory: 32720kb
input:
21 38 1 24 1 32 2 177 3 203 4 47 3 59 5 5 1 1 6 48 1 181 7 92 7 68 8 1 4 8 9 106 10 1 11 22 12 108 13 6 13 5 4 61 1 11 14 6 12 52 15 35 16 55 2 10 17 1 7 100 8 56 10 3 2 106 17 1 18 1 11 104 8 2 13 116 19 5 10 3 9 195 6 4 9 100 20 128 9 243 13 110 3 82 4 96 8 46 13 31 11 77 7 22 21 124 9 287 7 169 3...
output:
1917
result:
ok single line: '1917'
Test #14:
score: 19
Accepted
time: 3ms
memory: 33180kb
input:
33 38 1 18 1 81 2 100 1 245 3 102 4 5 2 204 5 26 6 37 7 10 8 41 6 52 7 100 4 71 9 25 3 172 10 61 11 144 12 33 9 9 9 17 11 46 13 15 5 17 14 20 15 87 14 73 2 223 8 41 1 283 11 96 16 1 17 4 18 5 19 7 9 17 20 4 17 31 8 2 13 58 21 9 22 2 23 121 7 102 24 50 9 2 25 7 26 13 27 14 28 4 29 45 16 3 20 2 1 219 ...
output:
804
result:
ok single line: '804'
Test #15:
score: 19
Accepted
time: 0ms
memory: 31664kb
input:
93 1 1 2 2 2 3 1 4 1 5 2 6 2 7 2 8 1 9 1 10 1 11 2 12 2 13 1 14 2 15 2 16 2 17 1 18 1 19 1 20 1 21 2 22 2 23 1 24 1 25 2 26 1 27 2 28 2 29 1 30 1 31 1 32 2 33 1 34 2 35 2 36 2 37 1 38 1 39 2 40 1 41 1 42 1 43 1 44 2 45 2 46 2 47 1 48 1 49 1 50 2 51 2 52 2 53 2 54 1 55 1 56 2 57 1 58 2 59 1 60 1 61 2...
output:
0
result:
ok single line: '0'
Test #16:
score: 19
Accepted
time: 4ms
memory: 32272kb
input:
55 62 1 118 2 144 1 264 1 113 1 5 3 27 4 12 5 68 2 21 6 44 7 7 8 15 9 80 2 65 8 3 5 32 10 151 11 137 12 1 2 153 13 5 8 15 7 5 14 24 15 70 1 73 16 13 17 122 5 135 10 131 13 8 4 31 18 5 3 17 19 52 14 6 11 243 20 1 21 10 9 42 14 24 22 1 23 5 3 37 14 33 24 1 25 1 3 26 26 30 9 59 22 2 27 43 24 5 4 33 28 ...
output:
1219
result:
ok single line: '1219'
Test #17:
score: 19
Accepted
time: 3ms
memory: 32060kb
input:
63 67 1 121 2 106 1 147 3 4 4 87 3 36 5 52 6 21 7 21 2 169 8 8 2 165 2 46 7 33 8 13 9 34 10 7 4 119 3 43 1 225 1 2 11 5 12 4 1 137 1 215 13 12 14 1 15 2 3 55 16 3 14 95 17 3 12 7 18 6 19 1 20 12 19 29 21 53 22 71 8 10 5 24 8 4 23 3 24 1 25 100 11 9 12 8 4 60 4 36 14 47 26 44 9 22 27 1 25 76 13 3 28 ...
output:
934
result:
ok single line: '934'
Test #18:
score: 19
Accepted
time: 0ms
memory: 33164kb
input:
66 88 1 246 2 6 3 2 4 21 2 34 2 40 5 10 6 2 5 11 7 3 1 231 8 1 8 11 4 25 4 42 1 253 9 5 4 30 10 1 11 3 12 20 12 9 13 3 7 12 14 2 15 10 16 1 17 44 6 17 1 6 18 5 4 29 19 7 1 252 20 6 21 1 22 30 23 12 20 11 24 7 25 1 8 9 14 2 26 1 20 7 27 1 28 2 29 2 30 2 31 292 21 3 21 1 32 3 15 14 33 3 28 1 34 8 25 1...
output:
594
result:
ok single line: '594'
Test #19:
score: 19
Accepted
time: 0ms
memory: 31612kb
input:
78 99 1 204 1 105 1 137 2 3 3 125 4 21 5 65 6 39 7 41 2 41 8 6 6 14 9 17 10 46 4 44 11 20 1 183 4 103 12 13 13 18 14 9 13 17 3 37 15 1 16 4 17 9 18 78 2 56 11 39 19 8 20 2 21 29 13 12 22 3 2 64 1 125 23 32 24 119 5 89 9 27 25 39 26 16 27 7 28 8 29 3 29 19 7 30 30 4 16 112 14 12 22 1 31 51 32 3 33 3 ...
output:
1443
result:
ok single line: '1443'
Test #20:
score: 19
Accepted
time: 0ms
memory: 32844kb
input:
89 101 1 120 2 60 1 229 1 156 3 32 4 44 5 105 2 85 2 53 6 11 7 7 4 62 8 15 3 24 9 32 10 94 11 23 12 4 13 5 14 7 10 82 3 98 5 44 15 28 16 43 1 146 16 5 17 25 9 59 17 16 2 118 17 31 18 47 19 10 20 2 8 34 21 8 22 3 23 3 24 65 25 34 26 14 27 61 10 75 2 175 3 89 6 71 28 9 29 3 30 4 31 10 32 16 33 1 34 2 ...
output:
1341
result:
ok single line: '1341'
Test #21:
score: 19
Accepted
time: 0ms
memory: 31520kb
input:
101 112 1 172 2 49 3 19 4 3 1 86 1 217 5 28 1 141 6 160 7 35 8 9 9 25 9 109 9 36 6 103 10 37 3 56 11 31 7 58 12 12 2 99 13 88 5 3 14 6 11 39 15 84 16 19 13 106 9 100 17 12 10 41 18 1 17 7 19 10 20 16 21 5 22 16 23 23 1 222 24 33 25 30 26 3 27 7 28 14 29 14 20 20 30 32 2 43 30 40 31 3 23 32 3 17 2 93...
output:
939
result:
ok single line: '939'
Test #22:
score: 19
Accepted
time: 0ms
memory: 31496kb
input:
109 126 1 111 2 115 3 33 1 55 4 26 5 110 6 9 1 188 5 125 7 21 7 102 2 42 2 49 8 1 9 20 10 36 11 19 12 11 4 6 5 154 5 97 7 123 13 5 7 106 14 130 3 51 3 43 6 4 15 2 4 31 11 13 16 2 17 33 18 90 19 6 5 128 7 131 14 87 20 23 21 21 22 25 21 79 10 45 3 25 23 2 24 132 14 77 25 25 22 145 14 18 26 1 27 21 4 3...
output:
1725
result:
ok single line: '1725'
Test #23:
score: 19
Accepted
time: 7ms
memory: 32112kb
input:
117 141 1 107 1 44 2 46 3 99 4 37 5 127 6 46 7 1 1 210 8 59 1 175 1 68 5 56 6 28 9 13 3 86 10 66 11 1 12 38 13 228 12 94 13 180 12 38 8 22 14 1 15 64 16 11 13 159 17 112 18 21 17 47 7 15 19 1 11 1 18 11 20 67 21 2 22 10 23 38 12 53 24 19 25 1 15 34 26 20 27 6 28 1 29 28 30 1 3 204 7 27 10 58 21 1 31...
output:
1931
result:
ok single line: '1931'
Test #24:
score: 19
Accepted
time: 0ms
memory: 33180kb
input:
134 137 1 232 2 11 1 91 1 60 3 47 4 156 5 192 5 103 6 2 1 200 7 16 5 2 8 15 9 26 10 2 11 41 12 23 4 27 9 64 13 131 14 21 2 2 15 92 4 140 16 1 17 50 5 151 18 4 11 17 19 99 20 50 1 155 19 139 21 53 1 188 17 3 22 5 23 31 24 11 25 42 12 22 26 2 23 46 27 1 15 61 10 4 28 69 29 7 26 2 30 57 31 63 32 14 33 ...
output:
1934
result:
ok single line: '1934'
Test #25:
score: 19
Accepted
time: 0ms
memory: 33040kb
input:
293 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61 ...
output:
0
result:
ok single line: '0'
Test #26:
score: 19
Accepted
time: 0ms
memory: 31984kb
input:
139 161 1 108 2 10 2 10 3 51 1 274 4 101 5 3 1 26 6 13 6 2 7 21 4 129 1 253 8 7 2 116 9 98 10 5 11 9 12 28 4 9 13 34 14 12 15 64 16 21 17 86 18 4 2 21 5 25 19 3 16 9 20 3 2 178 12 4 21 104 7 29 22 8 15 90 23 2 15 46 17 169 24 19 25 50 26 12 13 22 20 22 27 1 28 152 9 196 5 95 29 25 30 9 31 57 32 11 3...
output:
2627
result:
ok single line: '2627'
Test #27:
score: 19
Accepted
time: 4ms
memory: 32184kb
input:
130 170 1 166 2 66 1 9 1 94 1 133 3 2 4 244 5 100 6 127 7 20 8 6 6 85 9 65 4 223 10 14 6 142 11 16 8 4 9 4 5 83 1 279 8 23 6 139 12 11 13 50 12 34 9 43 14 31 15 47 16 8 3 62 14 7 17 5 12 38 18 26 12 38 19 19 7 11 17 19 20 68 21 82 16 3 22 19 9 99 23 3 9 5 24 6 25 13 25 27 14 4 26 20 27 3 28 8 17 22 ...
output:
1683
result:
ok single line: '1683'
Test #28:
score: 19
Accepted
time: 3ms
memory: 32440kb
input:
137 163 1 14 2 97 3 91 1 91 4 28 5 89 3 55 6 47 1 159 7 33 5 116 8 19 9 2 10 103 11 78 1 170 1 261 12 10 13 43 1 41 14 8 15 5 10 14 16 5 17 82 18 3 17 99 19 75 20 43 21 140 18 10 7 16 18 19 22 2 15 22 22 2 8 2 16 1 18 4 12 16 23 23 24 10 25 1 26 10 27 29 28 12 29 5 1 235 30 11 31 50 32 19 33 51 34 3...
output:
1711
result:
ok single line: '1711'
Test #29:
score: 19
Accepted
time: 3ms
memory: 32544kb
input:
140 160 1 116 2 62 1 228 3 54 4 14 3 112 3 33 5 14 6 23 1 44 7 2 2 124 2 7 2 9 8 76 9 9 10 4 11 240 11 7 2 69 12 1 1 123 13 34 14 119 15 36 16 3 17 16 5 7 18 7 19 2 20 94 21 5 22 1 8 30 18 22 23 37 24 11 19 7 3 22 25 4 26 79 27 3 8 55 22 1 5 54 28 7 29 13 1 51 30 4 11 22 31 6 32 64 7 6 12 4 33 51 34...
output:
2102
result:
ok single line: '2102'
Test #30:
score: 19
Accepted
time: 0ms
memory: 31792kb
input:
121 179 1 16 1 66 2 7 3 69 4 14 1 273 5 64 6 71 1 121 7 9 8 65 7 4 9 150 10 75 11 3 2 277 5 52 12 10 13 2 8 87 8 27 14 22 15 7 11 5 9 104 16 4 17 3 12 5 18 107 19 13 5 28 1 107 2 164 20 15 21 7 16 7 22 15 23 2 17 5 12 29 22 34 6 186 24 54 25 9 7 25 26 28 27 3 28 1 29 28 11 13 30 3 31 3 30 2 1 112 5 ...
output:
2300
result:
ok single line: '2300'
Subtask #3:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #31:
score: 0
Wrong Answer
time: 4ms
memory: 32524kb
input:
301 162 1 920419098 2 13883963 3 922640814 4 576832601 5 458005774 6 627831463 7 637684083 8 363092299 9 608472070 10 993759086 11 191942487 12 177579899 11 778884458 13 475677264 14 618356404 15 782795435 16 407631159 17 814281326 18 753827889 19 190238739 20 344692223 21 212165514 22 256037358 23 ...
output:
339947868906
result:
wrong answer 1st lines differ - expected: '132811066618', found: '339947868906'
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%