题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个。

莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减少对答案的贡献。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. using namespace std;
  6. #define MAXN (1<<20)
  7.  
  8. int block;
  9. struct Query{
  10. int l,r,i;
  11. bool operator<(const Query &q) const {
  12. if(l/block==q.l/block) return r<q.r;
  13. return l/block<q.l/block;
  14. }
  15. }query[];
  16.  
  17. int n,m,k,a[],sum[];
  18.  
  19. int cnt1[MAXN],cnt2[MAXN];
  20. long long ans;
  21. void left_insert(int i){
  22. ++cnt1[sum[i-]];
  23. ++cnt2[sum[i]];
  24. ans+=cnt2[sum[i-]^k];
  25. }
  26. void left_remove(int i){
  27. ans-=cnt2[sum[i-]^k];
  28. --cnt1[sum[i-]];
  29. --cnt2[sum[i]];
  30. }
  31. void right_insert(int i){
  32. ++cnt1[sum[i-]];
  33. ++cnt2[sum[i]];
  34. ans+=cnt1[sum[i]^k];
  35. }
  36. void right_remove(int i){
  37. ans-=cnt1[sum[i]^k];
  38. --cnt1[sum[i-]];
  39. --cnt2[sum[i]];
  40. }
  41.  
  42. long long res[];
  43.  
  44. int main(){
  45. scanf("%d%d%d",&n,&m,&k);
  46. for(int i=; i<=n; ++i){
  47. scanf("%d",a+i);
  48. sum[i]=sum[i-]^a[i];
  49. }
  50. for(int i=; i<m; ++i){
  51. scanf("%d%d",&query[i].l,&query[i].r);
  52. query[i].i=i;
  53. }
  54.  
  55. block=(int)(sqrt(n)+1e-);
  56. sort(query,query+m);
  57.  
  58. int l=,r=;
  59. right_insert();
  60. for(int i=; i<m; ++i){
  61. while(l<query[i].l) left_remove(l++);
  62. while(l>query[i].l) left_insert(--l);
  63. while(r<query[i].r) right_insert(++r);
  64. while(r>query[i].r) right_remove(r--);
  65. res[query[i].i]=ans;
  66. }
  67.  
  68. for(int i=; i<m; ++i){
  69. printf("%lld\n",res[i]);
  70. }
  71. return ;
  72. }

Codeforces 617E XOR and Favorite Number(莫队算法)的更多相关文章

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

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

  2. Codeforces 617E XOR and Favorite Number莫队

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

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

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

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

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

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

  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 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. CodeForces 617E XOR and Favorite Number

    莫队算法. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...

随机推荐

  1. 项目差异class文件提取-->上线用

    package fileReader; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStre ...

  2. UISplitViewController - iPad分屏视图控制器

    UISplitViewController - 分屏视图控制器 概述 UISplitViewController 是一个容器vc, 展示一个 master-detail(主-详(从))界面. 主视图改 ...

  3. ThinkPHP入门(二)

    smarty使用 smarty引入流程 1. 控制器IndexAction.class.php function index() $this -> display(); (父类Action的di ...

  4. Android Studio 配置

    Android配置:[转]原地址:http://www.cnblogs.com/smyhvae/p/4022844.html [开发环境] 物理机版本:Win7旗舰版(64位) Android Stu ...

  5. python中的monkey-patching

    这个技巧我很少用过. 但知道无防. 在运行时改变函数或类的行为, 一般用猴子补丁,原类,装饰器都可以实现. #!/usr/bin/env python # -*- coding: utf-8 -*- ...

  6. JAVA 使用线程的几种方式

    之前放在自己网站上的例子,因为网站关闭,已经找不到了,想用的时候,没有的话又重新翻书是很麻烦的事情.所以重新记录一下,以备将来查看. 第一种,让任务类继承Runable接口,然后将任务类对象放入Thr ...

  7. MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏

    我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...

  8. android 入门-android Studio 配置

    重要:sdk 最好先有一个版本 19版本.build-tools 19.1.0 extras 19.0和platforms android-19 1.下载android sdk 和jdk 并配置环境变 ...

  9. 【leetcode】Candy

    题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  10. [Outlook] 重新取得outlook中被禁止访问的文件

    摘要:接收到老大的邮件,邮件中带有jar包,导致我无法接收到这个文件,outlook2010中提示:outlook禁止访问不安全……,相信很多人都遇到过这个问题,以前也遇到过,总没去想着解决这个问题, ...