【BZOJ】【P2002】【HNOI2010】【弹飞绵羊】【题解】【LCT】

bzoj挂了好几天了……

LCT模板题,留着自己看……

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=200100;
int n,m;
struct node{
	node *c[2],*p;
	int sz,rev;
	void pushdown(){
		if(!rev)return ;
		c[0]->rev^=1;
		c[1]->rev^=1;
		swap(c[0],c[1]);
		rev^=1;
	}
	void rz(){
		sz=c[0]->sz+c[1]->sz+1;
	}
	bool d(){
		return this==p->c[1];
	}
	bool root(){
		return this!=p->c[1]&&this!=p->c[0];
	}
	void sets(node *x,int d){
		pushdown();
		(c[d]=x)->p=this;rz();
	}
}nil[maxn]; 
void rot(node *x){
	node *p=x->p;
	if(!p->root())p->p->pushdown();
	p->pushdown();x->pushdown();
	int d=x->d();
	p->sets(x->c[!d],d);
	if(p->root())
		x->p=p->p;
	else
		p->p->sets(x,p->d());
	x->sets(p,!d);
}
void splay(node *x){
	while(!x->root())
		if(x->p->root())	
			rot(x);
		else if(x->p->d()==x->d())
			rot(x->p),rot(x);
		else 
			rot(x),rot(x);
}
node* access(node *x){
	node *y=nil;
	for(;x!=nil;y=x,x=x->p){
		splay(x);
		x->sets(y,1);
	}return y;
}
void makert(node *x){
	access(x)->rev^=1;
	splay(x);
}
node* findrt(node *x){
	for(x=access(x);x->c[0]!=nil;x=x->c[0]);
		return x;
} 
void link(node *x,node *y){
	makert(x);
	x->p=y;
	access(x);
}
void cut(node *x,node *y){
	makert(x);
	access(y);
	splay(y);
	y->c[0]=x->p=nil;
	y->rz();
}
int Qsize(node *x){
	access(x);
	splay(x);
	return x->c[0]->sz;
}
void Change(int i,int k){
	node *x=&nil[i+1];
	splay(x);
	x->c[0]->p=x->p;
	x->c[0]=nil;
	//x->c[0]=x->c[0]->p=nil;
	x->p=&nil[min(i+1+k,n+1)];
}
int main(){
	scanf("%d",&n);
	node *x;int k;
	for(int i=1;i<=n;i++){
		scanf("%d",&k);
		x=&nil[i];
		x->c[0]=x->c[1]=nil;
		x->p=&nil[min(i+k,n+1)];
	}
	x=&nil[n+1];
	x->c[0]=x->c[1]=x->p=nil->c[0]=nil->c[1]=nil->p=nil;
	scanf("%d",&m);
	while(m--){
		int opt;scanf("%d",&opt);
		int i,k;
		if(opt==1){
			scanf("%d",&i);
			printf("%d\n",Qsize(&nil[i+1]));
		}else{
			scanf("%d%d",&i,&k);
			Change(i,k);
		}
	}
	return 0;
}


你可能感兴趣的:(bzoj,省选)