【HNOI2010】【BZOJ2002】Bounce 弹飞绵羊

【题目链接】

【前置技能】

  • LCT

【题解】

  • 将每个位置向下一个弹跳到的位置连边,弹出去的连到点 n + 1 n+1 n+1。SPLAY维护大小 s i z e size size,就可以支持查询点到根 n + 1 n+1 n+1的距离。
  • 时间复杂度 O ( Q l o g N ) O(QlogN) O(QlogN)

【代码】

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long
#define MAXN    200010
using namespace std;
int n, m, k[MAXN];
 
template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}
 
struct Link_Cut_Tree{
    struct info{int son[2], fa, up, tag, size;}a[MAXN];
    int cnt;
    void create(){
        ++cnt;
        a[cnt].size = 1;
    }
    int get(int pos){
        return (pos == a[a[pos].fa].son[1]);
    }
    void push_up(int pos){
        a[pos].size = 1;
        if (a[pos].son[0]) a[pos].size += a[a[pos].son[0]].size;
        if (a[pos].son[1]) a[pos].size += a[a[pos].son[1]].size;
    }
    void push_down(int pos){
        if (!a[pos].tag) return;
        a[pos].tag = 0;
        swap(a[pos].son[0], a[pos].son[1]);
        if (a[pos].son[0]) a[a[pos].son[0]].tag ^= 1;
        if (a[pos].son[1]) a[a[pos].son[1]].tag ^= 1;
    }
    void rotate(int pos){
        int fa = a[pos].fa, gr = a[fa].fa;
        a[pos].up = a[fa].up, a[fa].up = 0;
        push_down(fa), push_down(pos);
        int x = get(pos), f = get(fa);
        if (a[pos].son[x ^ 1]) a[a[pos].son[x ^ 1]].fa = fa;
        a[fa].son[x] = a[pos].son[x ^ 1];
        a[pos].son[x ^ 1] = fa;
        a[fa].fa = pos;
        if (gr) a[gr].son[f] = pos;
        a[pos].fa = gr;
        push_up(fa), push_up(pos);
    }
    void splay(int pos){
        push_down(pos);
        for (int fa = a[pos].fa; (fa = a[pos].fa); rotate(pos))
            if (a[fa].fa){
                if (get(pos) == get(fa)) rotate(fa);
                else rotate(pos);
            }
    }
    void access(int x){
        splay(x);
        int tmp = a[x].son[1];
        if (tmp) a[tmp].fa = 0, a[tmp].up = x;
        a[x].son[1] = 0;
        push_up(x);
        while (a[x].up){
            int f = a[x].up;
            splay(f);
            int tmp = a[f].son[1];
            if (tmp) a[tmp].fa = 0, a[tmp].up = f;
            a[f].son[1] = 0;
            a[x].fa = f, a[x].up = 0, a[f].son[1] = x;
            push_up(f);
            splay(x);
        }
    }
    void reverse(int x){
        access(x);
        a[x].tag ^= 1;
        push_down(x);
    }
    void link(int x, int y){
        access(x), reverse(y);
        a[x].son[1] = y, a[y].fa = x;
        push_up(x);
    }
    void cut(int x, int y){
        reverse(x), access(y), splay(x);
        a[x].son[1] = 0, a[y].fa = 0;
        push_up(x);
    }
    int query(int x){
        reverse(n + 1);
        access(x);
        return a[x].size - 1;
    }
    void modify(int x, int y, int z){
        if (y == z) return;
        if (y) cut(x, y);
        if (z) link(x, z);
    }
}lct;
 
int main(){
    read(n);
    for (int i = 1; i <= n + 1; ++i)
        lct.create();
    for (int i = 1; i <= n; ++i){
        read(k[i]);
        if (i + k[i] <= n) lct.link(i, i + k[i]);
        else lct.link(i, n + 1);
    }
    read(m);
    while (m--){
        int opt, x, y; read(opt), read(x); ++x;
        if (opt == 1){
            printf("%d\n", lct.query(x));
        } else {
            read(y);
            int tmp = min(x + k[x], n + 1), tnp = min(x + y, n + 1);
            k[x] = y;
            lct.modify(x, tmp, tnp);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值