→题目链接←
新博客链接:https://www.everlasting.wang/archives/180
【想说话的】
!解锁成就:学会LCT
【题解】
啊啊啊啊,不想写教程
所以.....
LCT
【代码】
#include<bits/stdc++.h>
#define MAXN 200020
using namespace std;
inline int rd(){
int x=0,y=1;char c=getchar();
while(c<'0' || c>'9'){if(c=='-')y=-y;c=getchar();}
while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
return x*y;
}
int n,m;
int son[MAXN][2],fa[MAXN],sz[MAXN],stk[MAXN],nxt[MAXN];
bool rev[MAXN];
inline bool isroot(int x){
return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}
inline bool is_right(int x){
return son[fa[x]][1]==x;
}
inline void pushup(int x){
sz[x]=sz[son[x][0]]+sz[son[x][1]]+1;
}
inline void pushdown(int x){
if(rev[x]){
rev[x]^=1,rev[son[x][0]]^=1,rev[son[x][1]]^=1;
swap(son[x][0],son[x][1]);
}
}
inline void Rotate(int x){
int f=fa[x],ff=fa[f],which=is_right(x);
if(!isroot(f))son[ff][son[ff][1]==f]=x;
son[f][which]=son[x][which^1];
fa[son[f][which]]=f;fa[f]=x;
son[x][which^1]=f;fa[x]=ff;
pushup(f);pushup(x);
}
inline void splay(int x){
int tot=0;
stk[++tot]=x;
for(int i=x; !isroot(i); i=fa[i])stk[++tot]=fa[i];
for(int i=tot; i>0; i--)pushdown(stk[i]);
while(!isroot(x)){
int f=fa[x];
if(!isroot(f))Rotate((is_right(x)==is_right(f)?f:x));
Rotate(x);
}
}
inline void access(int x){
int t=0;
while(x){
splay(x);
son[x][1]=t;
t=x;x=fa[x];
}
}
inline void rever(int x){
access(x);splay(x);
rev[x]^=1;
}
inline void link(int x,int y){
rever(x);
fa[x]=y;
splay(x);
}
inline void cut(int x,int y){
rever(x);access(y);splay(y);
son[y][0]=fa[x]=0;
}
int main(){
n=rd();
for(int i=1; i<=n; i++){
int x=rd();
fa[i]=x+i,sz[i]=1;
if(fa[i]>n+1)fa[i]=n+1;
nxt[i]=fa[i];
}
sz[n+1]=1;
m=rd();
while(m--){
int op=rd();
if(op==1){
rever(n+1);
int x=rd()+1;
access(x);splay(x);
printf("%d\n",sz[son[x][0]]);
}
else{
int x=rd()+1,y=rd();
int t=min(n+1,x+y);
cut(x,nxt[x]);link(x,t);
nxt[x]=t;
}
}
return 0;
}