QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#226358#7563. Fun on Treegrass8cowWA 1176ms56004kbC++142.9kb2023-10-25 21:16:032023-10-25 21:16:03

Judging History

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

  • [2023-10-25 21:16:03]
  • 评测
  • 测评结果:WA
  • 用时:1176ms
  • 内存:56004kb
  • [2023-10-25 21:16:03]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
int top[200100],dfn[200100],fa[200100],sz[201000],son[200010],d[200100];
ll ds[200100];
struct ed{int v,w;};
vector<ed>g[200100];
void dfs(int x){
	sz[x]=1;int mm=-1;
	for(ed t:g[x]){
		int v=t.v;
		fa[v]=x,d[v]=d[x]+1,ds[v]=ds[x]+t.w,dfs(v),sz[x]+=sz[v]; 
		if(mm<sz[v])mm=sz[v],son[x]=v;
	}
}
int bel[200100];
void dfs2(int x,int ff){
	top[x]=ff,bel[dfn[x]=++dfn[0]]=x;
	if(!son[x])return;
	dfs2(son[x],ff);for(ed t:g[x])if(t.v!=son[x])
	dfs2(t.v,t.v);
}
const ll I=1e18;
struct zm{int ip;ll z;}; 
zm operator + (const zm &a,const zm &b){
	if(a.z!=b.z)return (a.z>b.z)?a:b;
	return (a.ip<b.ip)?a:b; 
}
struct SG{
	zm mx[801000];ll tg[800100];
	inline void ad(int p,ll z){tg[p]+=z,mx[p].z+=z;}
	inline void pd(int p){ad(p<<1,tg[p]),ad(p<<1|1,tg[p]),tg[p]=0;}
	inline void build(int p,int l,int r,zm *a){
		if(l==r){mx[p]=a[l];return;}
		int mid=(l+r)>>1;
		build(p<<1,l,mid,a),build(p<<1|1,mid+1,r,a);
		mx[p]=mx[p<<1]+mx[p<<1|1];
	}
	inline void up(int p,int l,int r,int x,int y,ll z){
		if(x<=l&&r<=y)return ad(p,z);if(y<l||x>r)return;
		pd(p);int mid=(l+r)>>1;
		up(p<<1,l,mid,x,y,z),up(p<<1|1,mid+1,r,x,y,z);
		mx[p]=mx[p<<1]+mx[p<<1|1];
	}
	inline void up2(int p,int l,int r,int x,zm z){
		if(l==r){mx[p]=z;return;}
		pd(p);int mid=(l+r)>>1;
		if(x<=mid)up2(p<<1,l,mid,x,z);
		else up2(p<<1|1,mid+1,r,x,z);
		mx[p]=mx[p<<1]+mx[p<<1|1];
	}
	inline zm ask(int p,int l,int r,int x,int y){
		if(x<=l&&r<=y)return mx[p];if(y<l||x>r)return (zm){0,-I};
		pd(p);int mid=(l+r)>>1;
		return ask(p<<1,l,mid,x,y)+ask(p<<1|1,mid+1,r,x,y);
	}
}T1,T2;
int n,q;
zm que(int x){
	zm mx=T1.ask(1,1,n,dfn[x],dfn[x]);
	if(son[x])mx=mx+T1.ask(1,1,n,dfn[son[x]]+sz[son[x]],dfn[x]+sz[x]-1);
	mx.z-=ds[x]*2;
	return mx;
}
ll a[200100];zm fz[201000];
zm xd(int x,int f){
	zm pd=T1.ask(1,1,n,dfn[f],dfn[x]-1);
	if(dfn[x]+sz[x]<dfn[f]+sz[f])pd=pd+T1.ask(1,1,n,dfn[x]+sz[x],dfn[f]+sz[f]-1);
	return pd;
}
int main(){
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=2,f,w;i<=n;i++){
		scanf("%d%d",&f,&w);
		g[f].pb((ed){i,w});
	}
	dfs(1),dfs2(1,1);
	for(int i=1;i<=n;i++)fz[dfn[i]]=(zm){i,-a[i]+ds[i]};
	T1.build(1,1,n,fz);
	for(int i=1;i<=n;i++)fz[dfn[i]]=que(i);
	T2.build(1,1,n,fz);
	for(int ie=1,x,z,u;ie<=q;ie++){
		scanf("%d%d%d",&u,&x,&z),z=-z;
		T1.up(1,1,n,dfn[x],dfn[x]+sz[x]-1,z);
		T2.up(1,1,n,dfn[x],dfn[x]+sz[x]-1,z);
		while(1){
			int f=fa[top[x]];if(!f)break;
			x=f,T2.up2(1,1,n,dfn[x],que(x));
		}
		x=u;
		zm mx=T1.ask(1,1,n,dfn[x],dfn[x]+sz[x]-1);
		mx.z-=ds[x];
		while(x){
			int f=top[x];
			if(x!=f){
				zm t2=T2.ask(1,1,n,dfn[f],dfn[x]-1);t2.z+=ds[u];
				mx=mx+t2;
			}
			if(fa[f]){
				zm t2=xd(f,fa[f]);t2.z+=ds[u]-ds[f]*2;
				mx=mx+t2;
			}
			x=fa[f];
		}
		printf("%d %lld\n",mx.ip,mx.z);
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 22376kb

input:

6 6
1 1 4 5 1 4
1 5
2 0
3 2
4 1
5 6
3 2 -100000
1 2 100000
1 1 0
2 2 66
3 1 5
4 4 -3

output:

6 100005
6 10
6 10
1 4
1 -1
1 1

result:

ok 6 lines

Test #2:

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

input:

5 6
-10 0 2 -4 8
1 7
1 1
2 2
2 -2
1 1 100
2 1 -100
1 1 0
4 3 10
2 5 3
5 2 2

output:

4 -87
1 17
4 13
1 19
1 17
1 15

result:

ok 6 lines

Test #3:

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

input:

6 3
0 0 0 0 0 0
1 10
1 10
1 -100
4 10
4 11
1 1 0
4 1 0
1 4 1000

output:

2 10
6 11
2 10

result:

ok 3 lines

Test #4:

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

input:

2 0
1 1
1 3

output:


result:

ok 0 lines

Test #5:

score: -100
Wrong Answer
time: 1176ms
memory: 56004kb

input:

200000 200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...

output:

119017 15000000000
120167 15000000000
119017 15000000000
119017 15000000000
120167 15000000000
120167 13000000000
120167 14000000000
119017 17000000000
119017 16000000000
119017 12000000000
119017 17000000000
120167 14000000000
120167 12000000000
120167 15000000000
120167 16000000000
120167 14000000...

result:

wrong answer 2nd lines differ - expected: '120167 17000000000', found: '120167 15000000000'