如果树剖过了样例但是测评全都wa,并且标记异或下传
查看原帖
如果树剖过了样例但是测评全都wa,并且标记异或下传
448965
杨丶老爹楼主2021/8/9 21:38

rt,本蒟蒻调了一早上了,希望dalao帮助

#include<bits/stdc++.h>
using namespace std;
const int N=1e6;
int n,m,a[N],cnt,head[N],dep[N],fa[N],num[N],son[N],tot,top[N],pre[N],tpos[N],fan[N],val[N];
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
struct edge{
	int w,next,to,from;
}edge[N];
struct node{
	int minn,maxx,v;
}sum[N];
void add_edge(int u,int v,int w)
{
	edge[++cnt].next=head[u];
	edge[cnt].w=w;
	edge[cnt].to=v;
	edge[cnt].from=u;
	head[u]=cnt;
}
void dfs1(int u,int f)
{
	fa[u]=f;
	num[u]=1;
	for(int i=head[u];i;i=edge[i].next)
	{
		int v=edge[i].to,w=edge[i].w;
		if(v==fa[u]) continue;
		dfs1(v,u);
		a[v]=w;
		dep[v]=dep[u]+1;
		num[u]+=num[v];
		if(num[son[u]]<num[v]) son[u]=v;
	}
}
void dfs2(int u,int tp)
{
	top[u]=tp;
	tpos[u]=++tot;
	pre[tot]=u;
	if(son[u]!=0) dfs2(son[u],tp);
	for(int i=head[u];i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(v==fa[u] || v==son[u]) continue;
		dfs2(v,v);
	}
}
void pushup(int rt)
{
	sum[rt].v=sum[rt<<1].v+sum[rt<<1|1].v;
	sum[rt].maxx=max(sum[rt<<1].maxx,sum[rt<<1|1].maxx);
	sum[rt].minn=min(sum[rt<<1].minn,sum[rt<<1|1].minn);
}
void build (int l,int r,int rt)
{
//	fan[rt]=1;
	if(l==r)
	{
		sum[rt].v=a[pre[l]];
		sum[rt].maxx=a[pre[l]];
		sum[rt].minn=a[pre[l]];
		return;
	}
	int mid=l+r>>1;
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	pushup(rt);
}
void pushdown(int rt,int ln,int rn)
{
	if(fan[rt]==1)
	{
		fan[rt<<1]^=1;
		fan[rt<<1|1]^=1;
		sum[rt<<1].v=-sum[rt<<1].v;
		sum[rt<<1|1].v=-sum[rt<<1|1].v; 
		sum[rt<<1].maxx=-sum[rt<<1].maxx;
		sum[rt<<1|1].maxx=-sum[rt<<1|1].maxx;
		sum[rt<<1].minn=-sum[rt<<1].minn;
		sum[rt<<1|1].minn=-sum[rt<<1|1].minn;
		swap(sum[rt<<1].maxx,sum[rt<<1].minn);
		swap(sum[rt<<1|1].maxx,sum[rt<<1|1].minn);
		fan[rt]=0;
	}
}
void modify(int l,int r,int rt,int a,int c)
{
	if(l==a && r==a)
	{
		sum[rt].v=c;
		sum[rt].maxx=c;
		sum[rt].minn=c;
		return;
	}
	int mid=l+r>>1;
	pushdown(rt,mid-l+1,r-mid);
	if(a<=mid) modify(l,mid,rt<<1,a,c);
	if(a>mid) modify(mid+1,r,rt<<1|1,a,c);
	pushup(rt);
}
void opp(int l,int r,int rt,int nowl,int nowr)
{
	if(nowl<=l && nowr>=r)
	{
		fan[rt]^=1;
		sum[rt].v=-sum[rt].v;
		sum[rt].maxx=-sum[rt].maxx;
		sum[rt].minn=-sum[rt].minn;
		swap(sum[rt].maxx,sum[rt].minn);
		return;
	}
	int mid=l+r>>1;
	pushdown(rt,mid-l+1,r-mid);
	if(nowl<=mid) oppo(l,mid,rt<<1,nowl,nowr);
	if(nowr>mid) oppo(mid+1,r,rt<<1|1,nowl,nowr);
	pushup(rt);
}
int querysum(int l,int r,int rt,int nowl,int nowr)
{
	if(nowl<=r && nowr>=l) return sum[rt].v;
	int mid=l+r>>1;
	int ans=0;
	pushdown(rt,mid-l+1,r-mid);
	if(nowl<=mid) ans+=querysum(l,mid,rt<<1,nowl,nowr);
	if(nowr>mid) ans+=querysum(mid+1,r,rt<<1|1,nowl,nowr);
	pushup(rt);
	return ans;
}
int querymax(int l,int r,int rt,int nowl,int nowr)
{
	if(nowl<=l && nowr>=r) return sum[rt].maxx;
	int mid=l+r>>1;
	int ans=-2147483647;
	pushdown(rt,mid-l+1,r-mid);
	if(nowl<=mid) ans=max(ans,querymax(l,mid,rt<<1,nowl,nowr));
	if(nowr>mid) ans=max(ans,querymax(mid+1,r,rt<<1|1,nowl,nowr));
	pushup(rt);
	return ans;
}
int querymin(int l,int r,int rt,int nowl,int nowr)
{
	if(nowl<=l && nowr>=r) return sum[rt].minn;
	int mid=l+r>>1;
	int ans=2147483647;
	pushdown(rt,mid-l+1,r-mid);
	if(nowl<=mid) ans=min(ans,querymin(l,mid,rt<<1,nowl,nowr));
	if(nowr>mid) ans=min(ans,querymin(mid+1,r,rt<<1|1,nowl,nowr));
	pushup(rt);
	return ans;
}
void update(int x,int y)
{
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		opp(1,n,1,tpos[top[x]],tpos[x]);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	if(x!=y) opp(1,n,1,tpos[x]+1,tpos[y]);
}
int qsum(int x,int y)
{
	int ans=0;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		ans+=querysum(1,n,1,tpos[top[x]],tpos[x]);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	if(x!=y) ans+=querysum(1,n,1,tpos[x]+1,tpos[y]);
	return ans;
}
int qmax(int x,int y)
{
	int ans=-2147483647;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		ans=max(ans,querymax(1,n,1,tpos[top[x]],tpos[x]));
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	if(x!=y) ans=max(ans,querymax(1,n,1,tpos[x]+1,tpos[y]));
	return ans;
}
int qmin(int x,int y)
{
	int ans=2147483647;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		ans=min(ans,querymin(1,n,1,tpos[top[x]],tpos[x]));
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	if(x!=y) ans=min(ans,querymin(1,n,1,tpos[x]+1,tpos[y]));
	return ans;
}
int main()
{
	n=read();
	for(int i=1;i<n;i++)
	{
		int u=read()+1,v=read()+1,w=read();
		add_edge(u,v,w);
		add_edge(v,u,w);
	}
	dfs1(1,0);
	dfs2(1,1);
	build(1,n,1);
	m=read();
	for(int i=1;i<=m;i++)
	{
		char s[10];
		scanf("%s",s);
		int x=read(),y=read();
		if(s[0]=='C')
		{
			int temp;
			if(dep[edge[2*x-1].to]>dep[edge[2*x-1].from]) temp=edge[2*x-1].to;
			else temp=edge[x].from;
			modify(1,n,1,tpos[temp],y);
		}
		else if(s[0]=='N')
		{
			x++,y++;
			update(x,y);
		}
		else if(s[0]=='S')
		{
			x++,y++;
			printf("%d\n",qsum(x,y));
		}
		else if(s[0]=='M'&&s[1]=='A')
		{
			x++,y++;
			printf("%d\n",qmax(x,y));
		}
		else if(s[0]=='M'&&s[1]=='I')
		{
			x++,y++;
			printf("%d\n",qmin(x,y));
		}
	}
	return 0;
}
2021/8/9 21:38
加载中...