QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#397697 | #2429. Conquer The World | C1942huangjiaxu | AC ✓ | 627ms | 88020kb | C++14 | 1.6kb | 2024-04-24 16:03:07 | 2024-04-24 16:03:08 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2.5e5+5;
typedef long long ll;
ll inf=1e18;
int n,a[N],b[N],rt1[N],rt2[N],rt3[N],cnt;
vector<pair<int,int> >e[N];
ll dp[N],ans;
struct node{
int lc,rc,d;
ll va;
}tr[N<<3];
int merge(int x,int y){
if(!x||!y)return x|y;
if(tr[x].va>tr[y].va)swap(x,y);
tr[x].rc=merge(tr[x].rc,y);
if(tr[tr[x].lc].d<tr[tr[x].rc].d)swap(tr[x].lc,tr[x].rc);
tr[x].d=tr[tr[x].rc].d+1;
return x;
}
int newnode(ll v){
tr[++cnt].va=v;
return cnt;
}
void pop(int &x){
x=merge(tr[x].lc,tr[x].rc);
}
void dfs(int x,int y){
b[x]-=a[x];
if(b[x]>0)while(b[x]--)rt1[x]=merge(rt1[x],newnode(dp[x]));
else while(b[x]++)rt2[x]=merge(rt2[x],newnode(dp[x]));
for(auto [v,w]:e[x])if(v!=y){
dp[v]=dp[x]+w;
dfs(v,x);
rt1[x]=merge(rt1[x],rt1[v]);
rt2[x]=merge(rt2[x],rt2[v]);
rt3[x]=merge(rt3[x],rt3[v]);
}
while(rt1[x]&&rt2[x]){
ans+=tr[rt1[x]].va+tr[rt2[x]].va-2ll*dp[x];
int u=rt2[x];
tr[u].va=2ll*dp[x]-tr[u].va;
pop(rt1[x]),pop(rt2[x]);
tr[u].lc=tr[u].rc=tr[u].d=0;
rt3[x]=merge(rt3[x],u);
}
while(rt2[x]&&rt3[x]){
ll w=tr[rt2[x]].va+tr[rt3[x]].va-2ll*dp[x];
if(w>=0)break;
ans+=w;
int u=rt2[x],v=rt3[x];
tr[v].va=tr[u].va-w;
tr[u].va=2ll*dp[x]-tr[u].va;
pop(rt2[x]),pop(rt3[x]);
tr[u].lc=tr[u].rc=tr[u].d=0;
tr[v].lc=tr[v].rc=tr[v].d=0;
rt3[x]=merge(rt3[x],u);
rt2[x]=merge(rt2[x],v);
}
}
int main(){
scanf("%d",&n);
for(int i=1,x,y,z;i<n;++i){
scanf("%d%d%d",&x,&y,&z);
e[x].emplace_back(y,z);
e[y].emplace_back(x,z);
}
for(int i=1;i<=n;++i)scanf("%d%d",&a[i],&b[i]);
dfs(1,0);
printf("%lld\n",ans);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 14152kb
Test #2:
score: 0
Accepted
time: 3ms
memory: 12092kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 14008kb
Test #4:
score: 0
Accepted
time: 3ms
memory: 14132kb
Test #5:
score: 0
Accepted
time: 3ms
memory: 14196kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 14348kb
Test #7:
score: 0
Accepted
time: 3ms
memory: 14132kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 16512kb
Test #9:
score: 0
Accepted
time: 23ms
memory: 27736kb
Test #10:
score: 0
Accepted
time: 53ms
memory: 29004kb
Test #11:
score: 0
Accepted
time: 97ms
memory: 39392kb
Test #12:
score: 0
Accepted
time: 4ms
memory: 14216kb
Test #13:
score: 0
Accepted
time: 3ms
memory: 12136kb
Test #14:
score: 0
Accepted
time: 3ms
memory: 14272kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 14432kb
Test #16:
score: 0
Accepted
time: 0ms
memory: 14396kb
Test #17:
score: 0
Accepted
time: 0ms
memory: 14480kb
Test #18:
score: 0
Accepted
time: 7ms
memory: 17120kb
Test #19:
score: 0
Accepted
time: 14ms
memory: 21704kb
Test #20:
score: 0
Accepted
time: 81ms
memory: 45128kb
Test #21:
score: 0
Accepted
time: 137ms
memory: 24640kb
Test #22:
score: 0
Accepted
time: 125ms
memory: 24672kb
Test #23:
score: 0
Accepted
time: 148ms
memory: 25876kb
Test #24:
score: 0
Accepted
time: 67ms
memory: 21932kb
Test #25:
score: 0
Accepted
time: 86ms
memory: 21076kb
Test #26:
score: 0
Accepted
time: 146ms
memory: 26208kb
Test #27:
score: 0
Accepted
time: 124ms
memory: 40836kb
Test #28:
score: 0
Accepted
time: 144ms
memory: 35068kb
Test #29:
score: 0
Accepted
time: 179ms
memory: 50984kb
Test #30:
score: 0
Accepted
time: 233ms
memory: 68072kb
Test #31:
score: 0
Accepted
time: 228ms
memory: 57872kb
Test #32:
score: 0
Accepted
time: 181ms
memory: 55288kb
Test #33:
score: 0
Accepted
time: 198ms
memory: 53224kb
Test #34:
score: 0
Accepted
time: 325ms
memory: 59776kb
Test #35:
score: 0
Accepted
time: 247ms
memory: 51816kb
Test #36:
score: 0
Accepted
time: 250ms
memory: 56080kb
Test #37:
score: 0
Accepted
time: 262ms
memory: 71484kb
Test #38:
score: 0
Accepted
time: 319ms
memory: 87472kb
Test #39:
score: 0
Accepted
time: 434ms
memory: 81836kb
Test #40:
score: 0
Accepted
time: 280ms
memory: 58216kb
Test #41:
score: 0
Accepted
time: 325ms
memory: 64844kb
Test #42:
score: 0
Accepted
time: 258ms
memory: 59204kb
Test #43:
score: 0
Accepted
time: 278ms
memory: 60144kb
Test #44:
score: 0
Accepted
time: 264ms
memory: 64152kb
Test #45:
score: 0
Accepted
time: 304ms
memory: 69572kb
Test #46:
score: 0
Accepted
time: 0ms
memory: 12048kb
Test #47:
score: 0
Accepted
time: 81ms
memory: 37808kb
Test #48:
score: 0
Accepted
time: 3ms
memory: 14196kb
Test #49:
score: 0
Accepted
time: 185ms
memory: 50856kb
Test #50:
score: 0
Accepted
time: 153ms
memory: 25976kb
Test #51:
score: 0
Accepted
time: 3ms
memory: 14200kb
Test #52:
score: 0
Accepted
time: 3ms
memory: 14152kb
Test #53:
score: 0
Accepted
time: 3ms
memory: 14148kb
Test #54:
score: 0
Accepted
time: 0ms
memory: 14184kb
Test #55:
score: 0
Accepted
time: 3ms
memory: 14200kb
Test #56:
score: 0
Accepted
time: 0ms
memory: 14048kb
Test #57:
score: 0
Accepted
time: 0ms
memory: 14196kb
Test #58:
score: 0
Accepted
time: 0ms
memory: 14868kb
Test #59:
score: 0
Accepted
time: 68ms
memory: 25864kb
Test #60:
score: 0
Accepted
time: 137ms
memory: 36724kb
Test #61:
score: 0
Accepted
time: 267ms
memory: 66776kb
Test #62:
score: 0
Accepted
time: 335ms
memory: 70164kb
Test #63:
score: 0
Accepted
time: 341ms
memory: 70676kb
Test #64:
score: 0
Accepted
time: 342ms
memory: 70580kb
Test #65:
score: 0
Accepted
time: 627ms
memory: 72728kb
Test #66:
score: 0
Accepted
time: 527ms
memory: 71704kb
Test #67:
score: 0
Accepted
time: 313ms
memory: 76692kb
Test #68:
score: 0
Accepted
time: 291ms
memory: 87168kb
Test #69:
score: 0
Accepted
time: 441ms
memory: 88020kb
Test #70:
score: 0
Accepted
time: 447ms
memory: 87600kb