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
1 2 1 1
3
1 1
2 1 1
1 1
Sample Output
2
3
3
这个题给了10s,但是也不能乱搞.
我们现将弹簧进行分块,对于每个弹簧我们记录它跳出自己所在块的步数times跟跳出当前块的落点pos
这样我们对于每次更改就可以只更改x所在块中位于x之前的点,跟x自己,这样是每个询问是

然而对于每一个询问由于我们记录的pos是跳出当前块的落点,这样最多我们跳
块,所以也是


综合复杂度
写完这个题感觉分块真是优美
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 ",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 }