WA on 4 ,玄关求条
查看原帖
WA on 4 ,玄关求条
926978
xiexinxin楼主2024/10/14 11:44

思路是求出dfn序,然后转换成线段树,除了初始值根据深度取反,其余正常加,然后输出时再取反。 代码

#include<bits/stdc++.h>
using namespace std;
int n,m,a[200001],u,v,b,dfn[200001],ls[200001],rs[200001],f[200001],c[200001],cnt,val;
vector<int> x[200001];
struct wendy{
	int l,r,z,laze;
}tree[1600001];
void la(int wz){
	if(tree[wz].laze!=0&&tree[wz].l!=tree[wz].r){
		tree[wz*2].laze+=tree[wz].laze;
		tree[wz*2+1].laze+=tree[wz].laze;
		if(tree[wz*2].l==tree[wz*2].r) tree[wz*2].z+=tree[wz].laze;
		if(tree[wz*2+1].l==tree[wz*2+1].r) tree[wz*2+1].z+=tree[wz].laze;
		tree[wz].laze=0;
	}
}
int dfs(int wz,int last){
	cnt++,dfn[wz]=cnt;
	f[cnt]=wz;
	c[wz]=-1*c[last];
	ls[wz]=rs[wz]=dfn[wz];
	for(int i=0;i<x[wz].size();i++){
		if(x[wz][i]!=last)rs[wz]=max(rs[wz],dfs(x[wz][i],wz));
	}
	return rs[wz];
}
void buid(int wz){
	int mid=(tree[wz/2].l+tree[wz/2].r)/2;
	if(wz%2==0){
		tree[wz].l=tree[wz/2].l;
		tree[wz].r=mid;
	}
	else {
		tree[wz].l=mid+1;
		tree[wz].r=tree[wz/2].r;
	}
	if(tree[wz].l!=tree[wz].r){
		buid(wz*2),buid(wz*2+1);
	}
	else {
		tree[wz].z=a[f[tree[wz].l]]*c[f[tree[wz].l]];
	}
}
void add(int z,int l,int r,int wz){
	la(wz);
	if(tree[wz].l>=l&&tree[wz].r<=r){
		tree[wz].laze+=z;
		if(tree[wz].l==tree[wz].r) tree[wz].z+=tree[wz].laze;
		return;
	}
	int mid=(tree[wz].l+tree[wz].r)/2;
	if(l<=mid&&r>=tree[wz].l) add(z,l,r,wz*2);
	if(r>=mid+1&&l<=tree[wz].r) add(z,l,r,wz*2+1);
}
int out(int l,int wz){
	la(wz);
	if(tree[wz].l==l&&tree[wz].r==l){
		return tree[wz].z;
	}
	int mid=(tree[wz].l+tree[wz].r)/2;
	if(l<=mid) return out(l,wz*2);
	if(l>=mid+1) return out(l,wz*2+1);
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<n;i++){
		cin>>u>>v;
		x[u].push_back(v);
		x[v].push_back(u);
	}
	c[0]=-1;
	dfs(1,0);
	tree[1].l=1,tree[1].r=n;
	if(n!=1) buid(2),buid(3);
	else tree[1].z=a[f[tree[1].l]];
	for(int i=1;i<=m;i++){
		cin>>b;
		if(b==1){
			cin>>u>>val;
			val=val*c[u];
			add(val,ls[u],rs[u],1);
		}
		else {
			cin>>u;
			cout<<c[u]*out(dfn[u],1)<<endl;
		}
	}
}
2024/10/14 11:44
加载中...