QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#397712#2429. Conquer The WorldC1942huangjiaxuWA 72ms39232kbC++141.5kb2024-04-24 16:09:422024-04-24 16:09:43

Judging History

你现在查看的是最新测评结果

  • [2024-04-24 16:09:43]
  • 评测
  • 测评结果:WA
  • 用时:72ms
  • 内存:39232kb
  • [2024-04-24 16:09:42]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2.5e5+5;
typedef long long ll;
int n,a[N],b[N],rt1[N],rt2[N],rt3[N],cnt;
vector<pair<int,int> >e[N];
ll inf=1e18,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=tr[u].va-w;
		pop(rt2[x]),pop(rt3[x]);
		tr[u].lc=tr[u].rc=tr[u].d=0;
		rt2[x]=merge(rt2[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: 0ms
memory: 14080kb

Test #2:

score: 0
Accepted
time: 0ms
memory: 14144kb

Test #3:

score: 0
Accepted
time: 0ms
memory: 12064kb

Test #4:

score: 0
Accepted
time: 2ms
memory: 14152kb

Test #5:

score: 0
Accepted
time: 2ms
memory: 14136kb

Test #6:

score: 0
Accepted
time: 2ms
memory: 12292kb

Test #7:

score: 0
Accepted
time: 0ms
memory: 12160kb

Test #8:

score: 0
Accepted
time: 3ms
memory: 14516kb

Test #9:

score: 0
Accepted
time: 30ms
memory: 28848kb

Test #10:

score: 0
Accepted
time: 48ms
memory: 29064kb

Test #11:

score: 0
Accepted
time: 72ms
memory: 39232kb

Test #12:

score: 0
Accepted
time: 0ms
memory: 14256kb

Test #13:

score: 0
Accepted
time: 3ms
memory: 14100kb

Test #14:

score: -100
Wrong Answer
time: 3ms
memory: 14148kb