1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include<iostream> #include<cstdio> using namespace std; long long read() { long long x=0,f=1; char c=getchar(); while(!isdigit(c)){if(c=='-') f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } const int N=200000+100; int n,m; struct LCT { int son[N][2],fa[N],lazy[N],mstack[N],top,size[N]; inline bool isRoot(int x) { return x!=son[fa[x]][0] && x!=son[fa[x]][1]; } inline void update(int x) { size[x]=size[son[x][0]]+size[son[x][1]]+1; } inline void mirror(int x) { lazy[x]=!lazy[x],swap(son[x][0],son[x][1]); } inline void pushDown(int x) { if(lazy[x]==0) return; lazy[x]=0; mirror(son[x][0]),mirror(son[x][1]); } inline void rotate(int x,int type) { int y=fa[x],z=fa[y]; if(isRoot(y)==false) son[z][y==son[z][1]]=x; fa[x]=z; son[y][!type]=son[x][type],fa[son[x][type]]=y; son[x][type]=y,fa[y]=x; update(y),update(x); } void splay(int x) { mstack[top=1]=x; for(int i=x;isRoot(i)==false;i=fa[i]) mstack[++top]=fa[i]; for(int i=top;i>=1;i--) pushDown(mstack[i]); while(isRoot(x)==false) { if(x==son[fa[x]][fa[x]==son[fa[fa[x]]][1]] and isRoot(fa[x])==false) rotate(fa[x],x==son[fa[x]][0]), rotate(x,x==son[fa[x]][0]); else rotate(x,x==son[fa[x]][0]); } } void Access(int x) { for(int t=0;x!=0;t=x,x=fa[x]) splay(x),son[x][1]=t,fa[t]=x,update(x); } inline void MakeRoot(int x) { Access(x),splay(x); mirror(x); } inline void Link(int x,int y) { MakeRoot(x); fa[x]=y; } inline void split(int x,int y) { MakeRoot(x); Access(y),splay(y); } inline void Cut(int x,int y) { split(x,y); son[y][0]=fa[x]=0; update(y); } int Query(int x) { MakeRoot(n+1); Access(x),splay(x); return size[x]-1; } }lct; int q[N]; int main() { n=read(); for(int i=1;i<=n;i++) q[i]=read();
for(int i=n;i>=1;i--) if(i+q[i]>n) lct.Link(i,n+1); else lct.Link(i,i+q[i]); m=read(); for(int i=1;i<=m;i++) { int op=read(); if(op==1) { int x=read()+1; printf("%d\n",lct.Query(x)); } else { int x=read()+1,K=read(); if(x+q[x]>n) lct.Cut(x,n+1); else lct.Cut(x,x+q[x]); q[x]=K; if(x+q[x]<=n) lct.Link(x,x+q[x]); else lct.Link(x,n+1); } } return 0; }
|