bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)

时间:2021-08-27 15:30:36

链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002

题面:

2002: [Hnoi2010]Bounce 弹飞绵羊

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 15763  Solved: 8080
[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
 
 
思路;
之前用分块写过,分块的写法比较难想
除了分块写法还可以用LCT写,用LCT的话就很简单了
我们对每个点与当前点下一步被弹到的点建边,每个点赋值为1,并建一个虚点n+1代表已被弹出去
那么直接询问这一条链上的权值和就知道他跳了多少次了,减去最开始点的权值1就是跳的次数
修改的话直接cut当前边,然后link建新边,并维护下v数组里的值就好了
 
实现代码:
#include<bits/stdc++.h>
using namespace std;
const int M = 3e5+;
const int inf = 0x3f3f3f3f;
int n,m,sz,rt,c[M][],fa[M],v[M],sum[M],st[M],top;
bool rev[M]; inline void up(int x){
int l = c[x][],r = c[x][];
sum[x] = sum[l] + sum[r] + ;
} inline void pushrev(int x){
int t = c[x][];
c[x][] = c[x][]; c[x][] = t;
rev[x] ^= ;
} inline void pushdown(int x){
if(rev[x]){
int l = c[x][],r = c[x][];
if(l) pushrev(l);
if(r) pushrev(r);
rev[x] = ;
}
} inline bool nroot(int x){ //判断一个点是否为一个splay的根
return c[fa[x]][]==x||c[fa[x]][] == x;
} inline void rotate(int x){
int y = fa[x],z = fa[y],k = c[y][] == x;
int w = c[x][!k];
if(nroot(y)) c[z][c[z][]==y]=x;
c[x][!k] = y; c[y][k] = w;
if(w) fa[w] = y; fa[y] = x; fa[x] = z;
up(y);
} inline void splay(int x){
int y = x,z = ;
st[++z] = y;
while(nroot(y)) st[++z] = y = fa[y];
while(z) pushdown(st[z--]);
while(nroot(x)){
y = fa[x];z = fa[y];
if(nroot(y))
rotate((c[y][]==x)^(c[z][]==y)?x:y);
rotate(x);
}
up(x);
} //打通根节点到指定节点的实链,使得一条中序遍历从根开始以指定点结束的splay出现
inline void access(int x){
for(int y = ;x;y = x,x = fa[x])
splay(x),c[x][]=y,up(x);
} inline void makeroot(int x){ //换根,让指定点成为原树的根
access(x); splay(x); pushrev(x);
} inline int findroot(int x){ //寻找x所在原树的树根
access(x); splay(x);
while(c[x][]) pushdown(x),x = c[x][];
splay(x);
return x;
} inline void split(int x,int y){ //拉出x-y的路径成为一个splay
makeroot(x); access(y); splay(y);
} inline void cut(int x,int y){ //断开边
makeroot(x);
if(findroot(y) == x&&fa[y] == x&&!c[y][]){
fa[y] = c[x][] = ;
up(x);
}
} inline void link(int x,int y){ //连接边
makeroot(x);
if(findroot(y)!=x) fa[x] = y;
} int main()
{
int op,x,y;
scanf("%d",&n);
int root = n+;
for(int i = ;i <= n+;i ++) sum[i] = ;
for(int i = ;i <= n;i ++){
scanf("%d",&v[i]);
if(v[i]+i>n) link(root,i);
else link(v[i]+i,i);
}
scanf("%d",&m);
while(m--){
scanf("%d%d",&op,&x); x++;
if(op == ){
split(root,x);
printf("%d\n",sum[x]-);
}
else{
scanf("%d",&y);
cut(min(v[x]+x,root),x);
v[x] = y;
link(x,min(v[x]+x,root));
}
}
return ;
}