题目描述
某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。
输入输出格式
输入格式:
第一行包含一个整数n,表示地上有n个装置,装置的编号从0到n-1,接下来一行有n个正整数,依次为那n个装置的初始弹力系数。第三行有一个正整数m,接下来m行每行至少有两个数i、j,若i=1,你要输出从j出发被弹几次后被弹飞,若i=2则还会再输入一个正整数k,表示第j个弹力装置的系数被修改成k。对于20%的数据n,m<=10000,对于100%的数据n<=200000,m<=100000
输出格式:
对于每个i=1的情况,你都要输出一个需要的步数,占一行。
输入输出样例
输入样例#1:
4 1 2 1 1 3 1 1 2 1 1 1 1
输出样例#1:
2 3
【分析】
LCT裸题...233
【代码】
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=200005;
int n,m,top;
int ch[mxn][2],f[mxn],st[mxn],rev[mxn],size[mxn],pre[mxn];
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
inline bool isroot(int x)
{
return (ch[f[x]][0]!=x && ch[f[x]][1]!=x);
}
inline int get(int x)
{
if(ch[f[x]][0]==x) return 0;return 1;
}
inline void update(int x)
{
size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
}
inline void pushdown(int x)
{
if(rev[x])
{
rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
rev[x]=0;
}
}
inline void rotate(int x)
{
pushdown(x);
int fa=f[x],fafa=f[fa],which=get(x);
if(!isroot(fa)) ch[fafa][ch[fafa][1]==fa]=x;
ch[fa][which]=ch[x][which^1],f[ch[fa][which]]=fa;
ch[x][which^1]=fa,f[fa]=x;
f[x]=fafa;
update(fa),update(x);
}
inline void splay(int x)
{
st[top=1]=x;
for(int i=x;!isroot(i);i=f[i]) st[++top]=f[i];
for(int i=top;i;i--) pushdown(st[i]);
for(;!isroot(x);rotate(x))
if(!isroot(f[x])) rotate(get(x)==get(f[x])?f[x]:x);
}
inline void access(int x)
{
for(int y=0;x;y=x,x=f[x])
splay(x),ch[x][1]=y;
}
inline void makeroot(int x)
{
access(x),splay(x),rev[x]^=1;
}
inline int findroot(int x)
{
access(x),splay(x);
while(ch[x][0]) x=ch[x][0];
return x;
}
inline void link(int x,int y)
{
makeroot(x);
f[x]=y,splay(x);
}
inline void cut(int x,int y)
{
makeroot(x);
access(y),splay(y);
ch[y][0]=f[x]=0;
}
int main()
{
int i,j,k,opt,x,y;
n=read();
fo(i,1,n)
{
k=read();
size[i]=1;
pre[i]=f[i]=min(n+1,i+k);
}
m=read();
while(m--)
{
opt=read();
if(opt==1)
{
x=read()+1;
makeroot(n+1),access(x),splay(x);
printf("%d\n",size[ch[x][0]]);
}
else
{
x=read()+1,y=read();
cut(x,pre[x]),pre[x]=min(n+1,x+y),link(x,pre[x]);
}
}
return 0;
}