您的位置:首页 > 其它

【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊

2017-03-15 18:25 309 查看
题目链接

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
;
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
;
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->p
4000
up();
}
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: