传送门

只有第一个,第二个权限题。

分块,然而wa,没看出来错在哪里,有时间再看。

  1. #include <cmath>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. const int N = , M = ;
  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 = , f = ;
  14. char ch = getchar();
  15. for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
  16. for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
  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) >> ;
  31. if(c[mid] >= z) y = mid;
  32. else x = mid + ;
  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] + );
  42. }
  43.  
  44. inline void init()
  45. {
  46. int i, j;
  47. n = read();
  48. m = read();
  49. S = sqrt(n);
  50. for(i = ; i <= n; i++)
  51. {
  52. a[i] = read();
  53. pre[i] = last[a[i]];
  54. last[a[i]] = i;
  55. }
  56. for(i = ; i <= n; i += S)
  57. st[++C] = i, ed[C] = min(i + S - , n);
  58. for(i = ; 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 = ;
  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 + ; i <= r - ; i++) ans += find(st[i], ed[i] + , 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 = ; i <= n; i++) last[a[i]] = ;
  86. a[x] = d;
  87. for(i = ; 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[];
  100. for(i = ; i <= m; i++)
  101. {
  102. scanf("%s", ch);
  103. x = read();
  104. y = read();
  105. if(ch[] == '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 ;
  115. }

莫队

比分块不知道高到哪里去了。

带修改之后真是。。。。

  1. #include <cmath>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. const int N = , M = 1e6 + ;
  7. int n, Q, S, cnt1, cnt2, cur, l = , r, now;
  8. int a[N], t[N], belong[N], ans[N], c[M];
  9.  
  10. struct ovo
  11. {
  12. int l, r, tim, id;
  13. ovo(int l = , int r = , int tim = , int id = ) : l(l), r(r), tim(tim), id(id) {}
  14. }q[N];
  15. struct qwq
  16. {
  17. int p, v, last;
  18. qwq(int p = , int v = , int last = ) : p(p), v(v), last(last) {}
  19. }cq[N];
  20.  
  21. inline int read()
  22. {
  23. int x = , f = ;
  24. char ch = getchar();
  25. for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
  26. for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
  27. return x * f;
  28. }
  29.  
  30. inline bool cmp(ovo x, ovo y)
  31. {
  32. return belong[x.l] == belong[y.l] ? (belong[x.r] == belong[y.r] ? x.tim < y.tim : belong[x.r] < belong[y.r]) : belong[x.l] < belong[y.l];
  33. }
  34.  
  35. inline void add(int x)
  36. {
  37. now += (++c[x]) == ;
  38. }
  39.  
  40. inline void del(int x)
  41. {
  42. now -= (--c[x]) == ;
  43. }
  44.  
  45. inline void update(int x, int d)
  46. {
  47. if(l <= x && x <= r) add(d), del(a[x]);
  48. a[x] = d;
  49. }
  50.  
  51. int main()
  52. {
  53. int i, x, y;
  54. char ch[];
  55. n = read();
  56. Q = read();
  57. S = sqrt(n);
  58. for(i = ; i <= n; i++) a[i] = t[i] = read(), belong[i] = i / S + ;
  59. for(i = ; i <= Q; i++)
  60. {
  61. scanf("%s", ch);
  62. x = read();
  63. y = read();
  64. if(ch[] == 'Q') q[++cnt1] = ovo(x, y, cnt2, cnt1);
  65. else cq[++cnt2] = qwq(x, y, t[x]), t[x] = y;
  66. }
  67. std::sort(q + , q + cnt1 + , cmp);
  68. for(i = ; i <= cnt1; i++)
  69. {
  70. while(cur < q[i].tim) cur++, update(cq[cur].p, cq[cur].v);
  71. while(cur > q[i].tim) update(cq[cur].p, cq[cur].last), cur--;
  72. while(l < q[i].l) del(a[l]), l++;
  73. while(l > q[i].l) l--, add(a[l]);
  74. while(r < q[i].r) r++, add(a[r]);
  75. while(r > q[i].r) del(a[r]), r--;
  76. ans[q[i].id] = now;
  77. }
  78. for(i = ; i <= cnt1; i++) printf("%d\n", ans[i]);
  79. return ;
  80. }

[BZOJ2120] 数颜色 && [bzoj2453] 维护队列(莫队 || 分块)的更多相关文章

  1. Bzoj 2120: 数颜色 && 2453: 维护队列 莫队,分块,bitset

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 2645  Solved: 1039[Submit][Status][Discuss] ...

  2. bzoj2120: 数颜色 &&bzoj2453: 维护队列

    题目大意: 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会依据个人喜好 ...

  3. BZOJ2120 数颜色 【带修莫队】

    BZOJ2120 数颜色 Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到 ...

  4. BZOJ2120 数颜色 【带修改莫队】

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MB Submit: 6579  Solved: 2625 [Submit][Status][Discus ...

  5. BZOJ2120 数颜色(带修改莫队)

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  6. 【BZOJ2120】数颜色(带修莫队)

    点此看题面 大致题意:告诉你\(n\)只蜡笔的颜色,有两种操作:第一种操作将第\(x\)只蜡笔颜色改成\(y\),第二种操作询问区间\([l,r]\)内有多少种颜色的蜡笔. 考虑普通莫队 这题目第一眼 ...

  7. 「洛谷1903」「BZOJ2120」「国家集训队」数颜色【带修莫队,树套树】

    题目链接 [BZOJ传送门] [洛谷传送门] 题目大意 单点修改,区间查询有多少种数字. 解法1--树套树 可以直接暴力树套树,我比较懒,不想写. 稍微口胡一下,可以直接来一个树状数组套主席树,也就是 ...

  8. BZOJ 2120 数颜色 【带修改莫队】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 2120: 数颜色 Time Limit: 6 Sec  Memory Limit: ...

  9. BZOJ 2120 数颜色(带修改莫队)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2120 [题目大意] 给出一颜色序列,每次可以修改一个位置的颜色或者询问一个区间不同颜色 ...

随机推荐

  1. oracle创建默认表空间---重要

    当oracle创建数据库后,sys创建用户时还要有默认表空间.不创建默认表空间在导如项目时会有些数据表导入不成功! 由于时间仓促以截屏为例  之后会在刚刚那个空文件生成一个文件 ----------- ...

  2. mysql机制总结

    Innodb和myisam最大的不同就是 innodb支持事物 采用了行锁 myisam 采用了表锁 默认就使用了表锁 表锁:速度快 并发小 发生锁冲突高 开销小 行锁:速度慢 并发高 发生锁冲突低 ...

  3. CSS实现居中的方式

    在介绍居中方式之前,简单介绍一下行内元素和块级元素. 行内元素 和其他元素都在同一行 高,行高及外边距和内边距部分可以改变 宽度只与内容有关 行内元素只能容纳文本或者其他行内元素 常用内联元素:a,i ...

  4. laravel学习一

    Laravel 是一款简洁,优雅的一款框架,可以说是入门TP后的第二款可以选择的框架. 目录部分: app -> 自己写的代码 http -> Controller -> 控制器 b ...

  5. 如何在Eclipse或者Myeclipse中使用tomcat(配置tomcat,发布web项目)?(图文详解)(很实用)

    前期博客 Eclipse里的Java EE视图在哪里?MyEclipse里的Java EE视图在哪里?MyEclipse里的MyEclipse Java Enterprise视图在哪里?(图文详解) ...

  6. ajax无限循环

    // 猜你喜欢的无限加载 (function(){ var content = document.getElementsByClassName("content")[0]; var ...

  7. Elasticsearch--建议器

    目录 可用的建议器类型 term建议器 term建议器的配置选项 phrase建议器 completion建议器 在考虑性能的情况下,允许用户的拼写错误,以及构建一个自动完成功能 可用的建议器类型 t ...

  8. IBatis的分页研究

    IBatis的分页研究 博客分类: Ibatis学习   摘自: http://cpu.iteye.com/blog/311395 yangtingkun   Oracle分页查询语句 ibaits. ...

  9. Ubuntu-Python2.7安装 scipy,numpy,matplotlib

    sudo apt-get install python-scipy sudo apt-get install python-numpy sudo apt-get install python-matp ...

  10. C++ 程序的编译

    一.编译器都具备集成开发环境(Integrated Developed Environment,IDE) 二.程序源文件命名约定: C++ 的后缀一般是 .cpp .cc .C .cpp .cxx 三 ...