BZOJ2002: [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的情况,你都要输出一个需要的步数,占一行。

Sample Input
4
1 2 1 1 
3
1 1
2 1 1
1 1
Sample Output
2
3
简要题解

LCT的练手题,就是用LCT维护子树大小,不知道为什么就是比别人长了一倍(hzwer大神加起来都只要100行+,用时也比我快。。。)

AC code:
#include <ctime>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

inline int read()
{
    int x = 0; int v = 1, c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) if(c == '-') v = -1;
    for(;c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
    return x * v;
}

template<typename T>
inline void write(T x, int ch = 10)
{
    int s[20], t = 0;
    if(x < 0) { x = -x; putchar('-'); }
    do s[t++] = x % 10; while(x /= 10);
    while(t--) putchar('0' + s[t]);
    putchar(ch);
}

const int maxn = 200005;

struct lct
{
    int lc[maxn], rc[maxn], fa[maxn], size[maxn];
    bool rev[maxn];

    inline bool isroot(const int& o)
    {
        return lc[fa[o]] != o && rc[fa[o]] != o;
    }

    inline void maintain(const int& o)
    {
        size[o] = size[lc[o]] + size[rc[o]] + 1;
    }

    inline void pushdown(const int& o)
    {
        if(rev[o])
        {
            rev[o] = 0; rev[lc[o]] ^= 1; rev[rc[o]] ^= 1;
            swap(lc[o], rc[o]);
        }
    }

    inline void setc(const int& x, const int& y, bool kind)
    {
        //make y to be the son of x
        !kind ? lc[x] = y : rc[x] = y;
        if(y) fa[y] = x;
    }

    inline void rotate(const int& x)
    {
        int p = fa[x];
        bool kind = rc[p] == x;
        if(!isroot(p))
            setc(fa[p], x, rc[fa[p]] == p);
        else
            fa[x] = fa[p];
        setc(p, !kind ? rc[x] : lc[x], kind);
        setc(x, p, kind^1);
        maintain(p);
        maintain(x);
    }

    inline void splay(const int& x)
    {
        static int s[maxn];

        int t = 0;

        s[t++] = x;
        for(int i = x; !isroot(i); i = fa[i])
            s[t++] = fa[i];

        while(t--)
            pushdown(s[t]);

        while(!isroot(x))
        {
            int y = fa[x], z = fa[y];
            if(!isroot(y))
            {
                if(lc[z] == y ^ lc[y] == x)
                    rotate(x);
                else
                    rotate(y);
            }
            rotate(x);
        }
    }

    inline void access(int x)
    {
        for(int c = 0; x; c = x, x = fa[x])
        {
            splay(x);
            rc[x] = c;
        }
    }

    inline void makeroot(const int& x)
    {
        access(x);
        splay(x);
        rev[x] ^= 1;
    }

    inline void link(const int& x, const int& y)
    {
        makeroot(x);
        fa[x] = y;
        splay(x);
    }

    inline void cut(const int& x, const int& y)
    {
        makeroot(x);
        access(y);
        splay(y);
        lc[y] = fa[x] = 0;
    }
};

int n, m;
int next[maxn];
lct T;

void input()
{
    n = read();
    for(int i = 1; i <= n; ++i)
    {
        int x = read();
        T.fa[i] = x + i;
        T.size[i] = 1;
        if(T.fa[i] > n + 1)
            T.fa[i] = n + 1;
        next[i] = T.fa[i];
    }
    T.size[n+1] = 1;
}

void calc(const int& x)
{
    T.makeroot(n + 1);
    T.access(x);
    T.splay(x);
    write(T.size[T.lc[x]]);
}

void modify(const int& x, const int& y)
{
    T.cut(x, next[x]);
    T.link(x, y);
    next[x] = y;
}

void solve()
{
    register int x;

    m = read();
    for(int i = 1; i <= m; ++i)
    {
        int op = read();
        x = read() + 1;
        if(op == 1)
            calc(x);
        else
            modify(x, min(n+1, x+read()));
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    input();
    solve();

#ifndef ONLINE_JUDGE
    fprintf(stderr, "Run Time: %dms\n", clock());
    fclose(stdin), fclose(stdout);
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值