任意门:http://codeforces.com/problemset/problem/617/E

E. XOR and Favorite Number

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k.

Input

The first line of the input contains integers nm and k (1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 1 000 000) — the length of the array, the number of queries and Bob's favorite number respectively.

The second line contains n integers ai (0 ≤ ai ≤ 1 000 000) — Bob's array.

Then m lines follow. The i-th line contains integers li and ri (1 ≤ li ≤ ri ≤ n) — the parameters of the i-th query.

Output

Print m lines, answer the queries in the order they appear in the input.

Examples

input
  1. 6 2 3
    1 2 1 1 0 3
    1 6
    3 5
output
  1. 7
    0
input
  1. 5 3 1
    1 1 1 1 1
    1 5
    2 4
    1 3
output
  1. 9
    4
    4
Note

In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query.

In the second sample xor equals 1 for all subarrays of an odd length.

题目大意:

有一串长度为 N 的数列,M次查询 ( l,r )内有多少对 ( i, j )使得 ai ^ ai+1 ^ ... ^ aj = K;

解题思路:

这里的区间查询是离线的,可以用传说中的莫队算法(优雅而华丽的暴力算法)。

异或的题目有一个很巧妙的地方就是利用 a^b^a = b 这个性质。

这道题目我们也要预处理一下 sum(i) 前 i 个数的异或和, 那么 ai ^ ai+1 ^ ... ^ aj = sum( i - 1) ^ sum( j ) 了。

接下来我们就可以按照查询的区间用莫队算法遍历一遍,同时记录当前区间某个前缀和的出现次数;

根据 sum( i - 1) ^ sum( j ) = K ,K  ^ sum( i - 1 ) = sum( j ) || K ^ sum( j ) = sum( i - 1) ,由符合条件的前缀和次数来推出符合条件的( i,j )的个数啦。

Tip:

听了大神的课,这道题有两个坑:

1、答案的数据的数据范围是爆 int 的;

2、虽然是1e6 的 K,但异或的结果要大于 1e6 ;

AC code:

  1. #include <bits/stdc++.h>
  2. #define LL long long int
  3. using namespace std;
  4. const int MAXN = <<;
  5.  
  6. struct node
  7. {
  8. int l, r, id; //区间和查询编号
  9. }Q[MAXN]; //记录查询数据(离线)
  10.  
  11. int pos[MAXN]; //记录分块
  12. LL ans[MAXN]; //记录答案
  13. LL flag[MAXN]; //维护前缀异或和出现的次数
  14. int a[MAXN]; //原本数据
  15. int L=, R; //当前区间的左右结点
  16. LL res; //储存当前区间的值
  17. int N, M, K;
  18. bool cmp(node a, node b) //排序
  19. {
  20. if(pos[a.l]==pos[b.l]) return a.r < b.r; //只有左结点在同一分块才排右结点
  21. return pos[a.l] < pos[b.l]; //否则按照左结点分块排
  22. }
  23. void add(int x)
  24. {
  25. res+=flag[a[x]^K];
  26. flag[a[x]]++;
  27. }
  28. void del(int x)
  29. {
  30. flag[a[x]]--;
  31. res-=flag[a[x]^K];
  32. }
  33. int main()
  34. {
  35. scanf("%d%d%d", &N, &M, &K);
  36. int sz = sqrt(N);
  37. for(int i = ; i <= N; i++){ //读入数据
  38. scanf("%d", &a[i]);
  39. a[i] = a[i]^a[i-]; //计算前缀异或和
  40. pos[i] = i/sz; //分块
  41. }
  42. for(int i = ; i <= M; i++){ //读入查询
  43. scanf("%d%d", &Q[i].l, &Q[i].r);
  44. Q[i].id = i;
  45. }
  46. sort(Q+, Q++M, cmp);
  47. flag[] = ;
  48. for(int i = ; i <= M; i++){
  49. while(L < Q[i].l){ //当前左结点比查询结点小
  50. del(L-);
  51. L++;
  52. }
  53. while(L > Q[i].l){ //当前左结点比查询左结点大
  54. L--;
  55. add(L-);
  56. }
  57. while(R < Q[i].r){ //当前右结点比查询右结点小
  58. R++;
  59. add(R);
  60. }
  61. while(R > Q[i].r){ //当前右节点比查询右节点大
  62. del(R);
  63. R--;
  64. }
  65. ans[Q[i].id] = res;
  66. }
  67. for(int i = ; i <= M; i++){
  68. printf("%lld\n", ans[i]);
  69. }
  70. }

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】的更多相关文章

  1. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

    E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...

  2. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number —— 莫队算法

    题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...

  3. Codeforces Round #340 (Div. 2) E XOR and Favorite Number 莫队板子

    #include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...

  4. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number (莫队)

    题目链接:http://codeforces.com/contest/617/problem/E 题目大意:有n个数和m次查询,每次查询区间[l, r]问满足ai ^ ai+1 ^ ... ^ aj ...

  5. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. CodeForces - 617E XOR and Favorite Number 莫队算法

    https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...

  7. codeforces 617E E. XOR and Favorite Number(莫队算法)

    题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...

  8. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  9. [Codeforces Round #340 (Div. 2)]

    [Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...

随机推荐

  1. oracle trim不掉空白字符分享(转)

    本文转自:http://www.2cto.com/database/201306/223558.html 问题背景:一个工商注册号,正常的用trim能解决的问题,但是这个case,trim后和肉眼看到 ...

  2. POST 还是 GET?

    POST 还是 GET? 浏览器使用 method 属性设置的方法将表单中的数据传送给服务器进行处理.共有两种方法:POST 方法和 GET 方法. 如果采用 POST 方法,浏览器将会按照下面两步来 ...

  3. [转]MVC系列——MVC源码学习:打造自己的MVC框架(一:核心原理)

    本文转自:http://www.cnblogs.com/landeanfen/p/5989092.html 阅读目录 一.MVC原理解析 1.MVC原理 二.HttpHandler 1.HttpHan ...

  4. java连接sql server数据库

    1.新建项目,导入包  sqljdbc4.jar或sqljdbc.jar(jdk1.7版本) 2.新建类文件ConnectionDB.java package hello; import java.s ...

  5. OpenLayers 3 给features 添加手势

    map.on('pointermove',function(e){ var pixel = map.getEventPixel(e.originalEvent); var hit = map.hasF ...

  6. WinSock Socket 池

    之前在WinSock2.0 API 中说到,像DisConnectEx 函数这样,它具有回收SOCKET的功能,而像AcceptEx这样的函数,它不会自己在内部创建新的SOCKET,需要外部传入SOC ...

  7. jquery toggle(listenerOdd, listenerEven)

    1. example: <!DOCTYPE HTML><html>    <head></head>    <body>        &l ...

  8. Python爬虫之requests模块(2)

    一.今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 二.回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 三. ...

  9. android 调试卡在:Waiting for Debugger - Application XXX is waiting for the debugger to Attach" 解决方法

    解决方法:重启adb. 步骤:cmd进入命令行,进入adb所在目录先后执行adb kill-server,adb start-server.

  10. 【Linux】文本编辑器Vim常用操作入门

    Linux常用文本编辑器:Vi & Eamcs Vim -- Vi的升级版本 Vim 一.3种工作模式 命令行模式 (Command Mode) 插入模式 (Insert Mode) -- 键 ...