2002: [Hnoi2010]Bounce 弹飞绵羊
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 5392 Solved: 2846
[ 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
维护子树的大小,注意下标是从0开始的。
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 2 * 1e5 + 5;
#define LS(x) node[(x)].ch[0]
#define RS(x) node[(x)].ch[1]
int nxt[N];
struct LCT {
struct Node {
int fa, ch[2];
bool rev;
int size;
void init() {
fa = 0;
rev = ch[0] = ch[1] = 0;
size = 1;
}
} node[N];
bool is_root(int x) {
return (LS(node[x].fa) != x && RS(node[x].fa) != x);
}
void down(int x) {
if(node[x].rev) {
node[LS(x)].rev ^= 1;
node[RS(x)].rev ^= 1;
swap(LS(x), RS(x));
node[x].rev = 0;
}
}
void up(int x) {
node[x].size = node[LS(x)].size + node[RS(x)].size + 1;
}
void rotate(int x, bool kind) {
int fx = node[x].fa;
int ffx = node[fx].fa;
node[fx].ch[!kind] = node[x].ch[kind];
node[node[x].ch[kind]].fa = fx;
if(!is_root(fx))
node[ffx].ch[RS(ffx) == fx] = x;
node[x].fa = ffx;
node[x].ch[kind] = fx;
node[fx].fa = x;
up(fx);
}
int d_st[N], ds_top;
void splay(int x) {
ds_top = -1;
d_st[++ds_top] = x;
for(int i = x; !is_root(i); i = node[i].fa)
d_st[++ds_top] = node[i].fa;
for(int i = ds_top; i >= 0 ; --i) down(d_st[i]);
while(!is_root(x)) {
int fx = node[x].fa;
int ffx = node[fx].fa;
bool rot_x = (LS(fx) == x);
bool rot_fx = (LS(ffx) == fx);
if(is_root(fx)) rotate(x, rot_x);
else {
if(rot_x == rot_fx) rotate(fx, rot_fx);
else rotate(x, rot_x);
rotate(x, rot_fx);
}
}
up(x);
}
void access(int x) {
int last = 0;
while(x) {
splay(x);
RS(x) = last;
last = x;
x = node[x].fa;
}
}
//access(x); splay(x); 后x在根的位置,此时x只有左子树,没有右子树,打翻转标记就是使得左子树变到右子树上。
void make_root(int x) {
access(x); splay(x);
node[x].rev ^= 1;
}
void cut(int x, int y) {
make_root(x);
access(y); splay(y);
LS(y) = node[x].fa = 0;
up(y);
}
void link(int x, int y) {
make_root(x);
node[x].fa = y;
}
void init(int n) {
for(int i = 0; i <= n; ++i)
node[i].init();
node[0].size = 0;
}
void debug(int n) {
for(int i = 1; i <= n; ++i) {
cout<<i<<": "<<node[i].size<<' '<<node[i].fa<<' '<<node[i].ch[0]<<' '<<node[i].ch[1]<<endl;
}
}
void op1(int x, int n) {
make_root(n + 1);
access(x); splay(x);
printf("%d\n", node[LS(x)].size);
}
void op2(int x, int y, int n) {
int u = x, v = min(n + 1, x + y), z = nxt[x];
cut(u, z);
link(u, v);
nxt[x] = v;
}
} lct;
int main() {
int n;
scanf("%d", &n);
lct.init(n + 1);
for(int i = 1; i <= n; ++i) {
int a;
scanf("%d", &a);
lct.node[i].fa = i + a;
if(lct.node[i].fa > n) lct.node[i].fa = n + 1;
nxt[i] = lct.node[i].fa;
}
//lct.debug(n + 1);
int m;
scanf("%d", &m);
for(int i = 0; i < m; ++i) {
int op, x, y;
scanf("%d", &op);
if(op == 1) {
scanf("%d", &x);
++x;
lct.op1(x, n);
} else {
scanf("%d%d", &x, &y);
++x;
lct.op2(x, y, n);
}
//lct.debug(n + 1);
}
return 0;
}