省选专练[HNOI2010]BOUNCE 弹飞绵羊

#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<algorithm>
const int N=1e6+7;
using namespace std;
int ch[N][2]={0};
int fa[N]={0};
int rev[N]={0};//lazy
int val[N]={0};
int siz[N]={0};
int n,m;
int ed=300000;
inline int getson(int x){
	if(ch[fa[x]][1]==x)
		return 1;
	if(ch[fa[x]][0]==x)
		return 0;
	return -1;
}
inline void update(int x){
	siz[x]=1;
	if(x){
		if(ch[x][0])
			siz[x]+=siz[ch[x][0]];
		if(ch[x][1])
			siz[x]+=siz[ch[x][1]];
	}
}
inline void pushdown(int x){
	if(!rev[x])
		return;
	rev[x]=0;
	rev[ch[x][0]]^=1;
	rev[ch[x][1]]^=1;
	swap(ch[x][0],ch[x][1]);
}
inline void Clear(int x){
	if(getson(x)!=-1)
		Clear(fa[x]);
	pushdown(x);
}
inline void rotate(int x){
	int f=fa[x];
	int grandf=fa[f];
	int l=getson(x);
	int r=l^1;
	if(getson(f)!=-1){
		ch[grandf][ch[grandf][1]==f]=x;
	}
	fa[ch[x][r]]=f;
	fa[f]=x;
	fa[x]=grandf;
	ch[f][l]=ch[x][r];
	ch[x][r]=f;
	update(f);
	update(x);
}
inline void Splay(int x){
	Clear(x);
	for(int f=fa[x];getson(x)!=-1;rotate(x),f=fa[x]){
		if(getson(f)!=-1)
			rotate(getson(f)==getson(x)?f:x);
	}
	update(x);
}
inline void access(int x){
	for(int i=0;x;x=fa[i=x]){
		Splay(x);
		ch[x][1]=i;
		update(x);
	}
}
inline void makeroot(int x){
	access(x);
	Splay(x);
	rev[x]^=1;
}
inline void link(int x,int y){
	makeroot(x);
	fa[x]=y;
}
inline void cut(int x,int y){
	makeroot(x);
	access(y);
	Splay(y);
	fa[x]=ch[y][0]=0;
	update(y);
}
inline int getans(int x){
	makeroot(x);
	access(ed);
	Splay(ed);
	return siz[ed];
}
int main(){
	scanf("%d",&n);
	ed=n+1;
	for(int i=1;i<=ed;i++)
		siz[i]=1;
	for(int i=1;i<=n;i++){
		int u,v;
		u=i;
		scanf("%d",&v);
		if(u+v>n)
			link(u,ed);
		else
			link(u,u+v);
		val[i]=v;
	}
	int m;
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		int opt;
		int u,k;
		scanf("%d",&opt);
		if(opt==1){
			scanf("%d",&u);
			u++;
			printf("%d\n",getans(u)-1);
		}
		if(opt==2){
			scanf("%d%d",&u,&k);
			u++;
			if(u+val[u]<ed)
				cut(u,u+val[u]);
			else
				cut(u,ed);
			if(u+k<ed)
				link(u,u+k);
			else
				link(u,ed);
			val[u]=k;
		}
	}
}
LCT模板,挺简单的


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值