8pts只有11点AC了其余全WA
查看原帖
8pts只有11点AC了其余全WA
507534
YBaggio楼主2022/3/26 16:26
#include<bits/stdc++.h>
#define lc x<<1
#define rc x<<1|1
#define mid ((l+r)>>1)
#define inf 2000000000
#define int long long
using namespace std;
const int maxn=200010;
int dep[maxn],son[maxn],top[maxn],fa[maxn],dfn[maxn],tot,siz[maxn];
int to[maxn<<1],nex[maxn<<1],head[maxn<<1],cnt;
int maxx[maxn<<2],minn[maxn<<2],sum[maxn<<2],tag[maxn<<2];
int n,m;
void add(int x,int y){
	to[++cnt]=y;nex[cnt]=head[x];head[x]=cnt;
}
void dfs1(int x,int f){
	dep[x]=dep[f]+1;siz[x]=1;fa[x]=f;
	for(int i=head[x];i;i=nex[i]){
		if(to[i]==f)continue;
		dfs1(to[i],x);
		siz[x]+=siz[to[i]];
		if(siz[to[i]]>siz[son[x]])son[x]=to[i];
	}
	return;
}
void dfs2(int x,int stop){
	dfn[x]=++tot;top[x]=stop;
	if(!son[x])return;
	dfs2(son[x],stop);
	for(int i=head[x];i;i=nex[i])if(to[i]!=fa[x]&&to[i]!=son[x])dfs2(to[i],to[i]);
	return;
}
void pushup(int x){
	sum[x]=sum[lc]+sum[rc];
	maxx[x]=max(maxx[lc],maxx[rc]);
	minn[x]=min(minn[lc],minn[rc]);return;
}
void pushdown(int x){
	tag[lc]^=1;tag[rc]^=1;
	sum[lc]=-sum[lc];maxx[lc]=-maxx[lc];
	minn[lc]=-minn[lc];swap(minn[lc],maxx[lc]);
	sum[rc]=-sum[rc];maxx[rc]=-maxx[rc];
	minn[rc]=-minn[rc];swap(minn[rc],maxx[rc]);
	tag[x]=0;return;
}
void modify(int x,int l,int r,int s,int v){
	if(l==r){
		sum[x]=maxx[x]=minn[x]=v;
		return;
	}
	if(tag[x])pushdown(x);
	if(s<=mid)modify(lc,l,mid,s,v);
	if(s>mid)modify(rc,mid+1,r,s,v);
	pushup(x);return;
}
void modify_add(int x,int l,int r,int s,int v){
	if(l==r){
		sum[x]+=v;maxx[x]+=v;minn[x]+=v;
		return;
	}
	if(tag[x])pushdown(x);
	if(s<=mid)modify_add(lc,l,mid,s,v);
	if(s>mid)modify_add(rc,mid+1,r,s,v);
	pushup(x);return;
}
void change(int x,int l,int r,int s,int t){
	if(s<=l&&r<=t){
		tag[x]^=-1;
		sum[x]=-sum[x];
		maxx[x]=-maxx[x];
		minn[x]=-minn[x];
		swap(minn[x],maxx[x]);
		return;
	}
	if(tag[x])pushdown(x);
	if(s<=mid)change(lc,l,mid,s,t);
	if(t>mid)change(rc,mid+1,r,s,t);
	pushup(x);return;
}
int qsum(int x,int l,int r,int s,int t){
	int res=0;
	if(s<=l&&r<=t)return sum[x];
	if(tag[x])pushdown(x);
	if(s<=mid)res+=qsum(lc,l,mid,s,t);
	if(t>mid)res+=qsum(rc,mid+1,r,s,t);
	pushup(x);return res;
}
int qmax(int x,int l,int r,int s,int t){
	int res=-inf;
	if(s<=l&&r<=t)return maxx[x];
	if(tag[x])pushdown(x);
	if(s<=mid)res=max(res,qmax(lc,l,mid,s,t));
	if(t>mid)res=max(res,qmax(rc,mid+1,r,s,t));
	pushup(x);return res;
}
int qmin(int x,int l,int r,int s,int t){
	int res=inf;
	if(s<=l&&r<=t)return minn[x];
	if(tag[x])pushdown(x);
	if(s<=mid)res=min(res,qmin(lc,l,mid,s,t));
	if(t>mid)res=min(res,qmin(rc,mid+1,r,s,t));
	pushup(x);return res;
}
int lsum(int x,int y){
	int res=0;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		res+=qsum(1,1,tot,dfn[top[x]],dfn[x]);
		x=fa[top[x]]; 
	}
	if(x==y)return res;
	if(dep[x]>dep[y])swap(x,y);
	res+=qsum(1,1,tot,dfn[son[x]],y);
	return res;
}
int lmax(int x,int y){
	int res=-inf;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		res=max(res,qmax(1,1,tot,dfn[top[x]],dfn[x]));
		x=fa[top[x]]; 
	}
	if(x==y)return res;
	if(dep[x]>dep[y])swap(x,y);
	res=max(res,qmax(1,1,tot,dfn[son[x]],y));
	return res;
}
int lmin(int x,int y){
	int res=inf;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		res=min(res,qmin(1,1,tot,dfn[top[x]],dfn[x]));
		x=fa[top[x]]; 
	}
	if(x==y)return res;
	if(dep[x]>dep[y])swap(x,y);
	res=min(res,qmin(1,1,tot,dfn[son[x]],y));
	return res;
}
void cl(int x,int y){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		change(1,1,tot,dfn[top[x]],dfn[x]);
		x=fa[top[x]]; 
	}
	if(x==y)return;
	if(dep[x]>dep[y])swap(x,y);
	change(1,1,tot,dfn[son[x]],dfn[y]);
	return;
}
int X[maxn],Y[maxn],Z[maxn];
main(){
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		scanf("%lld%lld%lld",&X[i],&Y[i],&Z[i]);
		X[i]++;Y[i]++;
		add(X[i],Y[i]);add(Y[i],X[i]);
	}
	dfs1(1,0);dfs2(1,1);
	for(int i=1;i<n;i++){
		if(dep[Y[i]]<dep[X[i]])swap(X[i],Y[i]);
		modify_add(1,1,tot,dfn[Y[i]],Z[i]);
	}
	scanf("%lld",&m);
	while(m--){
		char s[5];int a,b;
		scanf("%s%lld%lld",s+1,&a,&b);
		if(s[1]=='S')printf("%lld\n",lsum(a+1,b+1));
		if(s[1]=='M'&&s[2]=='I')printf("%lld\n",lmin(a+1,b+1));
		if(s[1]=='M'&&s[2]=='A')printf("%lld\n",lmax(a+1,b+1));
		if(s[1]=='C')modify(1,1,tot,dfn[Y[a]],b);
		if(s[1]=='N')cl(a+1,b+1);
	}
	return 0;
}

2022/3/26 16:26
加载中...