现在有n个装置,每个装置会弹到一个地方(编号一定在它之后),也可能直接弹了出去,动态修改会弹到的地方,和动态查询从一个装置出发多少次会被弹出去。
1 <= n <= 200000, 1 <= m <= 100000
lct裸题不解释。
我就是来放模板的。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i, x, y) for(int i = x; i <= y; i ++)
#define min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
const int Maxn = 200050;
int n, b[Maxn], fa[Maxn], pf[Maxn], t[Maxn][2], st[Maxn];
struct node {
int siz, rev;
}a[Maxn];
void chan(int x) {if(x) a[x].rev ^= 1, swap(t[x][0], t[x][1]);}
void down(int x) {if(a[x].rev) chan(t[x][0]), chan(t[x][1]), a[x].rev = 0;}
void update(int x) {if(x) a[x].siz = a[t[x][0]].siz + a[t[x][1]].siz + 1;}
void xc(int x) {
for(; x; x = fa[x]) st[++ st[0]] = x;
for(; st[0]; st[0] --) down(st[st[0]]);
}
int lr(int x) {return t[fa[x]][1] == x;}
void rotate(int x) {
int y = fa[x], k = lr(x);
t[y][k] = t[x][!k]; if(t[x][!k]) fa[t[x][!k]] = y;
fa[x] = fa[y]; if(fa[y]) t[fa[y]][lr(y)] = x;
t[x][!k] = y; fa[y] = x; pf[x] = pf[y];
update(y); update(x);
}
void splay(int x, int y) {
xc(x);
while(fa[x] != y) {
if(fa[fa[x]] != y)
if(lr(x) == lr(fa[x])) rotate(fa[x]); else rotate(x);
rotate(x);
}
}
void access(int x) {
for(int y = 0; x; update(x), y = x, x = pf[x])
splay(x, 0), fa[t[x][1]] = 0, pf[t[x][1]] = x,
t[x][1] = y, fa[y] = x, pf[y] = 0;
}
void makeroot(int x) {
access(x); splay(x, 0); chan(x);
}
void link(int x, int y) {
makeroot(x); pf[x] = y;
}
void cut(int x, int y) {
makeroot(x); access(y);
splay(y, 0);
t[y][0] = fa[x] = pf[x] = 0;
update(y);
}
int main() {
scanf("%d", &n);
fo(i, 1, n) scanf("%d", &b[i]), link(i, min(n + 1, i + b[i]));
int Q; for(scanf("%d", &Q); Q; Q --) {
int z, x; scanf("%d %d", &z, &x); x ++;
if(z == 1) {
makeroot(n + 1); access(x); splay(x, 0);
printf("%d\n", a[x].siz - 1);
} else {
cut(x, min(x + b[x], n + 1));
scanf("%d", &b[x]);
link(x, min(x + b[x], n + 1));
}
}
}