【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模板题,但是好像比完全版的简单一点,并不需要翻转链就可以了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#define nd(x) (nil + (x))
using namespace std;

inline void in(int &x){
    x = 0; int f = 1; char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    x *= f;
}

const int N = 200000 + 10;
int n, m, v[N];
struct Node{
    int siz, rev;
    Node *f, *c[2];

    Node();
    int d() { return f->c[1] == this; }
    void sc(Node *x, int d) { (c[d] = x) -> f = this; }
    void pup() { siz = c[0]->siz + c[1]->siz + 1; }
    bool isroot() { return f->c[0] != this && f->c[1] != this; }
} *root, nil[N];
Node::Node() { c[0] = c[1]= f = nil; siz = 1; }
void rotate(Node *x){
    int d = x->d();
    Node *p = x->f;
    p->sc(x->c[!d], d);
    if(p->isroot()) x->f = p->f;
    else p->f->sc(x, p->d());
    x->sc(p, !d);
    x->pup(); p->pup();
}
void splay(Node *x){
    for(Node *y; !x->isroot();){
        y = x->f;
        if(!y->isroot()) (x->d() ^ y->d()) ? rotate(x) : rotate(y);
        rotate(x);
    }
    x->pup();
}
void access(Node *x){
    Node *t = nil;
    while(x != nil){
        splay(x);
        x->sc(t, 1); x->pup();
        t = x; x = x->f;
    }
}
void link(Node *x, Node *y){
    access(x); splay(x);
    x->c[0]->f = nil;
    x->c[0] = nil;
    x->f = y;
    x->pup();
}

void init(){
    in(n);
    nil->f = nil->c[0] = nil->c[1] = nil;
    nil->siz = 0;
    for(int i = 1; i <= n; i++){
        int x; in(x);
        if(x + i <= n) nil[i].f = nd(x + i);
    }
}
void work(){
    in(m);
    for(int i = 1; i <= m; i++){
        int f; in(f);
        if(f == 1){
            int x; in(x); x++;
            access(nd(x));
            splay(nd(x));
            printf("%d\n", nil[x].siz);
        }
        else{
            int x, y;
            in(x); in(y); x++;
            if(x + y <= n) link(nd(x), nd(x+y));
            else link(nd(x), nil);
        }
    }

}

int main(){
    init();
    work();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值