莫队算法。

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. const int maxn=+;
  8. int a[maxn],pre[maxn];
  9. long long cnt[*maxn];
  10. int pos[maxn];
  11. int n,m,k;
  12. long long ans[maxn];
  13. long long Ans;
  14. int L,R;
  15. struct X
  16. {
  17. int l,r,id;
  18. }s[maxn];
  19.  
  20. bool cmp(const X&a,const X&b)
  21. {
  22. if(pos[a.l]==pos[b.l]) return a.r<b.r;
  23. return a.l<b.l;
  24. }
  25.  
  26. int main()
  27. {
  28. scanf("%d%d%d",&n,&m,&k);
  29.  
  30. int sz=sqrt(n);
  31. for(int i=;i<=n;i++)
  32. {
  33. scanf("%d",&a[i]);
  34. pre[i]=(pre[i-]^a[i]);
  35. pos[i]=i/sz;
  36. }
  37.  
  38. for(int i=;i<=m;i++)
  39. {
  40. scanf("%d%d",&s[i].l,&s[i].r);
  41. s[i].id=i;
  42. }
  43.  
  44. sort(s+,s++m,cmp);
  45. Ans=;
  46.  
  47. cnt[pre[s[].l-]]++;
  48.  
  49. for(int i=s[].l;i<=s[].r;i++)
  50. {
  51. Ans=Ans+cnt[pre[i]^k];
  52. cnt[pre[i]]++;
  53. }
  54.  
  55. L=s[].l; R=s[].r;
  56. ans[s[].id]=Ans;
  57.  
  58. for(int i=;i<=m;i++)
  59. {
  60. while(L<s[i].l)
  61. {
  62. cnt[pre[L-]]--;
  63. Ans=Ans-cnt[pre[L-]^k];
  64. L++;
  65. }
  66.  
  67. while(L>s[i].l)
  68. {
  69. L--;
  70. Ans=Ans+cnt[pre[L-]^k];
  71. cnt[pre[L-]]++;
  72. }
  73.  
  74. while(R<s[i].r)
  75. {
  76. R++;
  77. Ans=Ans+cnt[pre[R]^k];
  78. cnt[pre[R]]++;
  79. }
  80.  
  81. while(R>s[i].r)
  82. {
  83. cnt[pre[R]]--;
  84. Ans=Ans-cnt[pre[R]^k];
  85. R--;
  86. }
  87. ans[s[i].id]=Ans;
  88. }
  89.  
  90. for(int i=;i<=m;i++)
  91. printf("%lld\n",ans[i]);
  92.  
  93. return ;
  94. }

CodeForces 617E XOR and Favorite Number的更多相关文章

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

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

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

  3. Codeforces 617E XOR and Favorite Number莫队

    http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...

  4. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

  5. codeforces 617E. XOR and Favorite Number 莫队

    题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...

  6. [CQOI 2018]异或序列&[Codeforces 617E]XOR and Favorite Number

    Description 题库链接1 题库链接2 已知一个长度为 \(n\) 的整数数列 \(a_1,a_2,\cdots,a_n\) ,给定查询参数 \(l,r\) ,问在 \([l,r]\) 区间内 ...

  7. Codeforeces 617E XOR and Favorite Number(莫队+小技巧)

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

  8. CODEFORCES 340 XOR and Favorite Number 莫队模板题

    原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...

  9. XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和

    CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! =  j) ...

随机推荐

  1. oracle一次删除多张表

    通过拼接sql语句来完成 例如有如下个表 想一次性删除,执行如下语句: select 'drop table '||table_name ||';' as dropsql from USER_TABL ...

  2. java获取程序执行时间

    第一种是以毫秒为单位计算的. //伪代码 long startTime=System.currentTimeMillis(); //获取开始时间 doSomeThing(); //测试的代码段 lon ...

  3. Hibernate 系列教程11-继承-Single Table策略

    Single Table策略 通过 discriminator鉴别器来区分是父类还是子类 Employee public class Employee { private Long id; priva ...

  4. 配置App真机测试证书的流程 一览

    原文链接:http://www.jianshu.com/p/6b0de0d4c925 有开发者账号的前提下, 请进行如下步骤:1.首先登录网站:https://developer.apple.com. ...

  5. UVA 796 Critical Links (tarjan算法求割边)

    这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的-本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小. ...

  6. Python基础学习6---存储器

    Python提供一个标准的模块,称为 pickle .使用它你可以在一个文件中储存任何Python对象,之后你又可以把它完整无缺地取出来.这被称为 持久地 储存对象.还有另一个模块称为 cPickle ...

  7. JSP 语法/标签

    ┣1.declaration Declaration定义了JSP脚本语言使用的变量和函数,这类似于Java中定义全局变量,或可以把它想像成pascal编程语言中的单元文件的interface部分.声明 ...

  8. Mie散射 文献图片

    Technorati 标签: Mie Scattering,遥感,Remote Sensing

  9. robot_framewok自动化测试

    robot_framewok自动化测试 http://wenku.baidu.com/view/691abcaa4b73f242336c5fec.html 接口自动化测试框架设计 http://wen ...

  10. ssh能够连接而sftp不能连接的解决方法

    ssh能够连接而sftp不能连接的解决方法   昨天开始用FileZilla一直不能登录远程的服务器,ssh的登录就OK,因为是服务器,也不敢乱动.查了好多资料终于解决了. 首先,查看一下系统的安全日 ...