一直都说学莫队,直到现在才学,训练的时候就跪了   T_T,其实挺简单的感觉。其实训练的时候也看懂了,一知半解,就想着先敲。(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯)

前缀异或也很快想出来,结果没弄好边界,也是对前缀异或和莫队的不熟练。

CF 的E题,给定区间中有多少子区间个数异或等于k

容易想到的是预处理前缀异或值,求解区间[L, R]的贡献,相当于在前缀异或值[L - 1, R]中任取两个数,异或值等于k

知道区间[L, R]的贡献,可以O(1)知道[L - 1, R]和[L, R + 1]的贡献,就可以用莫队了

把询问分块,每块大小sqrtn,然后块内按右端点排序,然后two pointer维护即可。

因为块内的大小是sqrtn,然后每次移动只会移动sqrtn的大小。复杂度是nsqrtn

两题都是莫队的一个应用,离线查询区间

  1. #include <bits/stdc++.h>
  2. #define IOS ios::sync_with_stdio(false)
  3. using namespace std;
  4. #define inf (0x3f3f3f3f)
  5. typedef long long int LL;
  6. const int maxn = + ;
  7. struct Query {
  8. int L, R, id;
  9. }node[maxn];
  10. int a[maxn];
  11. int n, m, k, magic;
  12. bool cmp(struct Query a, struct Query b) {
  13. if (a.L/magic != b.L/magic) return a.L/magic < b.L/magic;
  14. else return a.R < b.R;
  15. }
  16. LL ans[maxn];
  17. LL num[maxn];
  18. void calc() {
  19. LL temp = ;
  20. int L = , R = ;
  21. num[] = ;
  22. for (int i = ; i <= m; ++i) {
  23. while (R < node[i].R) {
  24. ++R;
  25. temp += num[a[R] ^ k];
  26. num[a[R]]++;
  27. }
  28. while (R > node[i].R) { // differ sqrt
  29. num[a[R]]--;
  30. temp -= num[a[R] ^ k];
  31. --R;
  32. }
  33. while (L < node[i].L) {
  34. num[a[L - ]]--;
  35. temp -= num[a[L - ] ^ k];
  36. ++L;
  37. }
  38. while (L > node[i].L) {
  39. --L;
  40. temp += num[a[L - ] ^ k];
  41. num[a[L - ]]++;
  42. }
  43. ans[node[i].id] = temp;
  44. }
  45. }
  46. void work() {
  47. scanf("%d%d%d", &n, &m, &k);
  48. for (int i = ; i <= n; ++i) {
  49. scanf("%d", a + i);
  50. a[i] ^= a[i - ];
  51. // printf("%d ", a[i]);
  52. }
  53. magic = (int)sqrt(n);
  54. for (int i = ; i <= m; ++i) {
  55. scanf("%d%d", &node[i].L, &node[i].R);
  56. node[i].id = i;
  57. }
  58. sort(node + , node + + m, cmp);
  59. calc();
  60. for (int i = ; i <= m; ++i) {
  61. cout << ans[i] << endl;
  62. }
  63. }
  64.  
  65. int main() {
  66. #ifdef local
  67. freopen("data.txt", "r", stdin);
  68. // freopen("data.txt", "w", stdout);
  69. #endif
  70. work();
  71. return ;
  72. }
  1. #include <bits/stdc++.h>
  2. #define IOS ios::sync_with_stdio(false)
  3. using namespace std;
  4. #define inf (0x3f3f3f3f)
  5. typedef long long int LL;
  6. const int maxn = 5e5 + ;
  7. struct Query {
  8. int L, R, id;
  9. LL a, b;
  10. void init() {
  11. if (a != ) {
  12. LL t = __gcd(a, b);
  13. a /= t, b /= t;
  14. } else b = ;
  15. }
  16. }node[maxn], ans[maxn];
  17. int n, m, magic;
  18. int a[maxn];
  19. LL num[maxn];
  20. bool cmp(struct Query a, struct Query b) {
  21. if (a.L/magic != b.L/magic) return a.L/magic < b.L/magic;
  22. else return a.R < b.R;
  23. }
  24. void work() {
  25. scanf("%d%d", &n, &m);
  26. for (int i = ; i <= n; ++i) {
  27. scanf("%d", a + i);
  28. }
  29. for (int i = ; i <= m; ++i) {
  30. scanf("%d%d", &node[i].L, &node[i].R);
  31. node[i].id = i;
  32. }
  33. magic = sqrt(n);
  34. sort(node + , node + + m, cmp);
  35. int L = , R = ;
  36. LL res = ;
  37. for (int i = ; i <= m; ++i) {
  38. while (R < node[i].R) {
  39. ++R;
  40. res -= num[a[R]] * num[a[R]] - num[a[R]];
  41. num[a[R]]++;
  42. res += num[a[R]] * num[a[R]] - num[a[R]];
  43. }
  44. while (R > node[i].R) { //不同块之间才会出现
  45. res -= num[a[R]] * num[a[R]] - num[a[R]];
  46. num[a[R]]--;
  47. res += num[a[R]] * num[a[R]] - num[a[R]];
  48. R--;
  49. }
  50. while (L < node[i].L) { //每个块之间只是按照R排序的
  51. res -= num[a[L]] * num[a[L]] - num[a[L]];
  52. num[a[L]]--;
  53. res += num[a[L]] * num[a[L]] - num[a[L]];
  54. L++;
  55. }
  56. while (L > node[i].L) {
  57. --L;
  58. res -= num[a[L]] * num[a[L]] - num[a[L]];
  59. num[a[L]]++;
  60. res += num[a[L]] * num[a[L]] - num[a[L]];
  61. }
  62. ans[node[i].id].a = res, ans[node[i].id].b = 1LL * (node[i].R - node[i].L + ) * (node[i].R - node[i].L);
  63. }
  64. for (int i = ; i <= m; ++i) {
  65. ans[i].init();
  66. printf("%lld/%lld\n", ans[i].a, ans[i].b);
  67. }
  68. }
  69.  
  70. int main() {
  71. #ifdef local
  72. freopen("data.txt", "r", stdin);
  73. // freopen("data.txt", "w", stdout);
  74. #endif
  75. work();
  76. return ;
  77. }

