树剖全WA,求助
查看原帖
树剖全WA,求助
399171
CeyHigNmNu楼主2023/7/4 14:10
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
int n,m,mod,res;
int head[N],tot,cnt;
int a[N],siz[N],son[N],fa[N];
int deep[N],id[N],top[N],rk[N];
struct Stree{
	int l,r;
	int maxx,lazy1,lazy2;
	int ht1;
}xds[N*4];
struct edge{
	int to,nt,w;
}d[N*2];
void add(int u,int v,int w){
	d[++tot].to=v;
	d[tot].w=w;
	d[tot].nt=head[u];
	head[u]=tot;
}
//--------------dfs-----------------
void dfs1(int x,int f){
	fa[x]=f;
	deep[x]=deep[f]+1;
	siz[x]=1;
	for(int i=head[x];i;i=d[i].nt){ 
		int y=d[i].to;
		if(y==f) continue;
		a[y]=d[i].w;
		dfs1(y,x);
		siz[x]+=siz[y];
		if(siz[y]>siz[son[x]])
			son[x]=y;
	}
}
void dfs2(int x,int tp){
	top[x]=tp;
	id[x]=++cnt;
	rk[cnt]=a[x];
	if(!son[x]) return;
	dfs2(son[x],tp);
	for(int i=head[x];i;i=d[i].nt){
		int y=d[i].to;
		if(y==son[x]||y==fa[x]) 
			continue;
		dfs2(y,y);
	}
}
//--------------dfs-----------------
//--------------xds-----------------
void pushup(int x){
	xds[x].maxx=max(xds[x*2].maxx,xds[x*2+1].maxx);
	return;
}
void pushdown(int x){
	if(xds[x].ht1){
		xds[x*2].lazy1=xds[x].lazy1;
		xds[x*2+1].lazy1=xds[x].lazy1;
		
		xds[x*2].lazy2=xds[x].lazy2;
		xds[x*2+1].lazy2=xds[x].lazy2;
		
		xds[x*2].maxx=xds[x].lazy1+xds[x].lazy2;
		xds[x*2+1].maxx=xds[x].lazy1+xds[x].lazy2;
		
		xds[x*2].ht1=xds[x*2+1].ht1=1;
		
	}
	else{
		xds[x*2].lazy2+=xds[x].lazy2;
		xds[x*2+1].lazy2+=xds[x].lazy2;
		
		xds[x*2].maxx+=xds[x].lazy2;
		xds[x*2+1].maxx+=xds[x].lazy2;
	}
	xds[x].ht1=xds[x].lazy1=xds[x].lazy2=0;
} 
void build(int p,int l,int r){
	xds[p].l=l,xds[p].r=r;
	xds[p].ht1=0;
	if(l==r){
		xds[p].maxx=rk[l];
		return;
	}
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	pushup(p);
}
void change1(int x,int l,int r,int d){
	if(l<=xds[x].l&&xds[x].r<=r){
		xds[x].lazy1=d;
		xds[x].maxx=d;
		xds[x].ht1=1;
		xds[x].lazy2=0;
		return;
	}
//	cout<<xds[x].maxx<<"\n";
	pushdown(x);
	int mid=(xds[x].l+xds[x].r)>>1;
	if(l<=mid) change1(x*2,l,mid,d);
	if(r>mid) change1(x*2+1,mid+1,r,d);
	pushup(x);
}
void change2(int x,int l,int r,int d){
	if(l<=xds[x].l&&xds[x].r<=r){
		xds[x].lazy2+=d;
		xds[x].maxx+=d;
		return;
	}
	pushdown(x);
	int mid=(xds[x].l+xds[x].r)>>1;
	if(l<=mid) change2(x*2,l,mid,d);
	if(r>mid) change2(x*2+1,mid+1,r,d);
	pushup(x);
}
int query(int x,int l,int r){
	if(l<=xds[x].l&&xds[x].r<=r)
		return xds[x].maxx;
//	cout<<xds[x].maxx<<" ";
	pushdown(x);
	int ans=-1e18;
	int mid=(xds[x].l+xds[x].r)>>1;
	if(l<=mid) ans=max(ans,query(x*2,l,mid));
	if(r>mid) ans=max(ans,query(x*2+1,mid+1,r));
	return ans;
}
//--------------xds-----------------
//--------------shupou-----------------
void modify1(int x,int y,int d){
	int fx=top[x],fy=top[y];
	while(fx!=fy){
		if(deep[fx]>=deep[fy]){
			change1(1,id[fx],id[x],d);
			x=fa[fx],fx=top[x];
		}
		else{
			change1(1,id[fy],id[y],d);
			y=fa[fy],fy=top[y];
		}
	}
	if(id[x]<=id[y]) change1(1,id[x],id[y],d);
	else change1(1,id[y],id[x],d);
}
void modify2(int x,int y,int d){
	int fx=top[x],fy=top[y];
	while(fx!=fy){
		if(deep[fx]>=deep[fy]){
			change2(1,id[fx],id[x],d);
			x=fa[fx],fx=top[x];
		}
		else{
			change2(1,id[fy],id[y],d);
			y=fa[fy],fy=top[y];
		}
	}
	if(id[x]<=id[y]) change2(1,id[x],id[y],d);
	else change2(1,id[y],id[x],d);
}
int squery(int x,int y){
	int fx=top[x],fy=top[y],ans=-1e18;
	while(fx!=fy){
		if(deep[fx]>=deep[fy]){
			ans=max(ans,query(1,id[fx],id[x]));
			x=fa[fx],fx=top[x];
		}
		else{
			ans=max(ans,query(1,id[fy],id[y]));
			y=fa[fy],fy=top[y];
		}
	}
	if(id[x]<=id[y]) ans=max(ans,query(1,id[x]+1,id[y]));
	else ans=max(ans,query(1,id[y]+1,id[x]));
	return ans;
}
//--------------shupou-----------------
signed main(){
	cin>>n;
	for(int i=1;i<n;i++){
		int x,y,z;
		cin>>x>>y>>z;
		add(x,y,z);
		add(y,x,z);
	}
	dfs1(1,0);
	dfs2(1,1);
	build(1,1,n);
	string op;
	while(cin>>op){
		int x,y,z;
		if(op=="Stop")
			return 0;
		if(op=="Change"){ 
			cin>>x>>y;
			int v=deep[d[x*2-1].to]<deep[d[x*2].to]?d[x*2].to:d[x*2-1].to;
			change1(1,id[v],id[v],y);
		}
		if(op=="Cover"){
			cin>>x>>y>>z;
			modify1(x,y,z);
		}
		if(op=="Add"){
			cin>>x>>y>>z;
			modify2(x,y,z);
		}
		if(op=="Max"){
			cin>>x>>y;
			cout<<squery(x,y)<<endl;
		}
	}
	return 0;
}
2023/7/4 14:10
加载中...