Description
- 某天,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
Output
- 對於每個i=1的情況,你都要輸出一個需要的步數,佔一行。
Sample Input
4
1 2 1 1
3
1 1
2 1 1
1 1
Sample Output
2
3
思路:
- LCT只用維護一個size值。。。
設從i可以跳到next[i],就在i與next[i]之間連邊,超出n的統一記作n+1。
代碼:
using namespace std;
const int MAXN = 200005;
int n, m;
int nex[MAXN], siz[MAXN], fa[MAXN], tr[MAXN][2];
bool rev[MAXN];
int q[MAXN], top = 0;
void pushup(int x) { siz[x] = siz[tr[x][0]] + siz[tr[x][1]] + 1; }
void pushdown(int x) {
int l = tr[x][0], r = tr[x][1];
if (rev[x]) {
rev[x] ^= 1;
rev[l] ^= 1;
rev[r] ^= 1;
swap(tr[x][0], tr[x][1]);
}
}
bool isroot(int x) {
return tr[fa[x]][0] != x && tr[fa[x]][1] != x;
}
void rotate(int x) {
int y = fa[x], z = fa[y];
int l, r;
if (tr[y][0] == x) l = 0;
else l = 1;
r = l ^ 1;
if (!isroot(y)) {
if (tr[z][0] == y) tr[z][0] = x;
else tr[z][1] = x;
}
fa[x] = z;
fa[y] = x;
fa[tr[x][r]] = y;
tr[y][l] = tr[x][r];
tr[x][r] = y;
pushup(y);
pushup(x);
}
void splay(int x) {
top = 0;
q[++top] = x;
for (int i = x; !isroot(i); i = fa[i])
q[++top] = fa[i];
for (int i = top; i; i--) pushdown(q[i]);
while (!isroot(x)) {
int y = fa[x], z = fa[y];
if (!isroot(y)) {
if (tr[y][0] == x ^ tr[z][0] == y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x) {
int t = 0;
while (x) {
splay(x);
tr[x][1] = t;
t = x;
x = fa[x];
}
}
void makeroot(int x) {
access(x);
splay(x);
rev[x] ^= 1;
}
void link(int x, int y) {
makeroot(x);
fa[x] = y;
splay(x);
}
void cut(int x, int y) {
makeroot(x);
access(y);
splay(y);
tr[y][0] = fa[tr[y][0]] = 0;
}
int main() {
scanf("%d", &n);
int op, x, y;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
fa[i] = x + i;
siz[i] = 1;
if (fa[i] > n + 1) fa[i] = n + 1;
nex[i] = fa[i];
}
siz[n + 1] = 1;
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d", &op);
if (op == 1) {
scanf("%d", &x);
x++;
makeroot(n + 1);
access(x);
splay(x);
printf("%d\n", siz[tr[x][0]]);
} else {
scanf("%d %d", &x, &y);
x++;
int t = min(x + y, n + 1);
cut(x, nex[x]);
link(x, t);
nex[x] = t;
}
}
return 0;
}