bzoj2002 弹飞绵羊
这种写法不太好,容易错
1 #include <iostream> 2 #include <string.h> 3 #include <cstdio> 4 #include <queue> 5 #include <vector> 6 #include <cstring> 7 #include <algorithm> 8 #include <math.h> 9 10 #define SIGMA_SIZE 26 11 #pragma warning ( disable : 4996 ) 12 13 using namespace std; 14 typedef long long LL; 15 //typedef unsigned long long uLL; 16 17 inline LL LMax(LL a,LL b) { return a>b?a:b; } 18 inline LL LMin(LL a,LL b) { return a>b?b:a; } 19 inline int Max(int a,int b) { return a>b?a:b; } 20 inline int Min(int a,int b) { return a>b?b:a; } 21 inline int gcd( int a, int b ) { return b==0?a:gcd(b,a%b); } 22 inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm 23 const long long INF = 0x3f3f3f3f3f3f3f3f; 24 const int inf = 0x3f3f3f3f; 25 const int mod = 7; 26 const int maxk = 5005; 27 const int maxn = 2e5+5; 28 29 int n, m; 30 int ki[maxn]; 31 int pos[maxn], times[maxn]; 32 33 int main() 34 { 35 cin >> n; 36 for ( int i = 0; i < n; i++ ) 37 scanf("%d", &ki[i]); 38 cin >> m; 39 40 int block = sqrt(n); 41 42 for ( int i = n - 1; i >= 0; i-- ) 43 { 44 int tmp = i+ki[i]; 45 if ( tmp >= n ) 46 { pos[i] = -1; times[i] = 1; } 47 else if ( tmp >= (i/block+1)*block ) 48 { pos[i] = tmp; times[i] = 1; } 49 else 50 { pos[i] = pos[tmp]; times[i] = times[tmp]+1; } 51 } 52 53 int x, y, k; 54 for ( int j = 0; j < m; j++ ) 55 { 56 scanf("%d", &x); 57 if ( x == 2 ) 58 { 59 scanf( "%d %d", &y, &k ); 60 ki[y] = k; 61 62 for ( int i = y; i >= (y / block)*block; i-- ) 63 { 64 int tmp = i+ki[i]; 65 if ( tmp >= n ) 66 { pos[i] = -1; times[i] = 1; } 67 else if ( tmp >= (i/block+1)*block ) 68 { pos[i] = tmp; times[i] = 1; } 69 else 70 { pos[i] = pos[tmp]; times[i] = times[tmp] + 1; } 71 } 72 } 73 else 74 { 75 scanf("%d", &y); 76 int ans = 0; 77 for ( int i = y; i+1; i = pos[i] ) 78 ans += times[i]; 79 printf( "%d ", ans ); 80 } 81 } 82 83 return 0; 84 }