如果i能弹到j,则将i和j相连
i的弹力系数修改后,先断开原来的边,然后与新的边连接即可。
统计每一个子树的节点数。输出的时候-1;
2002: [Hnoi2010]Bounce 弹飞绵羊
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2567 Solved: 1382
[ Submit][ Status][ Discuss]
Description
某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。
Input
第一行包含一个整数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
Output
对于每个i=1的情况,你都要输出一个需要的步数,占一行。
Sample Input
4
1 2 1 1
3
1 1
2 1 1
1 1
1 2 1 1
3
1 1
2 1 1
1 1
Sample Output
2
3
3
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
using namespace std;
#define MAXN 220000
int sz,ch[MAXN][2],f[MAXN],rev[MAXN],w[MAXN];
void newnode()
{
int p=++sz;
w[p]=1;
rev[p]=ch[p][0]=ch[p][1]=f[p]=0;
}
bool isroot(int x)
{
return (!f[x]||ch[f[x]][0]!=x&&ch[f[x]][1]!=x);
}
void push_up(int rt)
{
if(!rt) return ;
int l=ch[rt][0];
int r=ch[rt][1];
w[rt]=1;
if(l) w[rt]+=w[l];
if(r) w[rt]+=w[r];
}
void push_down(int rt)
{
if(!rt) return;
if(rev[rt])
{
swap(ch[rt][0],ch[rt][1]);
rev[ch[rt][0]]^=1;
rev[ch[rt][1]]^=1;
rev[rt]=0;
}
}
void rotate(int x,int c)
{
if(isroot(x)) return;
int y=f[x],z=f[y];
ch[y][!c]=ch[x][c];
if(ch[x][c]) f[ch[x][c]]=y;
ch[x][c]=y;
if(f[y])
{
if(ch[f[y]][0]==y)
ch[f[y]][0]=x;
if(ch[f[y]][1]==y)
ch[f[y]][1]=x;
}
f[y]=x;f[x]=z;
push_up(y);
}
void splay(int x)
{
int y,z;
push_down(x);
while(!isroot(x))
{
y=f[x],z=f[y];
if(isroot(y))
{
push_down(y);push_down(x);
if(ch[y][0]==x)
rotate(x,1);
else
rotate(x,0);
}
else
{
push_down(z);push_down(y);push_down(x);
if(ch[z][0]==y)
{
if(ch[y][0]==x)
rotate(y,1),rotate(x,1);
else
rotate(x,0),rotate(x,1);
}
else
{
if(ch[y][1]==x)
rotate(y,0),rotate(x,0);
else
rotate(x,1),rotate(x,0);
}
}
}
push_up(x);
}
int Access(int u)
{
int v=0;
for(;u;u=f[u])
{
splay(u);
ch[u][1]=v;
push_up(v=u);
}
return v;
}
void makeRoot(int u)
{
int p=Access(u);
rev[p]^=1;
splay(u);
}
int findRoot(int x) {
for (x = Access(x); push_down(x),ch[x][0]!=0 ; x = ch[x][0]);
splay(x);
return x;
}
bool link(int u,int v)
{
if(findRoot(u)==findRoot(v)) return false;
makeRoot(u);
f[u]=v;
Access(u);
return true;
}
bool cut(int u,int v)
{
if(u==v||findRoot(u)!=findRoot(v)) return false;
makeRoot(u);
Access(v);
splay(v);
f[ch[v][0]]=0;
ch[v][0]=0;
push_up(v);
return true;
}
bool query(int u,int v,int &ans) {
if (findRoot(u)!=findRoot(v)) return false;
makeRoot(u);
Access(v);
splay(v);
ans=w[v];
return true;
}
int n,m;
int a[MAXN];
int main()
{
while(~scanf("%d",&n))
{
sz=0;
for(int i=1;i<=n+1;i++)
newnode();
for(int i=1;i<=n;i++)
{
int st,ed;
scanf("%d",&a[i]);
st=i;
ed=st+a[i];
ed=ed>n+1?n+1:ed;
link(st,ed);
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
int tp,u,v,w;
scanf("%d",&tp);
if(tp==1)
{
scanf("%d",&u);
query(u+1,n+1,w);
printf("%d\n",w-1);
}
else
{
int st,ed;
scanf("%d%d",&u,&w);
u++;
st=u;ed=u+a[u];
if(ed>n+1) ed=n+1;
cut(st,ed);
st=u,ed=u+w,a[u]=w;
if(ed>n+1) ed=n+1;
link(st,ed);
}
}
}
return 0;
}