bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 (分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=2002
题意+思路: 同codeforces 13 E holes.
/* ***********************************************
Author :111qqz
Created Time :2016年02月21日 星期日 02时29分39秒
File Name :code/bzoj/2002.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=2E5+7;
7int n,m;
8int siz = 450; //sqrt(2E5)
9int a[N];
10int pos[N];
11int cnt[N];
12int nxt[N];
13int end[N];
1void go( int x)
2{
3 int ans = 0 ;
4 while (1)
5 {
6 if (x>=n)
7 {
8 printf("%d\n",ans);
9 break;
10 }
11 ans +=cnt[x];
12 x = nxt[x];
13// cout<<"nxt[x]:"<<nxt[x]<<endl;
14 }
15}
16void update ( int i,int j)
17{
18 if (j>=n)
19 {
20 cnt[i]=1;
21 nxt[i]=n;
22 end[i]=i;
23 }
24 else
25 {
26 if (pos[i]==pos[j])
27 {
28 cnt[i] = cnt[j] + 1;
29 nxt[i] = nxt[j];
30 end[i]=end[j];
31 }
32 else
33 {
34 cnt[i] = 1;
35 nxt[i] = j;
36 end[i] = end[j];
37 }
38 }
39}
40int main()
41{
42 #ifndef ONLINE_JUDGE
43 freopen("code/in.txt","r",stdin);
44 #endif
1 scanf("%d",&n);
2 for ( int i = 0 ; i < n ;i++)
3 {
4 scanf("%d",&a[i]);
5 pos[i] = i/siz;
6 }
for ( int i = n - 1 ; i >= 0 ; i--) update(i,i+a[i]);
1 scanf("%d",&m);
2 while (m--)
3 {
4 int opt;
5 scanf("%d",&opt);
6 if (opt==1)
7 {
8 int x;
9 scanf("%d",&x);
10 go(x);
11 }
12 else
13 {
14 int x,y;
15 scanf("%d %d",&x,&y);
16 a[x]=y;
17 update(x,x+a[x]);
18 int p = pos[x]*siz;
19 for ( int i = x-1 ; i >= p ; i--) update(i,i+a[i]);
20 }
21 }
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}