luogu P3313 [SDOI2014]旅行

题面传送门
显然是树剖题目,关键是怎么维护不同宗教。
对于每个宗教可以开一棵线段树,但是空间会爆。
所以可以动态开点。
代码实现:

#include<cstdio>
#include<cstring>
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,tot,root[100039],top[100039],d[100039],fa[100039],idea,id[100039],siz[100039],son[100039],w[100039],c[100039];
char _s;
struct yyy{int to,z;};
struct ljb{
	int head,h[100039];
	yyy f[200039];
	inline void add(int x,int y){
		f[++head]=(yyy){y,h[x]};
		h[x]=head;
	}
}s;
struct tree{int l,r,f,sum;}f[2000039];
inline void get(int x,int z,int l,int r,int now){
	if(l==r){f[now].sum=z;f[now].f=z;return;}
	int m=(l+r)>>1;
	if(x<=m){
		if(!f[now].l) f[now].l=++tot;
		get(x,z,l,m,f[now].l);
	}
	else{
		if(!f[now].r) f[now].r=++tot;
		get(x,z,m+1,r,f[now].r);
	}
	f[now].f=max(f[f[now].l].f,f[f[now].r].f);
	f[now].sum=f[f[now].l].sum+f[f[now].r].sum;
}
inline int find1(int x,int y,int l,int r,int now){
	if(x<=l&&r<=y) return f[now].f;
	int m=(l+r)>>1,ans1=0,ans2=0;
	if(x<=m&&f[now].l)ans1=find1(x,y,l,m,f[now].l);
	if(y>m&&f[now].r) ans2=find1(x,y,m+1,r,f[now].r);
	return max(ans1,ans2);
}
inline int find2(int x,int y,int l,int r,int now){
	if(x<=l&&r<=y)return f[now].sum;
	int m=(l+r)>>1,fs=0;
	if(x<=m&&f[now].l) fs+=find2(x,y,l,m,f[now].l);
	if(y>m&&f[now].r) fs+=find2(x,y,m+1,r,f[now].r);
	return fs;
}
inline void dfs1(int x,int last){
	fa[x]=last;
	d[x]=d[last]+1;
	siz[x]=1;
	int cur=s.h[x],pus=-1;
	yyy tmp;
	while(cur!=-1){
		tmp=s.f[cur];
		if(tmp.to!=last){
			dfs1(tmp.to,x);
			siz[x]+=siz[tmp.to];
			if(siz[tmp.to]>pus) pus=siz[tmp.to],son[x]=tmp.to;
		}
		cur=tmp.z;
	}
}
inline void dfs2(int x,int last){
	top[x]=last;
	id[x]=++idea;
	if(!root[c[x]]) root[c[x]]=++tot;
	get(id[x],w[x],1,n,root[c[x]]);
	if(!son[x]) return;
	dfs2(son[x],last);
	int cur=s.h[x];
	yyy tmp;
	while(cur!=-1){
		tmp=s.f[cur];
		if(tmp.to!=fa[x]&&tmp.to!=son[x]) dfs2(tmp.to,tmp.to);
		cur=tmp.z;
	}
}
inline void get1(int x,int y){
	get(id[x],0,1,n,root[c[x]]);
	c[x]=y;
	if(!root[y]) root[y]=++tot;
	get(id[x],w[x],1,n,root[c[x]]);
}
inline void get2(int x,int y){w[x]=y;get(id[x],y,1,n,root[c[x]]);}
inline void swap(int &x,int &y){x^=y,y^=x,x^=y;}
inline int finds1(int x,int y,int z){
	int ans=0;
	while(top[x]!=top[y]){
		if(d[top[x]]<d[top[y]]) swap(x,y);
		ans+=find2(id[top[x]],id[x],1,n,root[z]);
		x=fa[top[x]];
	}
	if(d[x]>d[y]) swap(x,y);
	return ans+find2(id[x],id[y],1,n,root[z]);
}
inline int finds2(int x,int y,int z){
	int ans=0,tot=0;
	while(top[x]!=top[y]){
		if(d[top[x]]<d[top[y]]) swap(x,y);
		tot=find1(id[top[x]],id[x],1,n,root[z]);
		ans=max(ans,tot);
		x=fa[top[x]];
	}
	if(d[x]>d[y]) swap(x,y);
	tot=find1(id[x],id[y],1,n,root[z]);
	return max(ans,tot);
}
int main(){
	memset(s.h,-1,sizeof(s.h));
	register int i;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++) scanf("%d%d",&w[i],&c[i]);
	for(i=1;i<n;i++){
		scanf("%d%d",&x,&y);
		s.add(x,y);s.add(y,x);
	}
	dfs1(1,0);
	dfs2(1,1);
	for(i=1;i<=m;i++){
		_s=getchar();
		while(_s<'A'||_s>'Z') _s=getchar();
		if(_s=='C'){
			_s=getchar();
			if(_s=='C')scanf("%d%d",&x,&y),get1(x,y);
			else scanf("%d%d",&x,&y),get2(x,y);
		}
		else{
			_s=getchar();
			if(_s=='S') scanf("%d%d",&x,&y),printf("%d\n",finds1(x,y,c[x]));
			else scanf("%d%d",&x,&y),printf("%d\n",finds2(x,y,c[x]));
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值