E. XOR and Favorite Number

题目连接:

http://www.codeforces.com/contest/617/problem/E

Descriptionww.co

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 n, m 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.

Sample Input

6 2 3

1 2 1 1 0 3

1 6

3 5

Sample Output

7

0

Hint

题意

给你n个数,然后M次询问,问你l,r区间内有多少对数,使得a[i]^a[j] = k

题解:

无修改,而且可以知道[l,r]可以O(1)就出[l-1,r],[l,r+1],[l+1,r],[l,r-1]的数据的

所有很显然的莫队算法搞一搞就好了

直接大暴力,注意不能再带log,所以直接开数组存就好了

注意,数组得开大一点哦

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 120010;
  4. int a[maxn],pos[maxn];
  5. long long ans,flag[5000000];
  6. long long Ans[maxn];
  7. int k;
  8. struct query
  9. {
  10. int l,r,id;
  11. }Q[maxn];
  12. bool cmp(query a,query b)
  13. {
  14. if(pos[a.l]==pos[b.l])
  15. return a.r<b.r;
  16. return pos[a.l]<pos[b.l];
  17. }
  18. void Updata(int x)
  19. {
  20. ans+=flag[a[x]^k];
  21. flag[a[x]]++;
  22. }
  23. void Delete(int x)
  24. {
  25. flag[a[x]]--;
  26. ans-=flag[a[x]^k];
  27. }
  28. int main()
  29. {
  30. int n,m;
  31. scanf("%d%d%d",&n,&m,&k);
  32. int sz =ceil(sqrt(1.0*n));
  33. for(int i=1;i<=n;i++)
  34. {
  35. scanf("%d",&a[i]);
  36. pos[i]=(i-1)/sz;
  37. }
  38. for(int i=1;i<=n;i++)
  39. a[i]^=a[i-1];
  40. for(int i=1;i<=m;i++)
  41. {
  42. scanf("%d%d",&Q[i].l,&Q[i].r);
  43. Q[i].id = i;
  44. }
  45. sort(Q+1,Q+1+m,cmp);
  46. int l = 1,r = 0;
  47. ans=0;
  48. flag[0]=1;
  49. for(int i=1;i<=m;i++)
  50. {
  51. int id = Q[i].id;
  52. while(r<Q[i].r)
  53. {
  54. r++;
  55. Updata(r);
  56. }
  57. while(l>Q[i].l)
  58. {
  59. l--;
  60. Updata(l-1);
  61. }
  62. while(r>Q[i].r)
  63. {
  64. Delete(r);
  65. r--;
  66. }
  67. while(l<Q[i].l)
  68. {
  69. Delete(l-1);
  70. l++;
  71. }
  72. Ans[id]=ans;
  73. }
  74. for(int i=1;i<=m;i++)
  75. printf("%lld\n",Ans[i]);
  76. }

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  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 E. XOR and Favorite Number(莫队算法)

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

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

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

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

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

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

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

随机推荐

  1. python27+django数据库配置常见问题

    mysql缺乏模块,需要安装,建议去http://sourceforge.net/projects/mysql-python/files/mysql-python/下源码编译,或者安装msi文件htt ...

  2. cocos2d-x 全面总结--字体描边和制作阴影

    关于字体描边的实现,不考虑效果和效率的话,是有三种方式: ① 利用CCLabelTTF制作文字描边和阴影效果 ② 利用CCRenderTexture渲染文理的方式生成带有描边效果的文字 ③ 利用sha ...

  3. PermGen space Eclipse 终极解决方案

    1.选中项目右键 run or debug configurations... 2.在 VM arguments 加入  -Xms128m -Xmx512m -XX:PermSize=64M -XX: ...

  4. Quartz与Spring集成

    关于Quartz的基本知识,这里就不再多说,可以参考Quartz的example. 这里主要要说的是,个人在Quartz和Spring集成的过程中,遇到的问题和个人理解. 首先来说说个人的理解: 1. ...

  5. enter mysql

    1, mysql -u database username -p 2, database password 3, use (database name) -> change database 4 ...

  6. Spark connect to Database

    Cannect to Cassandra: 用spark-cassandra-connector, 注意spark,cassandra和connector的版本要配套,Cassandra至少要版本2以 ...

  7. about云资源共享

      Nosql资源: http://www.aboutyun.com/thread-5655-1-1.html (1)redis安置(2)RedisAdminUI.zip(3)redis安装部署(4) ...

  8. HDU ACM 1515 Anagrams by Stack

    Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. spring 的properties解析

    一般使用PropertyPlaceholderConfigurer来替换占位符,例如: <bean class="org.springframework.beans.factory.c ...

  10. LightOJ 1245 Harmonic Number (II)(找规律)

    http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS    ...