没有插入和删除操作...
所以还是很好写的,分块暴力就好了。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 using namespace std; 6 7 const int N = 400; 8 const int M = 11; 9 int b[N * N]; 10 int cnt[N][M][M]; 11 int w[M]; 12 int h[M]; 13 int n, m, ps; 14 15 void init() 16 { 17 w[0] = 1; 18 for ( int i = 1; i < M - 1; i++ ) 19 { 20 w[i] = w[i - 1] * 10; 21 } 22 } 23 24 int judge( int val, int d, int p ) 25 { 26 if ( val / w[d - 1] % 10 == p ) return 1; 27 return 0; 28 } 29 30 void update( int cur, int val, int f ) 31 { 32 for ( int i = 1; i < M; i++ ) 33 { 34 h[i] = val % 10; 35 val = val / 10; 36 } 37 for ( int i = 1; i < M; i++ ) 38 { 39 cnt[cur][i][h[i]] += f; 40 } 41 } 42 43 void modify( int pos, int val ) 44 { 45 int cur = pos / ps; 46 update( cur, b[pos], -1 ); 47 b[pos] = val; 48 update( cur, b[pos], 1 ); 49 } 50 51 int query( int l, int r, int d, int p ) 52 { 53 int cur = l / ps, ncur = r / ps; 54 int ans = 0; 55 for ( int i = cur + 1; i <= ncur - 1; i++ ) 56 { 57 ans += cnt[i][d][p]; 58 } 59 if ( cur != ncur ) 60 { 61 cur = ( cur + 1 ) * ps; 62 for ( int j = l; j < cur; j++ ) 63 { 64 ans += judge( b[j], d, p ); 65 } 66 ncur = ncur * ps; 67 for ( int j = ncur; j <= r; j++ ) 68 { 69 ans += judge( b[j], d, p ); 70 } 71 } 72 else 73 { 74 for ( int j = l; j <= r; j++ ) 75 { 76 ans += judge( b[j], d, p ); 77 } 78 } 79 return ans; 80 } 81 82 int main () 83 { 84 init(); 85 int t; 86 scanf("%d", &t); 87 while ( t-- ) 88 { 89 scanf("%d%d", &n, &m); 90 ps = 350; 91 memset( cnt, 0, sizeof(cnt) ); 92 for ( int i = 0; i < n; i++ ) 93 { 94 scanf("%d", b + i); 95 update( i / ps, b[i], 1 ); 96 } 97 while ( m-- ) 98 { 99 char op[2]; 100 scanf("%s", op); 101 if ( op[0] == ‘S‘ ) 102 { 103 int x, y; 104 scanf("%d%d", &x, &y); 105 x--; 106 modify( x, y ); 107 } 108 else 109 { 110 int l, r, d, p; 111 scanf("%d%d%d%d", &l, &r, &d, &p); 112 l--; 113 r--; 114 printf("%d\n", query( l, r, d, p )); 115 } 116 } 117 } 118 return 0; 119 }
原文:http://www.cnblogs.com/huoxiayu/p/4736965.html