萌新求助0pts全RE
查看原帖
萌新求助0pts全RE
507534
YBaggio楼主2022/3/20 20:37
#include<bits/stdc++.h>
#define lc x<<1
#define rc x<<1|1
#define int long long 
using namespace std;
const int maxn=1000000+100,inf=2147483647;
int n,m,tag[maxn<<2],dep[maxn],son[maxn],fa[maxn],dfn[maxn],top[maxn];
int cnt,tot,head[maxn],wt[maxn],nex[maxn],to[maxn],size[maxn],root,a[maxn],val[maxn];
int sum[maxn<<2],maxx[maxn<<2],minn[maxn<<2];
void build(int x,int l,int r){
	if(l==r){
		sum[x]=maxx[x]=minn[x]=wt[l];
		return;
	}
	int mid=(l+r)>>1;
	build(lc,1,mid);build(rc,mid+1,r);
	return;
}
void dfs1(int x,int fat,int deep){
	size[x]=1;dep[x]=deep;fa[x]=fat;
	for(int i=head[x];i;i=nex[i]){
		if(to[i]==fat)continue;
		dfs1(to[i],x,deep+1);
		a[to[i]]=val[i];
		size[x]+=size[to[i]];
		if(size[to[i]]>size[son[x]])son[x]=to[i];
	}
	return;
}	
void dfs2(int x,int stop){
	dfn[x]=++tot;wt[tot]=a[x];top[x]=stop;
	if(!son[x])return;
	dfs2(son[x],stop);
	for(int i=head[x];i;i=nex[i])
		if(to[i]!=son[x]&&to[i]!=fa[x])dfs2(to[i],to[i]);
	return;
}	
void add(int u,int v,int w){to[++cnt]=v;val[cnt]=w;nex[cnt]=head[u];head[u]=cnt;}
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[lc]^=1;
	sum[lc]=-sum[lc];sum[rc]=-sum[rc];
	maxx[lc]=-maxx[lc];maxx[rc]=-maxx[rc];
	minn[lc]=-minn[lc];minn[rc]=-minn[rc];
	swap(maxx[lc],minn[lc]);swap(maxx[rc],minn[rc]);
	tag[x]=0;
}
void update(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);
	int mid=(l+r)>>1;
	if(s<=mid)update(lc,l,mid,s,v);
	if(s>mid)update(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(maxx[x],minn[x]);
		return;
	}
	if(tag[x])pushdown(x);
	int mid=(l+r)>>1;
	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);
	int mid=(l+r)>>1;
	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);
	int mid=(l+r)>>1;
	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);
	int mid=(l+r)>>1;
	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 tmp=0;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		tmp+=qsum(1,1,n,dfn[top[x]],dfn[x]);
		x=fa[top[x]];
	}	
	if(x==y)return tmp;
	if(dep[x]>dep[y])swap(x,y);
	tmp+=qsum(1,1,n,dfn[son[x]],dfn[y]);
	return tmp; 
}
int lmax(int x,int y){
	int tmp=-inf;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		tmp=max(tmp,qmax(1,1,n,dfn[top[x]],dfn[x]));
		x=fa[top[x]];
	}
	if(x==y)return tmp;
	if(dep[x]>dep[y])swap(x,y);
	tmp=max(tmp,qmax(1,1,n,dfn[son[x]],dfn[y]));
	return tmp;
}
int lmin(int x,int y){
	int tmp=inf;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		tmp=min(tmp,qmin(1,1,n,dfn[top[x]],dfn[x]));
		x=fa[top[x]];	
	}
	if(x==y)return tmp;
	if(dep[x]>dep[y])swap(x,y);
	tmp=min(tmp,qmin(1,1,n,dfn[son[x]],dfn[y]));
	return tmp;
}
void dlian(int x,int y){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		change(1,1,n,dfn[top[x]],dfn[x]);
		x=fa[top[x]];
	}
	if(x==y)return;
	if(dep[x]>dep[y])swap(x,y);
	change(1,1,n,dfn[son[x]],dfn[y]);
	return;
} 
int X[maxn],Y[maxn],Z[maxn];
signed main(){
	//freopen("P1505_1.in","r",stdin);
	//freopen("x.txt","w",stdout);
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		scanf("%lld%lld%lld",&X[i],&Y[i],&Z[i]);
		add(X[i],Y[i],Z[i]);add(Y[i],X[i],Z[i]);
	}
	dfs1(1,-1,1); dfs2(1,1);
	build(1,1,n);
	scanf("%lld",&m);
	while(m--){
		int x,y;
		char op[10];
		scanf("%s",op);
		if(op[0]=='S'){
			scanf("%lld%lld",&x,&y);
			printf("%lld\n",lsum(x,y));
		}else if(op[0]=='M'){
			scanf("%lld%lld",&x,&y);
			if(op[1]=='A')printf("%lld\n",lmax(x,y));
			else printf("%lld\n",lmin(x,y));
		}else if(op[0]=='C'){
			scanf("%lld%lld",&x,&y);
			if(dep[X[x]]>dep[Y[x]])update(1,1,n,dfn[X[x]],y);
			else update(1,1,n,dfn[Y[x]],y);
		}else if(op[0]=='N'){
			scanf("%lld%lld",&x,&y);
			dlian(x,y);
		}
	}
	return 0;
}
2022/3/20 20:37
加载中...