[HNOI2010]弹飞绵羊

题目大意:
  给你一个长度为$n(nleq200000)$的正整数数列$k$。数列中按照如下方式进行位置跳转,若当前所在的位置为$i$,则下一步跳转到$i+k_i$的位置上。要求进行如下两种操作:
  1.将$k_x$的值修改为$y$;
  2.询问从$k_x$出发,需要几步跳出整个数列?

思路:
  分块,记录每个位置跳出当前块所需的步数和跳出块后所到达的位置。每次修改暴力修改整个块,复杂度$O(sqrt n)$。询问时暴力往后跳,复杂度$O(sqrt n)$。总时间复杂度$O(n+msqrt n)$。

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cctype>
 4 inline int getint() {
 5     register char ch;
 6     while(!isdigit(ch=getchar()));
 7     register int x=ch^'0';
 8     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
 9     return x;
10 }
11 const int N=200000;
12 int n,k[N],bel[N],begin[N],end[N],step[N],to[N];
13 inline int query(int x) {
14     int ret=0;
15     while(x<n) {
16         ret+=step[x];
17         x=to[x];
18     }
19     return ret;
20 }
21 inline void update(const int &b,const int &e) {
22     for(register int i=e;i>=b;i--) {
23         if(i+k[i]>end[bel[i]]) {
24             step[i]=1;
25             to[i]=i+k[i];
26         } else {
27             step[i]=step[i+k[i]]+1;
28             to[i]=to[i+k[i]];
29         }
30     }
31 }
32 int main() {
33     const int block=sqrt(n=getint());
34     for(register int i=0;i<n;i++) {
35         k[i]=getint();
36         bel[i]=i/block;
37         if(bel[i]&&!begin[bel[i]]) begin[bel[i]]=i;
38         end[bel[i]]=i;
39     }
40     update(0,n-1);
41     for(register int i=getint();i;i--) {
42         const int opt=getint(),x=getint();
43         if(opt==1) {
44             printf("%d
",query(x));
45         } else {
46             k[x]=getint();
47             update(begin[bel[x]],x);
48         }
49     }
50     return 0;
51 }
原文地址:https://www.cnblogs.com/skylee03/p/8456971.html