【动态树】[BZOJ2002] Bounce 弹飞绵羊

实际上就是动态树的模版加上一个维护每一条链的size这样就可以吧一次弹射看成一条路径,然后统计这个路径上的size实际上就是经过了多少个节点然后没什么了。。

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000;
int ch[MAXN+10][2], Fa[MAXN+10];
int que[MAXN+10], sz[MAXN+10];
bool rev[MAXN+10];
bool isroot(int u){return ch[Fa[u]][0] != u && ch[Fa[u]][1] != u;}
void Rotate(int u){
    int x = Fa[u], y = Fa[x];
    bool md = ch[x][1] == u;
    Fa[u] = y;
    if(!isroot(x)) ch[y][ch[y][1] == x] = u;
    ch[x][md] = ch[u][!md]; Fa[ch[u][!md]] = x;
    ch[u][!md] = x; Fa[x] = u;
    sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
}
void push_down(int u){
    if(rev[u]){
        rev[u] ^= 1; rev[ch[u][0]] ^= 1; rev[ch[u][1]] ^= 1;
        swap(ch[u][0], ch[u][1]);
    }
}
void Splay(int u){
    int now = u;
    que[0] = 1; que[1] = now;
    while(!isroot(now)){
        que[++que[0]] = Fa[now];
        now = Fa[now];
    }
    for(;que[0];que[0]--) push_down(que[que[0]]);
    while(!isroot(u)){
        int x = Fa[u], y = Fa[x];
        if(!isroot(x)){
            if((ch[x][0] == u) ^ (ch[y][0] == x)) Rotate(u);
            else Rotate(x);
        }
        Rotate(u);
    }
    sz[u] = sz[ch[u][0]] + sz[ch[u][1]] + 1;
}
void access(int u){
    int up = 0;
    while(u != 0){
        Splay(u);
        ch[u][1] = up;
        up = u; u = Fa[u];
    }
}
void root(int u){
    access(u);
    Splay(u);
    rev[u] ^= 1;
}
void Link(int x, int y){
    root(x);
    Fa[x] = y;
    Splay(x);
}
void Cut(int x, int y){
    root(x);
    access(y);
    Splay(y);
    ch[y][0] = Fa[x] = 0;
}
/*bool Query(int x, int y){ access(x); Splay(x); int u = x; while(ch[u][0]) u = ch[u][0]; access(y); Splay(y); int u1 = y; while(ch[u1][0]) u1 = ch[u1][0]; return u == u1; }*/
int Jp[MAXN+10];
int main(){
    int n, m, a, b;
    scanf("%d", &n);
    for(int i=1;i<=n;i++){
        scanf("%d", &a);
        Jp[i] = min(i+a, n+1);
        Link(Jp[i], i);
    }
    int ord;
    scanf("%d", &m);
    for(int i=0;i<m;i++){
        scanf("%d", &ord);
        if(ord == 1){
            scanf("%d", &a);
            root(n+1);
            access(a+1);
            Splay(a+1);
            printf("%d\n", sz[ch[a+1][0]]);
        }
        else{
            scanf("%d %d", &a, &b);
            Cut(a+1, Jp[a+1]);
            Jp[a+1] = a + 1 + b;
            if(a + b + 1 > n) Jp[a+1] = n+1;
            Link(Jp[a+1], a+1);
        }
    }

    return 0;
}

你可能感兴趣的:(【动态树】[BZOJ2002] Bounce 弹飞绵羊)