Link-Cut Tree指针模板

时间:2023-03-08 23:36:06
Link-Cut Tree指针模板

模板:

  以下为弹飞绵羊代码:

 #define Troy

 #include "bits/stdc++.h"

 using namespace std;

 const int N=2e5+;

 inline int read(){
int s=,k=;char ch=getchar();
while(ch<''|ch>'') ch=='-'?k=-:,ch=getchar();
while(ch>&ch<='') s=s*+(ch^),ch=getchar();
return s*k;
} #define size(t) (t?t->size:0)
#define rev(t) (t?t->rev^=1:0) struct Node{
int size,rev;
Node *fa,*son[];
Node(){fa=son[]=son[]=NULL;size=,rev=;}
inline void update(){
size=+size(son[])+size(son[]);
}
inline void pushdown(){
if(!rev) return;
rev=,swap(son[],son[]);
rev(son[]),rev(son[]);
}
}*pool[N],*tmp[N],tree[N];int top; inline void init_pool(){for(;top<N;++top) pool[top]=tree+top;} inline void newNode(Node *&p,Node *f){p=pool[--top];*p=Node();p->fa=f;}
inline void freeNode(Node *&p){pool[top++]=p,p=NULL;} int to[N],n;
class LinkCutTree{
public:
Node *node[N];
inline void init(int n){for(register int i=;i<=n;++i)newNode(node[i],NULL);} #define son(p) (p->fa->son[1]==p)
#define is_root(p) ((!p->fa)||(p->fa->son[0]!=p&&p->fa->son[1]!=p)) inline void rotate(Node *p){
int a=son(p)^;Node *f=p->fa;
f->son[a^]=p->son[a];
if(p->son[a]) p->son[a]->fa=f;
p->fa=f->fa;
if(!is_root(f)) p->fa->son[son(f)]=p;
f->fa=p,p->son[a]=f,f->update(),p->update();
} inline void splay(Node *p){
register int pos=;
for(Node *t=p;;t=t->fa){
tmp[++pos]=t;
if(is_root(t)) break;
}
for(;pos;--pos) tmp[pos]->pushdown();
for(;!is_root(p);rotate(p))
if(!is_root(p->fa)) rotate(son(p)==son(p->fa)?p->fa:p);
} inline void access(Node *p){
for(Node *pre=NULL;p;pre=p,p=p->fa)
splay(p),p->son[]=pre,p->update();
} inline void make_root(Node *p){
access(p),splay(p),rev(p);
} inline void cut(Node *x,Node *y){
make_root(x),access(y),splay(y);
x->fa=y->son[]=NULL;y->update();
} inline void link(Node *x,Node *y){make_root(x);x->fa=y;}
inline void link(int x,int y){node[x]->fa=node[y];} inline void op1(int x){
make_root(node[n+]),access(node[x]),splay(node[x]);
printf("%d\n",node[x]->size-);
} inline void op2(int x,int y){
cut(node[x],node[min(n+,x+to[x])]);
link(node[x],node[min(n+,y+x)]);to[x]=y;
}
}lct; int main(){
n=read();
init_pool();
lct.init(n+);
register int i;
for(i=;i<=n;++i){
to[i]=read();
lct.link(i,min(i+to[i],n+));
}
int q=read(),x,y;
while(q--){
if(read()==)
lct.op1(read()+);
else{
x=read(),y=read();
lct.op2(x+,y);
}
}return ;
}