bzoj 2002[Hnoi2010]Bounce 弹飞绵羊(分治分块)
Posted 抓不住Jerry的Tom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj 2002[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
1 2 1 1
3
1 1
2 1 1
1 1
Sample Output
3




1 #include <bits/stdc++.h> 2 3 using namespace std; 4 const int maxn = 220000; 5 int n,m; 6 int q,op; 7 int a[maxn]; 8 int pos[maxn],times[maxn]; 9 int main() 10 { 11 //freopen("de.txt","r",stdin); 12 while (~scanf("%d",&n)){ 13 memset(pos,-1,sizeof pos); 14 for (int i=0;i<n;++i){ 15 scanf("%d",&a[i]); 16 } 17 int sz = (int) sqrt(n); 18 for (int i=n-1;i>=0;--i){//从后向前初始化pos,times 19 int tmp = a[i]+i; 20 if (tmp>n) 21 pos[i]=-1,times[i]=1;//直接跳出边界 22 else if (tmp>=(i/sz+1)*sz) 23 pos[i] = tmp,times[i]=1;//跳出了当前块 24 else 25 pos[i] = pos[tmp],times[i]=times[tmp]+1;//跳到了块内的另一个点 26 } 27 scanf("%d",&m); 28 for (int i=0;i<m;++i){ 29 scanf("%d",&q); 30 if (q==1){ 31 int x; 32 int ans = 0; 33 scanf("%d",&x); 34 for (int j=x;j!=-1;j=pos[j]){ 35 ans+=times[j]; 36 } 37 printf("%d\\n",ans); 38 } 39 else{ 40 int x,y; 41 scanf("%d%d",&x,&y); 42 a[x]=y; 43 for (int j=x;j>=x/sz*sz;j--){//更新只更新x所在块里面位于x之前的块跟x自己 44 int tmp=j+a[j]; 45 if (tmp>=n) 46 pos[j]=-1,times[j]=1; 47 else if (tmp>=(j/sz+1)*sz) 48 pos[j]=tmp,times[j]=1; 49 else 50 pos[j]=pos[tmp],times[j] = times[tmp]+1; 51 } 52 } 53 } 54 } 55 return 0; 56 }
以上是关于bzoj 2002[Hnoi2010]Bounce 弹飞绵羊(分治分块)的主要内容,如果未能解决你的问题,请参考以下文章
bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)
bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊
BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊
[BZOJ 2002][Luogu 3203][HNOI2010]Bounce 弹飞绵羊