BZOJ2002: [Hnoi2010]Bounce 彈飛綿羊(LCT)

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 13753  Solved: 6983
[Submit][Status][Discuss]

Description

某天,Lostmonkey發明了一種超級彈力裝置,爲了在他的綿羊朋友面前顯擺,他邀請小綿羊一塊兒玩個遊戲。遊戲一開始,Lostmonkey在地上沿着一條直線擺上n個裝置,每一個裝置設定初始彈力系數ki,當綿羊達到第i個裝置時,它會日後彈ki步,達到第i+ki個裝置,若不存在第i+ki個裝置,則綿羊被彈飛。綿羊想知道當它從第i個裝置起步時,被彈幾回後會被彈飛。爲了使得遊戲更有趣,Lostmonkey能夠修改某個彈力裝置的彈力系數,任什麼時候候彈力系數均爲正整數。php

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<=100000html

Output

對於每一個i=1的狀況,你都要輸出一個須要的步數,佔一行。node

Sample Input

4
1 2 1 1
3
1 1
2 1 1
1 1

Sample Output

2
3

HINT

 

Source

 

1A了好激動QWQ..ide

首先考慮沒有修改操做,那麼一個遞推就解決了post

可是有修改操做呢?spa

對於一個節點來講,它有且僅有一條出邊,這樣咱們若是開一個超級點,表示到達這個點就跳出去的話,這很顯然是一棵樹code

刪除操做就至關於斷開一條邊,而後再鏈接一條邊htm

因而咱們能夠用LCT維護這個東西blog

若是$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;
}
相關文章
相關標籤/搜索