bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊

板子题
link-cut tree

// It is made by XZZ
#include<cstdio>
#include<algorithm>
#define il inline
#define rg register
#define vd void
#define sta static
typedef long long ll;
il int gi(){
	rg int x=0,f=1;rg char ch=getchar();
	while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	return x*f;
}
const int maxn=200001;
int t[maxn],n,fly;
namespace LCT{
	int ch[maxn][2],fa[maxn],siz[maxn];bool rev[maxn];
	il vd upd(int x){siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;}
	il bool isrt(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
	il vd down(int x){if(rev[x])rev[x]=0,rev[ch[x][0]]^=1,rev[ch[x][1]]^=1,std::swap(ch[x][0],ch[x][1]);}
	il vd rotate(int x){
		sta int y,z,o;y=fa[x],z=fa[y],o=x==ch[y][1];
		if(!isrt(y))ch[z][y==ch[z][1]]=x;fa[x]=z;
		ch[y][o]=ch[x][!o],fa[ch[x][!o]]=y;
		ch[x][!o]=y,fa[y]=x;
		upd(y);
	}
	il vd splay(int x){
		sta int stk[maxn],top;stk[top=1]=x;
		for(rg int i=x;!isrt(i);i=fa[i])stk[++top]=fa[i];
		while(top)down(stk[top--]);
		for(rg int y=fa[x],z=fa[y];!isrt(x);rotate(x),y=fa[x],z=fa[y])
			if(!isrt(y))rotate(((x==ch[y][0])^(y==ch[z][0]))?y:x);
		upd(x);
	}
	il vd access(int x){for(rg int y=0;x;x=fa[y=x])splay(x),ch[x][1]=y,upd(x);}
	il vd makert(int x){access(x),splay(x),rev[x]^=1;}
	il vd link(int x,int y){makert(x),fa[x]=y;}
	il vd cut(int x,int y){makert(x),access(y),splay(y);fa[x]=ch[y][0]=0;}
	il int query(int x){makert(fly),access(x),splay(x);return siz[x]-1;}
}
int main(){
	n=gi(),fly=n+1;
	using namespace LCT;
	for(rg int i=1;i<=n;++i)link(i,t[i]=std::min(i+gi(),fly));
	int m=gi(),u;
	makert(fly);
	while(m--)
		if(gi()==1)printf("%d\n",query(gi()+1));
		else u=gi()+1,cut(t[u],u),t[u]=std::min(u+gi(),fly),link(u,t[u]);
	return 0;
}
posted @ 2017-12-19 13:29  菜狗xzz  阅读(94)  评论(1编辑  收藏