LCT
新增一个点n+1表示弹飞的点。
刚开始把i和i+ki接起来,查询时直接把x到n+1的路径拎出来,答案就是节点个数-1。改值时把x和x+ki断开,再把x和x+y接起来。注意对n+1取min。
代码:
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 200005
#define il inline
#define iv il void
using namespace std;
struct node{
int sz,to[2],fa,f;
}t[N];
int n,m,a[N],stk[N],tp;
il char readc(){
static char buf[100000],*l=buf,*r=buf;
if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
if (l==r) return EOF; return *l++;
}
il int _read(){
int x=0; char ch=readc();
while (!isdigit(ch)) ch=readc();
while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
return x;
}
iv writec(int x){ if (x>9) writec(x/10); putchar(x%10+48); }
iv _write(int x){ writec(x),putchar('\n'); }
iv pshp(int x){ t[x].sz=t[t[x].to[0]].sz+t[t[x].to[1]].sz+1; }
iv pshd(int x){
if (t[x].f){
int &l=t[x].to[0],&r=t[x].to[1];
t[l].f^=1,t[r].f^=1,t[x].f=0,swap(l,r);
}
}
il bool rt(int x){ return (t[t[x].fa].to[0]!=x&&t[t[x].fa].to[1]!=x); }
iv rtt(int x){
int y=t[x].fa,z=t[y].fa,l=(t[y].to[1]!=x);
if (!rt(y)) t[z].to[t[z].to[1]==y]=x;
t[x].fa=z,t[y].fa=x,t[t[x].to[l]].fa=y;
t[y].to[l^1]=t[x].to[l],t[x].to[l]=y;
pshp(y),pshp(x);
}
iv splay(int x){
stk[tp=1]=x;
for (int i=x;!rt(i);i=t[i].fa) stk[++tp]=t[i].fa;
while (tp) pshd(stk[tp--]);
while (!rt(x)){
int y=t[x].fa,z=t[y].fa;
if (!rt(y)) if (t[z].to[0]==y^t[y].to[0]==x) rtt(x); else rtt(y);
rtt(x);
}
}
iv ccss(int x){ for (int i=0;x;i=x,x=t[x].fa) splay(x),t[x].to[1]=i,pshp(x); }
iv mkrt(int x){ ccss(x),splay(x),t[x].f^=1; }
iv lnk(int x,int y){ mkrt(x),t[x].fa=y; }
iv sprt(int x,int y){ mkrt(x),ccss(y),splay(y); }
iv cut(int x,int y){ sprt(x,y),t[y].to[0]=t[x].fa=0,pshp(y); }
int main(){
n=_read();
for (int i=1;i<=n;i++){ a[i]=_read(),lnk(i,min(n+1,i+a[i])); }
m=_read();
while (m--){
int flag=_read(),x=_read()+1,y;
if (flag==1) sprt(n+1,x),_write(t[x].sz-1);
else y=_read(),cut(x,min(x+a[x],n+1)),lnk(x,min(n+1,x+(a[x]=y)));
}
return 0;
}