bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊 LCT

题目:

http://www.lydsy.com/JudgeOnline/problem.php?id=2002

题意:

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

思路:

之前用分块做过这题。用 LCT 做的话,对于第 i 个位置,弹跳系数为x,那么从 i i+x连边,把 i+x 作为父节点,如果 i+x>n 即弹出去了,不妨设为 n+1 ,然后就是 LCT 基本操作,查询的时候维护左子树大小即可

#include <bits/stdc++.h>

using namespace std;

const int N = 200000 + 10, INF = 0x3f3f3f3f;

int son[N][2], fat[N], siz[N], lazy[N], nxt[N];
int top, st[N];

bool is_root(int x)
{
    return son[fat[x]][0] != x && son[fat[x]][1] != x;
}
void push_up(int x)
{
    siz[x] = siz[son[x][0]] + siz[son[x][1]] + 1;
}
void push_down(int x)
{
    if(lazy[x])
    {
        swap(son[x][0], son[x][1]);
        lazy[son[x][0]] ^= 1, lazy[son[x][1]] ^= 1;
        lazy[x] ^= 1;
    }
}
void Rotate(int x)
{
    int y = fat[x], p = son[y][0] == x;
    son[y][!p] = son[x][p], fat[son[x][p]] = y;
    if(! is_root(y)) son[fat[y]][son[fat[y]][1]==y] = x;
    fat[x] = fat[y];
    son[x][p] = y, fat[y] = x;
    push_up(y);
}
void splay(int x)
{
    top = 0; st[++top] = x;
    for(int i = x; !is_root(i); i = fat[i]) st[++top] = fat[i];
    for(int i = top; i >= 1; i--) push_down(st[i]);
    while(! is_root(x))
    {
        int y = fat[x], z = fat[y];
        if(is_root(y)) Rotate(x);
        else
        {
            if((x == son[y][0]) ^ (y == son[z][0])) Rotate(x), Rotate(x);
            else Rotate(y), Rotate(x);
        }
    }
    push_up(x);
}
void access(int x)
{
    int y = 0;
    while(x)
    {
        splay(x);
        son[x][1] = y;
        push_up(x);//在splay过程中其实会维护到,加不加无所谓,为了心安还是加上吧。。。
        y = x, x = fat[x];
    }
}
void make_root(int x)
{
    access(x); splay(x);
    lazy[x] ^= 1;
}
void link(int x, int y)
{
    make_root(x); fat[x] = y; splay(x);
}
void cut(int x, int y)
{
    make_root(x); access(y); splay(y);
    son[y][0] = fat[x] = 0;
}
int main()
{
    int n, m, opt, x, y;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &x);
        fat[i] = i + x <= n ? i+x : n+1;
        nxt[i] = fat[i];
        siz[i] = 1;
    }
    siz[n+1] = 1;
    scanf("%d", &m);
    for(int i = 1; i <= m; i++)
    {
        scanf("%d", &opt);
        if(opt == 1)
        {
            scanf("%d", &x);
            x++;
            make_root(n+1); access(x); splay(x);
            printf("%d\n", siz[son[x][0]]);
        }
        else
        {
            scanf("%d%d", &x, &y);
            x++;
            int t = x+y <= n ? x+y : n+1;
            cut(x, nxt[x]); link(x, t);
            nxt[x] = t;
        }
    }
    return 0;
}

另外一种不太一样的写法,主要是没有把 x 调整为原树中的根这个操作,引起link cut 也有所不同

#include <bits/stdc++.h>

using namespace std;

const int N = 200000 + 10, INF = 0x3f3f3f3f;
int son[N][2], fat[N], siz[N];
void push_up(int x)
{
    siz[x] = siz[son[x][0]] + siz[son[x][1]] + 1;
}
bool is_root(int x) //判断x是不是所在辅助树的根
{
    return (son[fat[x]][0] != x && son[fat[x]][1] != x);
}
void Rotate(int x)
{
    int y = fat[x], p = son[y][0] == x;
    son[y][!p] = son[x][p], fat[son[x][p]] = y;
    if(!is_root(y)) son[fat[y]][son[fat[y]][1]==y] = x;
    fat[x] = fat[y];
    son[x][p] = y, fat[y] = x;
    push_up(y);
}
void splay(int x)
{
    while(! is_root(x))
    {
        int y = fat[x], z = fat[y];
        if(is_root(y)) Rotate(x);
        else
        {
            if((x == son[y][0]) ^ (y == son[z][0])) Rotate(x), Rotate(x);
            else Rotate(y), Rotate(x);
        }
    }
    push_up(x);
}
void access(int x)
{
    int y = 0;
    while(x)
    {
        splay(x);
        son[x][1] = y;
        push_up(x);
        y = x, x = fat[x];
    }
}
void cut(int x)
{//把x旋转到splay的根,其父节点即是x的左儿子,直接切掉即可
    access(x);
    splay(x);
    son[x][0] = fat[son[x][0]] = 0;
}
void link(int x, int y)
{
    cut(x);
    fat[x] = y;
    push_up(x);
}
int main()
{
    int n, m, x, y;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &x);
        //link(i, i + x <= n ? i+x : 0);
        fat[i] = i + x <= n ? i+x : n+1;
        siz[i] = 1;
    }
    siz[n+1] = 1;
    scanf("%d", &m);
    int opt;
    for(int i = 1; i <= m; i++)
    {
        scanf("%d", &opt);
        if(opt == 1)
        {
            scanf("%d", &x);
            x++;
            access(x); splay(x);
            printf("%d\n", siz[son[x][0]]);
        }
        else
        {
            scanf("%d%d", &x, &y);
            x++;
            link(x, x + y <= n ? x+y : n+1);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值