题目链接:

  CodeForces 617 E. XOR and Favorite Number

题目描述:

  给出n个数,m次查询,每次查询在区间[l, r]里面有多少对(i, j),满足a^ ai+1 ^ ai+2 ^ ...... ^ aj-1 ^ aj == k

解题思路:

  莫队算法,在线性复杂度内进行转移,问题关键在与如何进行状态转移,我们设定 sum[i] 为[1, i]区间内的异或和,对于区间[a, b]的异或和为sum[b] ^ sum[a-1]。如果区间 [a, b] 的异或和为k,则有sum[b] ^ sum[a-1] == k,由于异或的性质可以推论出:sum[b] ^ k == sum[a-1],sum[a-1] ^ k == sum[b]。

 1 /**
 2     ans要用LL,
 3     num[]要在k的范围内扩大两倍
 4 */
 5 #include <math.h>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <iostream>
 9 #include <algorithm>
10 using namespace std;
11
12 typedef __int64 LL;
13 const int maxn = 100010;
14 struct node
15 {
16     int l, r, id;
17 } q[maxn];
18 LL ans[maxn];
19 int b[maxn], num[maxn*20];
20 int n, m, k, lb;
21
22 bool cmp (node x, node y)
23 {
24     if (x.l / lb == y.l / lb)
25         return x.r < y.r;
26     return x.l < y.l;
27 }
28 void solve ()
29 {
30     int l, r;
31     LL tmp;
32     l = r = tmp = 0;
33     num[0] = 1;
34
35     for (int i=0; i<m; i++)
36     {
37         while (r < q[i].r)
38         {
39             r ++;
40             tmp += num[b[r]^k];
41             num[b[r]] ++;
42         }
43
44         while (r > q[i].r)
45         {
46             num[b[r]] --;
47             tmp -= num[b[r]^k];
48             r --;
49         }
50
51         while (l < q[i].l - 1)
52         {
53             num[b[l]] --;
54             tmp -= num[b[l] ^ k];
55             l ++;
56         }
57
58         while (l > q[i].l - 1)
59         {
60             l --;
61             tmp += num[b[l] ^ k];
62             num[b[l]] ++;
63         }
64
65         ans[q[i].id] = tmp;
66     }
67 }
68
69 int main ()
70 {
71     while(scanf ("%d %d %d", &n, &m, &k) != EOF)
72     {
73         b[0] = 0;
74         lb = (int) sqrt (n);
75
76         for (int i=1; i<=n; i++)
77         {
78             scanf ("%d", &b[i]);
79             b[i] ^= b[i-1];
80         }
81
82         for (int i=0; i<m; i++)
83         {
84             scanf ("%d %d", &q[i].l, &q[i].r);
85             q[i].id = i;
86         }
87
88         sort (q, q+m, cmp);
89
90         solve();
91
92         for (int i=0; i<m; i++)
93             printf ("%I64d\n", ans[i]);
94     }
95     return 0;
96 }

转载于:https://www.cnblogs.com/alihenaixiao/p/5473194.html

CodeForces 617 E. XOR and Favorite Number相关推荐

  1. CF617E. XOR and Favorite Number

    1 /* 2 CF617E. XOR and Favorite Number 3 http://codeforces.com/contest/617/problem/E 4 莫队算法 5 题意:求l, ...

  2. CodeForces 1616H Keep XOR Low {a^b≤x} / CodeForces gym102331 Bitwise Xor {a^b≥x}(trie树 + 计数)

    文章目录 CodeForces 1616H Keep XOR Low problem solution code CodeForces gym102331 Bitwise Xor problem so ...

  3. CF617E XOR and Favorite Number

    CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...

  4. Codeforces617E XOR and Favorite Number (普通莫队)

    题目链接: XOR and Favorite Number 2021.11.11更新了解题思路和代码 大致题意 给定一个长度为nnn的序列aaa, 和一个固定的数值kkk. 有mmm次询问, 每次询问 ...

  5. 【CF 617E】 XOR and Favorite Number (Mo's algorithm)

    [CF 617E]  XOR and Favorite Number (Mo's algorithm) E. XOR and Favorite Number time limit per test 4 ...

  6. Codeforces 617E XOR and Favorite Number

    Discription Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Eac ...

  7. CodeForces - 617E XOR and Favorite Number (莫队+前缀和)

    Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...

  8. codeforces 617E XOR and Favorite Number 莫队

    https://vjudge.net/problem/CodeForces-617E 题目大意:给nnn个数,mmm个询问,以及一个数kkk,每次询问要输出[l,r][l,r][l,r]内满足a[i] ...

  9. CodeForces -617E XOR and Favorite Number(莫队)

    题目链接:点击这里 题目大意: 给定一个长度为 nnn 的序列 a1,a2,...,ana_1,a_2,...,a_na1​,a2​,...,an​ ,再给出一个数字 kkk , mmm 组询问每组询 ...

最新文章

  1. Python运行报错IndentationError: unindent does not match any outer indentation level
  2. 计算机视觉算法——Transformer学习笔记
  3. 迷宫收集星星 并查集解答
  4. 数模笔记_多变量最优化计算之牛顿法
  5. 2016年:勒索病毒造成损失预估超过10亿美元
  6. JAVA 文件下载乱码问题解决办法
  7. 【算法导论】简单哈希表的除法实现
  8. python怎么恢复默认窗口_如何恢复python编程环境spyder的窗口?
  9. JDK的Proxy技术实现AOP,InvocationHandler和Proxy详解——Spring AOP(三)
  10. linux scp 拷贝文件
  11. 通过阿里云容器镜像服务下载谷歌gcr.io镜像
  12. sql注入学习笔记1
  13. 相对位置编码 relative position encoding
  14. 大学计算机专业吐槽,大学专业吐槽集锦:分分钟让你感觉“前途无亮”!
  15. Genymotion模拟器及其相关虚拟机下载地址
  16. LANDESK操作系统部署时,怎么默认恢复到客户端C盘配置
  17. Java excel导出压缩zip并加密(拿起即用,有用请点赞,不做白嫖怪!)
  18. 基于MATLAB的FIR滤波器的设计及应用(图像去噪)
  19. 计算机学后感作文400,科技展观后感作文400字(精选7篇)
  20. java fft_java实现快速傅里叶变换(FFT)

热门文章

  1. python堆栈汉诺塔非递归_汉诺塔问题的递归解法和非递归解法(python语言实现)...
  2. go web框架_干货分享:六个知名的Go语言web框架
  3. 仅需10道题轻松掌握Python字符串方法 | Python技能树征题
  4. easymock教程_EasyMock教程
  5. nsarray数组越界_NSArray,NSMutableArray –目标C数组
  6. jsf如何与数据库连接_JSF身份验证登录注销数据库示例
  7. scala中命名参数函数_Scala中的命名参数和默认参数值
  8. C++开发工程师的薪资和未来发展
  9. 使用代码对现实世界进行抽象,软件设计和思维。
  10. Linux高可用负载均衡 集群理解