只有第一个,第二个权限题。
分块,然而wa,没看出来错在哪里,有时间再看。
1 #include <cmath> 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 6 const int N = 10001, M = 1000001; 7 8 int n, m, S, C; 9 int a[N], st[N], ed[N], belong[N], pre[N], last[M], c[N]; 10 11 inline int read() 12 { 13 int x = 0, f = 1; 14 char ch = getchar(); 15 for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1; 16 for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0'; 17 return x * f; 18 } 19 20 inline int min(int x, int y) 21 { 22 return x < y ? x : y; 23 } 24 25 inline int find(int x, int y, int z) 26 { 27 int mid, l = x; 28 while(x < y) 29 { 30 mid = (x + y) >> 1; 31 if(c[mid] >= z) y = mid; 32 else x = mid + 1; 33 } 34 return x - l; 35 } 36 37 inline void reset(int x) 38 { 39 int i; 40 for(i = st[x]; i <= ed[x]; i++) c[i] = pre[i]; 41 std::sort(c + st[x], c + ed[x] + 1); 42 } 43 44 inline void init() 45 { 46 int i, j; 47 n = read(); 48 m = read(); 49 S = sqrt(n); 50 for(i = 1; i <= n; i++) 51 { 52 a[i] = read(); 53 pre[i] = last[a[i]]; 54 last[a[i]] = i; 55 } 56 for(i = 1; i <= n; i += S) 57 st[++C] = i, ed[C] = min(i + S - 1, n); 58 for(i = 1; i <= C; i++) 59 { 60 reset(i); 61 for(j = st[i]; j <= ed[i]; j++) belong[j] = i; 62 } 63 } 64 65 inline int query(int x, int y) 66 { 67 int i, l = belong[x], r = belong[y], ans = 0; 68 if(l == r) 69 { 70 for(i = x; i <= y; i++) if(c[i] < x) ans++; 71 return ans; 72 //return std::lower_bound(c + x, c + y + 1, x) - c - x; 73 } 74 //ans += std::lower_bound(c + x, c + ed[l] + 1, x) - c - x; 75 for(i = x; i <= ed[l]; i++) if(c[i] < x) ans++; 76 for(i = l + 1; i <= r - 1; i++) ans += find(st[i], ed[i] + 1, x); 77 //ans += std::lower_bound(c + st[r], c + y + 1, x) - c - st[r]; 78 for(i = st[r]; i <= y; i++) if(c[i] < x) ans++; 79 return ans; 80 } 81 82 inline void update(int x, int d) 83 { 84 int i, t; 85 for(i = 1; i <= n; i++) last[a[i]] = 0; 86 a[x] = d; 87 for(i = 1; i <= n; i++) 88 { 89 t = pre[i]; 90 pre[i] = last[a[i]]; 91 if(t ^ pre[i]) reset(belong[i]); 92 last[a[i]] = i; 93 } 94 } 95 96 inline void work() 97 { 98 int i, x, y; 99 char ch[5]; 100 for(i = 1; i <= m; i++) 101 { 102 scanf("%s", ch); 103 x = read(); 104 y = read(); 105 if(ch[0] == 'Q') printf("%d\n", query(x, y)); 106 else update(x, y); 107 } 108 } 109 110 int main() 111 { 112 init(); 113 work(); 114 return 0; 115 }