题解:
lct模板
没看过splay和树链剖分的去看看吧
至于lct可以看看别的文章
代码:
#include<bits/stdc++.h> using namespace std; const int N=400005; int c[N][2],size[N],n,m,op,rev[N],x,t,next[N],fa[N]; int isroot(int x){return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;} void pushup(int x){size[x]=size[c[x][0]]+size[c[x][1]]+1;} void pushdown(int x) { if (rev[x]) { rev[x]^=1;rev[c[x][0]]^=1;rev[c[x][1]]^=1; swap(c[x][0],c[x][1]); } } void down(int x) { if (!isroot(x))down(fa[x]); pushdown(x); } void rotate(int x) { int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1; if(!isroot(y))c[z][c[z][1]==y] = x; fa[x]=z;fa[y]=x;fa[c[x][r]]=y; c[y][l]=c[x][r];c[x][r]=y; pushup(y);pushup(x); } void splay(int x) { down(x); for (int y=fa[x];!isroot(x);rotate(x),y=fa[x]) if (!isroot(y))rotate((c[y][0]==x)==(c[fa[y]][0]==y)?y:x); } void access(int x) { int t=0; while(x) { splay(x); c[x][1]=t; t=x;x=fa[x]; } } void rever(int x){access(x);splay(x);rev[x]^=1;} void cut(int x,int y){rever(x);access(y);splay(y);c[y][0]=fa[x]=0;} void link(int x,int y){rever(x);fa[x]=y;c[x][1]=y;splay(x);} int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d",&x);fa[i]=i+x; if (fa[i]>n+1)fa[i]=n+1; next[i]=fa[i];size[i]=1; } scanf("%d",&m); size[n+1]=1; while (m--) { scanf("%d",&op); if (op==1) { rever(n+1); scanf("%d",&x);x++; access(x);splay(x); printf("%d\n",size[c[x][0]]); } else { scanf("%d%d",&x,&t);x++; int q=min(x+t,n+1); cut(x,next[x]);link(x,q);next[x]=q; } } return 0; }