记录编号 574868 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [国家集训队2011]数颜色 最终得分 100
用户昵称 Gravatarlihaoze 是否通过 通过
代码语言 C++ 运行时间 0.223 s
提交时间 2022-08-26 20:49:03 内存使用 9.47 MiB
显示代码纯文本
  1. #include <bits/stdc++.h>
  2.  
  3. struct Q { int l, r, tim, id, ans; };
  4. struct C { int pos, Old, New; };
  5.  
  6. const int N = 50010, M = 1e6+10;
  7. int n, m, t, l = 1, r, T, cnt, tim, ans;
  8. int a[N], b[N], pos[N], now[N], color[M];
  9. Q q[N];
  10. C c[N];
  11.  
  12. bool cmp1(Q a, Q b) { return pos[a.l] == pos[b.l] ? (pos[a.r] == pos[b.r] ? a.tim < b.tim : a.r < b.r) : a.l < b.l; }
  13. bool cmp2(Q a, Q b) { return a.id < b.id; }
  14.  
  15. void revise(int col, int op) {
  16. color[col] += op;
  17. if (op > 0) ans += color[col] == 1;
  18. else ans -= color[col] == 0;
  19. }
  20.  
  21. void work(int pos, int col) {
  22. if (l <= pos && pos <= r)
  23. revise(col, 1), revise(a[pos], -1);
  24. a[pos] = col;
  25. }
  26.  
  27. int main() {
  28. freopen("nt2011_color.in", "r", stdin);
  29. freopen("nt2011_color.out", "w", stdout);
  30. std::cin >> n >> m, t = std::pow(n, 0.6666666);
  31. for (int i = 1; i <= n; ++ i)
  32. std::cin >> a[i], now[i] = a[i], pos[i] = (i - 1) / t + 1;
  33. while (m --) {
  34. char op; int x, y;
  35. std::cin >> op >> x >> y;
  36. if (op == 'Q')
  37. q[++ cnt] = (Q) {x, y, tim, cnt};
  38. else
  39. c[++ tim] = (C) {x, now[x], y}, now[x] = y;
  40. }
  41. std::sort(q + 1, q + 1 + cnt, cmp1);
  42. for (int i = 1; i <= cnt; ++ i) {
  43. while (T < q[i].tim)
  44. ++ T, work(c[T].pos, c[T].New);
  45. while (T > q[i].tim)
  46. work(c[T].pos, c[T].Old), -- T;
  47. while (l < q[i].l) revise(a[l ++], -1);
  48. while (l > q[i].l) revise(a[-- l], 1);
  49. while (r < q[i].r) revise(a[++ r], 1);
  50. while (r > q[i].r) revise(a[r --], -1);
  51. q[i].ans = ans;
  52. }
  53. std::sort(q + 1, q + 1 + cnt, cmp2);
  54. for (int i = 1; i <= cnt; ++ i)
  55. std::cout << q[i].ans << '\n';
  56. return 0;
  57. }