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<=100000Output
对于每个i=1的情况,你都要输出一个需要的步数,占一行。Sample Input
4
1 2 1 1
3
1 1
2 1 1
1 1Sample Output
2
3
新建一个n+1的节点表示已经弹飞,然后连边(i,i+ki),那么就是一棵树的模型了
询问的就是n+1到x路径上的节点数-1
修改就是删边加边..
那么用LCT维护一下就好啦..
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int Maxn = 200010;
int size[Maxn], fa[Maxn], rev[Maxn], c[Maxn][2];
int n, m, a[Maxn];
int st[Maxn], tp;
bool is_root ( int x ){ return c[fa[x]][0] != x && c[fa[x]][1] != x; }
void update ( int x ){
int lc = c[x][0], rc = c[x][1];
size[x] = size[lc]+size[rc]+1;
}
void push_down ( int x ){
if ( rev[x] ){
rev[x] = 0;
int lc = c[x][0], rc = c[x][1];
swap ( c[x][0], c[x][1] );
rev[lc] ^= 1; rev[rc]^= 1;
}
}
void prep ( int x ){
tp = 0; int i;
for ( i = x; !is_root (i); i = fa[i] ) st[++tp] = i;
st[++tp] = i;
for ( i = tp; i >= 1; i -- ) push_down (st[i]);
}
void rotate ( int x ){
int y = fa[x], z = fa[y], l, r;
if ( c[y][0] == x ) l = 0; else l = 1; r = l^1;
if ( !is_root (y) ){ if ( c[z][0] == y ) c[z][0] = x; else c[z][1] = x; }
fa[x] = z; fa[y] = x; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = y;
update (y);
}
void splay ( int x ){
prep (x);
while ( !is_root (x) ){
int y = fa[x], z = fa[y];
if ( !is_root (y) ){
if ( (c[y][0]==x)^(c[z][0]==y) ) rotate (x);
else rotate (y);
}
rotate (x);
}
update (x);
}
void access ( int x ){
int t = 0;
while (x){
splay (x);
c[x][1] = t;
update (x);
t = x;
x = fa[x];
}
}
void make_root ( int x ){ access (x); splay (x); rev[x] ^= 1; }
int get_ans ( int x, int y ){
make_root (x);
access (y); splay (y);
return size[y];
}
void link ( int x, int y ){ make_root (x); fa[x] = y; }
void cut ( int x, int y ){ make_root (x); access (y); splay (y); fa[x] = 0; c[y][0] = 0; }
int main (){
int i, j, k;
scanf ( "%d", &n );
for ( i = 1; i <= n; i ++ ){
scanf ( "%d", &a[i] );
int y = i+a[i];
if ( y > n ) y = n+1;
link ( i, y );
}
scanf ( "%d", &m );
for ( i = 1; i <= m; i ++ ){
int x, y, fl;
scanf ( "%d", &fl );
if ( fl == 1 ){
scanf ( "%d", &x );
x ++;
printf ( "%d\n", get_ans ( x, n+1 )-1 );
}
else {
scanf ( "%d%d", &x, &y );
x ++;
int p = x+a[x];
if ( p > n ) p = n+1;
cut ( x, p );
a[x] = y;
p = x+a[x];
if ( p > n ) p = n+1;
link ( x, p );
}
}
return 0;
}