Description 某天,Lostmonkey發明瞭一種超級彈力裝置,為了在他的綿羊朋友面前顯擺,他邀請小綿羊一起玩個游戲。游戲一開始,Lostmonkey在地上沿著一條直線擺上n個裝置,每個裝置設定初始彈力繫數ki,當綿羊達到第i個裝置時,它會往後彈ki步,達到第i+ki個裝置,若不存在第i+ ...
Submit: 13753 Solved: 6983
[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
41 2 1 1
3
1 1
2 1 1
1 1
Sample Output
23
HINT
Source
1A了好激動QWQ..
首先考慮沒有修改操作,那麼一個遞推就解決了
但是有修改操作呢?
對於一個節點來說,它有且僅有一條出邊,這樣我們如果開一個超級點,表示到達這個點就跳出去的話,這很顯然是一棵樹
刪除操作就相當於斷開一條邊,然後再連接一條邊
於是我們可以用LCT維護這個東西
如果$i$這個位置跳一下能跳出去,就向$N+1$連邊,否則向它能跳到的位置連邊
詢問操作,我們可以先把要詢問的節點置成根,然後access(N+1),最後把$N+1$ splay到根節點,那麼$N+1$的子樹大小就是答案
剛開始想的是維護深度,但是平衡樹維護深度特別麻煩QWQ....
// luogu-judger-enable-o2 // luogu-judger-enable-o2 // luogu-judger-enable-o2 // luogu-judger-enable-o2 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=3 * 1e5 + 10; inline int read() { char c = getchar();int x = 0,f = 1; while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();} while(c >= '0' && c <= '9'){x = x * 10 + c - '0',c = getchar();} return x * f; } #define ls(x) T[x].ch[0] #define rs(x) T[x].ch[1] #define fa(x) T[x].f struct node { int ch[2], f, r, siz; }T[MAXN]; bool isroot(int x) { return T[fa(x)].ch[0] != x && T[fa(x)].ch[1] != x; } void update(int x) { T[x].siz = T[ls(x)].siz + T[rs(x)].siz + 1; } bool ident(int x) { return T[fa(x)].ch[0] == x ? 0 : 1; } void connect(int x, int fa, int how) { T[x].f = fa; T[fa].ch[how] = x; } void pushdown(int x) { if(T[x].r) { swap(ls(x), rs(x)); T[ls(x)].r ^= 1; T[rs(x)].r ^= 1; T[x].r = 0; } } void rotate(int x) { int Y = fa(x), R = fa(Y), Yson = ident(x), Rson = ident(Y); int B = T[x].ch[Yson ^ 1]; T[x].f = R; if(!isroot(Y)) connect(x, R, Rson); connect(B, Y, Yson); connect(Y, x, Yson ^ 1); update(Y);update(x); } int st[MAXN], top = 0; void splay(int x) { int now = x, top = 0; st[++top] = now; while(!isroot(now)) st[++top] = now = fa(now); while(top) pushdown(st[top--]); for(int y = T[x].f; !isroot(x); rotate(x), y = fa(x)) if(!isroot(y)) rotate( ident(x) == ident(y) ? y : x); } void access(int x) { for(int y = 0; x; x = fa(y = x)) splay(x), rs(x) = y, update(x); } int findroot(int x) { access(x); splay(x); while(ls(x)) x = ls(x); return x; } void makeroot(int x) { access(x); splay(x); T[x].r ^= 1; } void link(int x, int y) { makeroot(x); fa(x) = y; } void cut(int x, int y) { makeroot(x); if(findroot(y) == x && fa(x) == y && !rs(x)) fa(x) = T[y].ch[0] = 0, update(y); } int N; int a[MAXN]; int main() { #ifdef WIN32 freopen("a.in","r",stdin); //freopen("a.out","w",stdout); #else #endif N = read(); for(int i = 1; i <= N; i++) { a[i] = read(); (i + a[i] > N) ? link(i, N + 1) : link(i, i + a[i]); } int M = read(); while(M--) { int opt = read(), x = read() + 1; if(opt == 1) { makeroot(x); access(N + 1); splay(N + 1); printf("%d\n", T[N + 1].siz - 1); } else { int y = read(); //splay(x); (x + a[x] > N) ? cut(x, N + 1) : cut(x, x + a[x]); a[x] = y; (x + a[x] > N) ? link(x, N + 1) : link(x, x + a[x]); } } return 0; }