QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#397713 | #2429. Conquer The World | C1942huangjiaxu | AC ✓ | 521ms | 88184kb | C++14 | 1.6kb | 2024-04-24 16:10:04 | 2024-04-24 16:10:05 |
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;
}
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 14164kb
Test #2:
score: 0
Accepted
time: 2ms
memory: 12164kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 11980kb
Test #4:
score: 0
Accepted
time: 2ms
memory: 14080kb
Test #5:
score: 0
Accepted
time: 2ms
memory: 14028kb
Test #6:
score: 0
Accepted
time: 2ms
memory: 14276kb
Test #7:
score: 0
Accepted
time: 2ms
memory: 14092kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 16580kb
Test #9:
score: 0
Accepted
time: 34ms
memory: 29100kb
Test #10:
score: 0
Accepted
time: 52ms
memory: 28996kb
Test #11:
score: 0
Accepted
time: 74ms
memory: 39220kb
Test #12:
score: 0
Accepted
time: 0ms
memory: 14268kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 14240kb
Test #14:
score: 0
Accepted
time: 0ms
memory: 14220kb
Test #15:
score: 0
Accepted
time: 3ms
memory: 14416kb
Test #16:
score: 0
Accepted
time: 2ms
memory: 14356kb
Test #17:
score: 0
Accepted
time: 4ms
memory: 14564kb
Test #18:
score: 0
Accepted
time: 5ms
memory: 15728kb
Test #19:
score: 0
Accepted
time: 12ms
memory: 21196kb
Test #20:
score: 0
Accepted
time: 77ms
memory: 47012kb
Test #21:
score: 0
Accepted
time: 82ms
memory: 24760kb
Test #22:
score: 0
Accepted
time: 91ms
memory: 24488kb
Test #23:
score: 0
Accepted
time: 85ms
memory: 25980kb
Test #24:
score: 0
Accepted
time: 52ms
memory: 21124kb
Test #25:
score: 0
Accepted
time: 58ms
memory: 21884kb
Test #26:
score: 0
Accepted
time: 102ms
memory: 26140kb
Test #27:
score: 0
Accepted
time: 90ms
memory: 42484kb
Test #28:
score: 0
Accepted
time: 88ms
memory: 35216kb
Test #29:
score: 0
Accepted
time: 130ms
memory: 51712kb
Test #30:
score: 0
Accepted
time: 176ms
memory: 69776kb
Test #31:
score: 0
Accepted
time: 159ms
memory: 58992kb
Test #32:
score: 0
Accepted
time: 137ms
memory: 53532kb
Test #33:
score: 0
Accepted
time: 134ms
memory: 52980kb
Test #34:
score: 0
Accepted
time: 261ms
memory: 59332kb
Test #35:
score: 0
Accepted
time: 197ms
memory: 52900kb
Test #36:
score: 0
Accepted
time: 225ms
memory: 55124kb
Test #37:
score: 0
Accepted
time: 195ms
memory: 70992kb
Test #38:
score: 0
Accepted
time: 230ms
memory: 87460kb
Test #39:
score: 0
Accepted
time: 374ms
memory: 81968kb
Test #40:
score: 0
Accepted
time: 217ms
memory: 57192kb
Test #41:
score: 0
Accepted
time: 245ms
memory: 64936kb
Test #42:
score: 0
Accepted
time: 214ms
memory: 59788kb
Test #43:
score: 0
Accepted
time: 201ms
memory: 59760kb
Test #44:
score: 0
Accepted
time: 196ms
memory: 63316kb
Test #45:
score: 0
Accepted
time: 222ms
memory: 69688kb
Test #46:
score: 0
Accepted
time: 0ms
memory: 14016kb
Test #47:
score: 0
Accepted
time: 79ms
memory: 39280kb
Test #48:
score: 0
Accepted
time: 0ms
memory: 14208kb
Test #49:
score: 0
Accepted
time: 129ms
memory: 51104kb
Test #50:
score: 0
Accepted
time: 85ms
memory: 25940kb
Test #51:
score: 0
Accepted
time: 2ms
memory: 14168kb
Test #52:
score: 0
Accepted
time: 0ms
memory: 12164kb
Test #53:
score: 0
Accepted
time: 2ms
memory: 14144kb
Test #54:
score: 0
Accepted
time: 2ms
memory: 14104kb
Test #55:
score: 0
Accepted
time: 2ms
memory: 14160kb
Test #56:
score: 0
Accepted
time: 0ms
memory: 14160kb
Test #57:
score: 0
Accepted
time: 2ms
memory: 14256kb
Test #58:
score: 0
Accepted
time: 6ms
memory: 14944kb
Test #59:
score: 0
Accepted
time: 60ms
memory: 25872kb
Test #60:
score: 0
Accepted
time: 96ms
memory: 35076kb
Test #61:
score: 0
Accepted
time: 216ms
memory: 67308kb
Test #62:
score: 0
Accepted
time: 285ms
memory: 70672kb
Test #63:
score: 0
Accepted
time: 337ms
memory: 72536kb
Test #64:
score: 0
Accepted
time: 332ms
memory: 70392kb
Test #65:
score: 0
Accepted
time: 521ms
memory: 72632kb
Test #66:
score: 0
Accepted
time: 422ms
memory: 71556kb
Test #67:
score: 0
Accepted
time: 264ms
memory: 76636kb
Test #68:
score: 0
Accepted
time: 221ms
memory: 87112kb
Test #69:
score: 0
Accepted
time: 376ms
memory: 88184kb
Test #70:
score: 0
Accepted
time: 379ms
memory: 87604kb