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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> #include<ctime> #include<iomanip> #include<queue> #include<stack> #include<map> #include<vector> #define gh() getchar() #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=gh(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=gh(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=gh(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<class T> inline bool checkMax(T &x,T &y) { return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T &y) { return x>y?x=y,1:0; } const int MAXN=2e5+10; int N,M,K[MAXN]; struct LCT { int chi[2],fa,siz,rev; }Tr[MAXN]; int Stk[MAXN]; #define ls(p) Tr[p].chi[0] #define rs(p) Tr[p].chi[1] inline void pushRev(int p) { std::swap(ls(p),rs(p)); Tr[p].rev^=1; } inline void pushUp(int p) { Tr[p].siz=Tr[ls(p)].siz+Tr[rs(p)].siz+1; } inline void pushDown(int p) { if(Tr[p].rev) { pushRev(ls(p)),pushRev(rs(p)); Tr[p].rev=0; } } inline bool isRoot(int p) { return ls(Tr[p].fa)!=p&&rs(Tr[p].fa)!=p; } inline void rotate(int x) { int y=Tr[x].fa,z=Tr[y].fa; int k=rs(y)==x; if(!isRoot(y)) Tr[z].chi[rs(z)==y]=x; Tr[x].fa=z; Tr[y].chi[k]=Tr[x].chi[k^1],Tr[Tr[x].chi[k^1]].fa=y; Tr[x].chi[k^1]=y,Tr[y].fa=x; pushUp(y),pushUp(x); } inline void Splay(int x) { int r=x,Top=0; Stk[++Top]=r; while(!isRoot(r)) Stk[++Top]=r=Tr[r].fa; while(Top) pushDown(Stk[Top--]); while(!isRoot(x)) { int y=Tr[x].fa,z=Tr[y].fa; if(!isRoot(y)) if((rs(y)==x)^(rs(z)==y)) rotate(x); else rotate(y); rotate(x); } } inline void access(int x) { int z=x; for(int y=0;x;y=x,x=Tr[x].fa) { Splay(x); rs(x)=y,pushUp(x); } Splay(z); } inline void makeRoot(int x) { access(x); pushRev(x); } inline int findRoot(int x) { access(x); while(ls(x)) pushDown(x),x=ls(x); Splay(x); return x; } inline void split(int x,int y) { makeRoot(x); access(y); } inline void link(int x,int y) { makeRoot(x); if(findRoot(y)!=x) Tr[x].fa=y; } inline void cut(int x,int y) { makeRoot(x); if(findRoot(y)==x&&Tr[y].fa==x&&!ls(y)) { rs(x)=Tr[y].fa=0; pushUp(x); } } int main() { read(N); for(int i=1;i<=N;++i) { read(K[i]); link(i,std::min(N+1,i+K[i])); } read(M); for(int opt,x,y;M--;) { read(opt,x);++x; if(opt==1) { split(x,N+1); write(Tr[N+1].siz-1),puts(""); } else { read(y); cut(x,std::min(N+1,x+K[x])); K[x]=y; link(x,std::min(N+1,x+K[x])); } } return 0; }
|