BZOJ 2002 [LCT]

10 篇文章 0 订阅

2002: [Hnoi2010]Bounce 弹飞绵羊
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的情况,你都要输出一个需要的步数,占一行。

题意很简单,我们只需要这样建树,如果 u 可以弹到 v 那么 v 就是 u 的父亲,如果被弹飞就为null。这时候只需要用 Link-Cut Tree 来维护子树大小(其实就是深度)即可

附代码:

#include <cstdio>
#include <cstdlib>
#define N 200010
using namespace std;

inline char get(void) {
    static char buf[100000], *p1 = buf, *p2 = buf;
    if (p1 == p2) {
        p2 = (p1 = buf) + fread(buf, 1, 100000, stdin);
        if (p1 == p2) return EOF;
    }
    return *p1++;
}
inline void read(int &x) {
    static char c; x = 0;
    for (c = get(); c < '0' || c > '9'; c = get());
    for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = get());
}

namespace Link_Cut_Tree {
    struct node *null;
    struct node {
        node *ch[2], *fa;
        int size;
        inline void maintain(void) {
            size = ch[0]->size + ch[1]->size + 1;
        }
        inline void setc(node *k, bool d) {
            ch[d] = k; k->fa = this;
        }
        inline bool cmp(void) {
            return fa->ch[1] == this;
        }
        inline bool check(void) {
            return fa == null || (fa->ch[0] != this && fa->ch[1] != this);
        }
    };
    node *Tail, mem[N];
    void Init(void) {
        Tail = mem; null = Tail++;
        null->ch[0] = null->ch[1] = null->fa = null;
        null->size = 0;
    }
    inline node* NewNode(void) {
        node* p = Tail++;
        p->ch[0] = p->ch[1] = p->fa = null;
        p->size = 1; return p;
    }
    inline void Rotate(node* o) {
        node *fa = o->fa; bool d = o->cmp();
        if (fa->check()) o->fa = fa->fa;
        else fa->fa->setc(o, fa->cmp());
        fa->setc(o->ch[d ^ 1], d);
        o->setc(fa, d ^ 1);
        fa->maintain();
    }
    void Splay(node* o) {
        while (!o->check()) {
            if (o->fa->check()) {
                Rotate(o);
            } else {
                if (o->cmp() == o->fa->cmp()) {
                    Rotate(o->fa); Rotate(o);
                } else {
                    Rotate(o); Rotate(o);
                }
            }
        }
        o->maintain();
    }
    void Access(node* o) {
        for (node *p = null; o != null; o = o->fa) {
            Splay(o); o->setc(p, 1);
            o->maintain(); p = o;
        }
    }
    void Link(node* o, node* to) {
        Access(o); Splay(o);
        o->ch[0]->fa = null; o->ch[0] = null; o->fa = to;
        o->maintain();
    }
}

using namespace Link_Cut_Tree;
node* pos[N];
int n, m, x, y, opr;

int main(void) {
    freopen("1.in", "r", stdin);
    read(n); Init();
    for (int i = 0; i < n; i++) pos[i] = NewNode();
    for (int i = 0; i < n; i++) {
        read(x);
        if (i + x < n) pos[i]->fa = pos[i + x];
    }
    read(m);
    for (int i = 0; i < m; i++) {
        read(opr); read(x);
        if (opr == 1) {
            Access(pos[x]); Splay(pos[x]);
            printf("%d\n", pos[x]->size);
        } else {
            read(y);
            if (x + y < n) Link(pos[x], pos[x + y]);
            else Link(pos[x], null);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值