题意:
给出n个节点,编号0~n-1。
1 把[x,y]颜色覆盖为z。
2 询问[x,y]颜色为z的有多少个。
题解:http://page.renren.com/601081183/note/867254911
懒得hash,直接用map搞。
把数列分成sqrt(n)段,如果每段在完整的查询区间内,可以O(1)得到答案,否则对该段暴力答案。
1 #include<cstdio> 2 #include<cmath> 3 #include<map> 4 #define EPS 1e-8 5 #define MAXM 331 6 #define MAXN 100010 7 using namespace std; 8 int col[MAXN], same[MAXM]; 9 map<int, int> mymap[MAXM]; 10 int n, block; 11 void Hash(int t) { 12 int nd, i; 13 mymap[t].clear(); 14 nd = min(t * block + block, n); 15 for (i = t * block; i < nd; i++) 16 mymap[t][col[i]]++; 17 } 18 void PushDown(int t) { 19 int i, nd; 20 nd = min(t * block + block, n); 21 if (same[t] >= 0) { 22 for (i = t * block; i < nd; i++) 23 col[i] = same[t]; 24 same[t] = -1; 25 } 26 } 27 void Update(int x, int y, int z) { 28 int t, i, nd; 29 t = x / block; 30 if (t == y / block) { 31 if (same[t] != z) { 32 PushDown(t); 33 for (i = x; i <= y; i++) 34 col[i] = z; 35 Hash(t); 36 } 37 } else { 38 if (x % block) { 39 if (same[t] != z) { 40 PushDown(t); 41 for (nd = min(t * block + block, n); x < nd; x++) 42 col[x] = z; 43 Hash(t); 44 } else 45 x = min(t * block + block, n); 46 } 47 if (y % block != block - 1) { 48 t = y / block; 49 if (same[t] != z) { 50 PushDown(t); 51 for (nd = t * block; y >= nd; y--) 52 col[y] = z; 53 Hash(t); 54 } else 55 y = t * block - 1; 56 } 57 for (t = x / block; x <= y; x += block, t++) 58 same[t] = z; 59 } 60 } 61 int Query(int x, int y, int z) { 62 int t, i, nd, ans; 63 t = x / block; 64 ans = 0; 65 if (t == y / block) { 66 if (same[t] == z) 67 ans += y - x + 1; 68 else if (same[t] == -1) { 69 for (i = x; i <= y; i++) { 70 if (col[i] == z) 71 ans++; 72 } 73 } 74 } else { 75 if (x % block) { 76 if (same[t] == z) { 77 ans += block - x % block; 78 x = min(t * block + block, n); 79 } else if (same[t] == -1) { 80 for (nd = min(t * block + block, n); x < nd; x++) { 81 if (col[x] == z) 82 ans++; 83 } 84 } else 85 x = min(t * block + block, n); 86 } 87 if (y % block != block - 1) { 88 t = y / block; 89 if (same[t] == z) { 90 ans += y % block + 1; 91 y = t * block - 1; 92 } else if (same[t] == -1) { 93 for (nd = t * block; y >= nd; y--) { 94 if (col[y] == z) 95 ans++; 96 } 97 } else 98 y = t * block - 1; 99 } 100 for (t = x / block; x <= y; x += block, t++) { 101 if (same[t] >= 0) { 102 if (same[t] == z) 103 ans += block; 104 } else { 105 if (mymap[t].count(z)) 106 ans += mymap[t][z]; 107 } 108 } 109 } 110 return ans; 111 } 112 int main() { 113 int q, i, j, cmd, x, y, z; 114 while (~scanf("%d%d", &n, &q)) { 115 block = (int) (ceil(sqrt((double) n)) + EPS); 116 for (i = 0; i < n; i++) 117 scanf("%d", &col[i]); 118 for (i = j = 0; i < n; i += block, j++) { 119 same[j] = -1; 120 Hash(j); 121 } 122 while (q--) { 123 scanf("%d%d%d%d", &cmd, &x, &y, &z); 124 if (cmd == 1) 125 Update(x, y, z); 126 else 127 printf("%d\n", Query(x, y, z)); 128 } 129 } 130 return 0; 131 }