QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#674495 | #3. Fireworks | SimonLJK | 7 | 7ms | 31188kb | C++17 | 1.3kb | 2024-10-25 16:06:12 | 2024-10-25 16:06:15 |
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];
int 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 dfs(int u){
int v,w;
for(pair<int,int> now:to[u]){
v=now.first; w=now.second;
dfs(v); b[v]+=w;
if(to[v].empty()) k[v]--;
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]=w;
rt[v]=merge(cntnode,rt[v]);
rt[u]=merge(rt[u],rt[v]);
k[u]+=k[v]; b[u]+=b[v];
}
for(int i=0;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];
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: 3ms
memory: 29320kb
input:
1 1 1 594911537
output:
0
result:
ok single line: '0'
Test #2:
score: 7
Accepted
time: 7ms
memory: 30008kb
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: 5ms
memory: 29792kb
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: 7ms
memory: 29944kb
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: 3ms
memory: 30744kb
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: 7ms
memory: 30004kb
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: 3ms
memory: 31188kb
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: 30740kb
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: 29496kb
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: 30344kb
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: 0
Wrong Answer
Test #11:
score: 0
Wrong Answer
time: 5ms
memory: 30860kb
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:
366
result:
wrong answer 1st lines differ - expected: '302', found: '366'
Subtask #3:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%