1. 程式人生 > >【BZOJ4817】[Sdoi2017]樹點塗色 LCT+線段樹

【BZOJ4817】[Sdoi2017]樹點塗色 LCT+線段樹

sum urn using sof 兩個 如果 std bsp char

【BZOJ4817】[Sdoi2017]樹點塗色

Description

Bob有一棵n個點的有根樹,其中1號點是根節點。Bob在每個點上塗了顏色,並且每個點上的顏色不同。定義一條路徑的權值是:這條路徑上的點(包括起點和終點)共有多少種不同的顏色。Bob可能會進行這幾種操作: 1 x: 把點x到根節點的路徑上所有的點染上一種沒有用過的新顏色。 2 x y: 求x到y的路徑的權值。 3 x y: 在以x為根的子樹中選擇一個點,使得這個點到根節點的路徑權值最大,求最大權值。 Bob一共會進行m次操作

Input

第一行兩個數n,m。 接下來n-1行,每行兩個數a,b,表示a與b之間有一條邊。 接下來m行,表示操作,格式見題目描述 1<=n,m<=100000

Output

每當出現2,3操作,輸出一行。 如果是2操作,輸出一個數表示路徑的權值 如果是3操作,輸出一個數表示權值的最大值

Sample Input

5 6
1 2
2 3
3 4
3 5
2 4 5
3 3
1 4
2 4 5
1 5
2 4 5

Sample Output

3
4
2
2

題解:做過重組病毒那題再做這題就水了。

發現1操作可以看成LCT的access操作,而一個點到根路徑的權值就是該點在LCT中到根路徑上的虛邊數量。所以我們用LCT模擬這個過程,在access的時候順便改一下子樹內的虛邊數量即可。可以用線段樹實現。

那麽已知了一個點到根路徑上的權值,如何求一條路徑的權值呢?如果a,b的lca是c,那麽自己畫畫就知道,答案是:a路徑的權值+b路徑的權值-2*c路徑的權值+1。

#include <cstdio>
#include <cstring>
#include <iostream>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=100010;

struct LCT
{
	int ch[2],fa,L;
}s[maxn];
int n,m,cnt;
int fa[19][maxn],to[maxn<<1],next[maxn<<1],head[maxn],Log[maxn],dep[maxn],p[maxn],q[maxn],Q[maxn],sm[maxn<<2],ts[maxn<<2];
inline void add(int a,int b)
{
	to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
void dfs(int x)
{
	p[x]=++q[0],Q[q[0]]=x;
	for(int i=head[x];i!=-1;i=next[i])	if(to[i]!=fa[0][x])	fa[0][to[i]]=x,dep[to[i]]=dep[x]+1,dfs(to[i]);
	q[x]=q[0];
}
inline int lca(int a,int b)
{
	if(dep[a]<dep[b])	swap(a,b);
	for(int i=Log[dep[a]-dep[b]];i>=0;i--)	if(dep[fa[i][a]]>=dep[b])	a=fa[i][a];
	if(a==b)	return a;
	for(int i=Log[dep[a]];i>=0;i--)	if(fa[i][a]!=fa[i][b])	a=fa[i][a],b=fa[i][b];
	return fa[0][a];
}
inline bool isr(int x)	{return s[s[x].fa].ch[0]!=x&&s[s[x].fa].ch[1]!=x;}
inline void pushup(int x)
{
	if(s[x].ch[0])	s[x].L=s[s[x].ch[0]].L;
	else	s[x].L=x;
}
inline void rotate(int x)
{
	int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
	if(!isr(y))	s[z].ch[y==s[z].ch[1]]=x;
	s[x].fa=z,s[y].fa=x,s[y].ch[d]=s[x].ch[d^1];
	if(s[x].ch[d^1])	s[s[x].ch[d^1]].fa=y;
	s[x].ch[d^1]=y;
	pushup(y),pushup(x);
}
inline void splay(int x)
{
	while(!isr(x))
	{
		int y=s[x].fa,z=s[y].fa;
		if(!isr(y))
		{
			if((x==s[y].ch[0])^(y==s[z].ch[0]))	rotate(x);
			else	rotate(y);
		}
		rotate(x);
	}
}
inline void pushdown(int x)
{
	if(ts[x])	sm[lson]+=ts[x],ts[lson]+=ts[x],sm[rson]+=ts[x],ts[rson]+=ts[x],ts[x]=0;
}
void build(int l,int r,int x)
{
	if(l==r)
	{
		sm[x]=dep[Q[l]];
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,lson),build(mid+1,r,rson);
	sm[x]=max(sm[lson],sm[rson]);
}
void updata(int l,int r,int x,int a,int b,int val)
{
	if(a<=l&&r<=b)
	{
		sm[x]+=val,ts[x]+=val;
		return ;
	}
	pushdown(x);
	int mid=(l+r)>>1;
	if(a<=mid)	updata(l,mid,lson,a,b,val);
	if(b>mid)	updata(mid+1,r,rson,a,b,val);
	sm[x]=max(sm[lson],sm[rson]);
}
int query(int l,int r,int x,int a,int b)
{
	if(a<=l&&r<=b)	return sm[x];
	pushdown(x);
	int mid=(l+r)>>1;
	if(b<=mid)	return query(l,mid,lson,a,b);
	if(a>mid)	return query(mid+1,r,rson,a,b);
	return max(query(l,mid,lson,a,b),query(mid+1,r,rson,a,b));
}
inline void sumup(int x,int val)	{if(x)	updata(1,n,1,p[x],q[x],val);}
inline void access(int x)
{
	for(int y=0;x;)
	{
		splay(x);
		sumup(s[s[x].ch[1]].L,1),sumup(s[y].L,-1),s[x].ch[1]=y,pushup(x),y=x,x=s[x].fa;
	}
}
inline int rd()
{
	int ret=0,f=1;	char gc=getchar();
	while(gc<‘0‘||gc>‘9‘)	{if(gc==‘-‘)	f=-f;	gc=getchar();}
	while(gc>=‘0‘&&gc<=‘9‘)	ret=ret*10+gc-‘0‘,gc=getchar();
	return ret*f;
}
int main()
{
	n=rd(),m=rd();
	int i,j,a,b,c,op;
	memset(head,-1,sizeof(head));
	for(i=1;i<n;i++)	a=rd(),b=rd(),add(a,b),add(b,a);
	dep[1]=1,dfs(1);
	for(i=2;i<=n;i++)	Log[i]=Log[i>>1]+1;
	for(j=1;(1<<j)<=n;j++)	for(i=1;i<=n;i++)	fa[j][i]=fa[j-1][fa[j-1][i]];
	for(i=1;i<=n;i++)	s[i].fa=fa[0][i],s[i].L=i;
	build(1,n,1);
	for(i=1;i<=m;i++)
	{
		op=rd(),a=rd();
		if(op==1)	access(a),splay(a);
		if(op==2)
		{
			b=rd(),c=lca(a,b);
			printf("%d\n",query(1,n,1,p[a],p[a])+query(1,n,1,p[b],p[b])-2*query(1,n,1,p[c],p[c])+1);
		}
		if(op==3)	printf("%d\n",query(1,n,1,p[a],q[a]));
	}
	return 0;
}//5 3 1 2 2 3 3 4 3 5 1 4 1 5 2 4 5

【BZOJ4817】[Sdoi2017]樹點塗色 LCT+線段樹