bzoj 2002 弹飞绵羊 lct

2002: [Hnoi2010]Bounce 弹飞绵羊

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 15239  Solved: 7788
[Submit][Status][Discuss]

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

从点i飞到j,那么连边i->j,并增加虚节点n+1,那么这n+1个节点组成了一棵树,每次询问从根到点i的路径上的节点数,那么access一下点i,那么路径节点数即根节点所在splay节点数。

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
 
#define ll long long
const int maxn = 2e5 + 10;
int top, tot;
int fa[maxn], tr[maxn][2], p[maxn];
int ve[maxn];
int tag[maxn], sz[maxn];
int n, m;
 
int lson(int x) {
    return tr[x][0];
}
 
int rson(int x) {
    return tr[x][1];
}
 
int judge(int x) {
    return tr[fa[x]][1] == x;
}
 
int isroot(int x) {
    return (tr[fa[x]][0] != x && tr[fa[x]][1] != x);
}
 
void pushrev(int x) {
    if (!x) return ;
    tag[x] ^= 1;
    swap(tr[x][0], tr[x][1]);
}
 
void pushup(int x) {
    sz[x] = 1;
    if (lson(x)) sz[x] += sz[lson(x)];
    if (rson(x)) sz[x] += sz[rson(x)];
}
 
void pushdown(int x) {
    if (tag[x]) {
        pushrev(lson(x));
        pushrev(rson(x));
        tag[x] = 0;
    }
}
 
void rotate(int x) {
    int y = fa[x], d = judge(x);
    if (tr[y][d] = tr[x][d ^ 1]) fa[tr[y][d]] = y;
    if ((fa[x] = fa[y]) && !isroot(y)) tr[fa[y]][judge(y)] = x;
    tr[fa[y] = x][d ^ 1] = y;
    pushup(y);
}
 
void splay(int x) {
    for (int y = ve[top = 1] = x; !isroot(y); y = fa[y])
        ve[++top] = fa[y];
    while (top) pushdown(ve[top--]);
    for (int y; !isroot(x); rotate(x))
        if (!isroot(y = fa[x])) rotate(judge(x) == judge(y) ? y : x);
    pushup(x);
}
 
void access(int x) {
    for (int y = 0; x; x = fa[y = x]) {
        splay(x);
        tr[x][1] = y;
        pushup(x);
    }
}
 
void makeroot(int x) {
    access(x);
    splay(x);
    pushrev(x);
}
 
int findroot(int x) {
    access(x);
    splay(x);
    pushdown(x);
    while (lson(x)) pushdown(x = lson(x));
    return x;
}
 
void link(int x, int y) {
    makeroot(x);
    if (findroot(y) != x) 
        fa[x] = y;
}
 
void split(int x, int y) {
    makeroot(x);
    access(y);
    splay(y);
}
 
void cut(int x, int y) {
    makeroot(x);
    if (findroot(y) == x && fa[x] == y && tr[y][0] == x && !tr[x][1]) { 
        fa[x] = tr[y][0] = 0;
        pushup(y);
    }
}
 
int main() {
    memset(tag, 0, sizeof(tag));
    memset(tr, 0, sizeof(tr));
    memset(sz, 0, sizeof(sz));
    memset(fa, 0, sizeof(fa));
    scanf("%d", &n);
    int u, v, x;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &x);
        v = i + x > n ? n + 1 : i + x;
        link(i, v);
        p[i] = v;
    }
    scanf("%d", &m);
    int cnt = 0;
    while (m--) {
        cnt ++;
        scanf("%d", &x);
        if (x == 1) {
            scanf("%d", &u);
            u++;
            split(u, n + 1);
            printf("%d\n", sz[n + 1] - 1);
        }
        else if (x == 2) {
            scanf("%d%d", &u, &v);
            u++;
            cut(u, p[u]);
            int np = u + v > n ? n + 1 : u + v;
            link(u, np);
            p[u] = np; 
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值