E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)的更多相关文章

  1. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  2. 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

  3. Bzoj 2038: [2009国家集训队]小Z的袜子(hose) 莫队,分块,暴力

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 5763  Solved: 2660[Subm ...

  4. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) ( 莫队 )

    莫队..先按sqrt(n)分块, 然后按块的顺序对询问排序, 同块就按右端点排序. 然后就按排序后的顺序暴力求解即可. 时间复杂度O(n1.5) --------------------------- ...

  5. BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题&&学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 9894  Solved: 4561[Subm ...

  6. 2038: [2009国家集训队]小Z的袜子(hose) (莫队算法)

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 专题练习: http://acm.hust.edu.cn/vjudge/conte ...

  7. 莫队算法 BOJ 2038 [2009国家集训队]小Z的袜子(hose)

    题目传送门 /* 莫队算法:求出[l, r]上取出两只相同袜子的个数. 莫队算法是离线处理一类区间不修改查询类问题的算法.如果你知道了[L,R]的答案,可以在O(1)的时间下得到 [L,R-1]和[L ...

  8. BZOJ 2038: [2009国家集训队]小Z的袜子(hose)

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7676  Solved: 3509[Subm ...

  9. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 分块

    分块大法好 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MB Submit: 2938  Solved: 13 ...

随机推荐

  1. Python-连接Redis并操作

    首先开启redis的外连 sch01ar@ubuntu:~$ sudo vim /etc/redis/redis.conf 把bind 127.0.0.1这行注释掉 然后重启redis sudo /e ...

  2. linux日常管理-sar工具

    查看网卡瓶颈 查看网卡流量 默认10分钟一次 查看实时流量  每秒钟显示一次 显示5次 网卡有 lo eth0   主要看eth0外网  rxbyt/s 进网口和 txbyt/s出网口 带宽看txby ...

  3. 阿里巴巴Druid数据库连接池的使用

    准备: 创建一个基于SpringBoot的web项目 1 引入相关依赖 jpa.mysql.druid <?xml version="1.0" encoding=" ...

  4. 杭电acm 1022题

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  5. web特点

    1.图形化和易于导航的 Web是非常易于导航的,只需要从一个连接跳到另一个连接,就可以在各页各站点之间进行浏览了. 2.与平台无关 这里所说的平台是指软件的运行环境,可以是Windows.Linux等 ...

  6. Win7环境下Sublime Text 3下安装NodeJS插件

    1.首先下载安装Node.JS,配置好环境变量(安装好Node.JS默认是配置好了环境变量的). 2.Sublime Text 3下安装NodeJS插件. 参考的两篇文章:http://www.cnb ...

  7. java File基本操作,以及递归遍历文件夹

    java 的文件操作,相对来说是比较重要的,无论是编写CS还是BS程序,都避免不了要与文件打交道,例如读写配置文件等.虽然现在很多框架都直接帮你做好了这一步! java.io.File 底层是调用与c ...

  8. Codeforces Round #279 (Div. 2) C. Hacking Cypher (大数取余)

    题目链接 C. Hacking Cyphertime limit per test1 secondmemory limit per test256 megabytesinputstandard inp ...

  9. [CentOS7] ssh免密登录 scp免密传输

    我们采用RSA非对称加密算法,原理: 如果,A要和B通讯,则: (1). A通过RSA算法生成公钥(.pub)和私钥(公钥用于加密,私钥用于解密) (2). B将A的公钥文件(.pub)内容加入到au ...

  10. java模拟简易按键精灵

    很多小伙伴们都有过抢课的经历,有时候抢不到自己想上的课,只能盼望有人退选,可是很多时候别人退选了,但是很快又被别人抢走了,我们不可能时刻盯着电脑, 这时候如果有一个抢课的程序岂不是很棒.. 出于这个目 ...