QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#397672 | #2429. Conquer The World | C1942huangjiaxu | WA | 80ms | 34872kb | C++14 | 1.6kb | 2024-04-24 15:47:58 | 2024-04-24 15:47:59 |
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];
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;
rt3[x]=merge(rt3[x],u);
}
}
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: 3ms
memory: 9788kb
Test #2:
score: 0
Accepted
time: 4ms
memory: 9796kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 9784kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 9796kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 9844kb
Test #6:
score: 0
Accepted
time: 4ms
memory: 9888kb
Test #7:
score: 0
Accepted
time: 3ms
memory: 9900kb
Test #8:
score: 0
Accepted
time: 4ms
memory: 10084kb
Test #9:
score: 0
Accepted
time: 34ms
memory: 23260kb
Test #10:
score: 0
Accepted
time: 56ms
memory: 24628kb
Test #11:
score: 0
Accepted
time: 80ms
memory: 34872kb
Test #12:
score: -100
Wrong Answer
time: 0ms
memory: 9792kb