Time Limit: 2000 ms   Memory Limit: 256 MB

Description

  给定一个含有n个整数的序列 a1, a2,..., an.

  定义 f(x,x) = a[x], f(x,y) = a[x] xor a[x + 1] xor ... xor a[y] (y > x).

  本题设有m组询问,每组询问含有两个参数 (l, r) 。对于每组询问,你需要回答有多少个二元组 (x, y) 满足 l <= x <= y <= r 并且 f(x, y) = k.

Input

  第一行有3个整数, n, m, k(1 <= n <= 100000, 1 <= m <= 100000, 0 <= k < 10^6).
  第二行共有 n 个非负整数代表整个序列,每个整数均不超过 10^6.
  接下来m行,每行两个整数 (li, ri), li <= ri

Output

  对于每组询问,输出一行表示有多少满足上述条件的二元组。

Sample Input

Sample Output

5 2 1
1 0 1 1 0
1 2
2 5

2

4

Hint 

  对于10%的数据,$n, m \leq 500$

  对于30%的数据,$n, m \leq 3000$

  对于50%的数据,$n, m \leq 30000$

  对于100%的数据,$n, m \leq 100000$


题解:

  一看范围就知道是标准$n\sqrt{n}$莫队啊,是我今天感冒脑抽了想不出这么简单的处理吗......

  

  $[l,r]$异或起来的值刚好等于$k$,维护异或前缀和$a$后,等价于判断$a_{l-1}\hat{} a_{r}$是否等于$k$。

  那么用莫队在这个异或前缀和数组上爬。

  维护莫队中统计每种值出现次数的数组$cnt$,$cnt_i$表示值为$i$的有多少。

  这样一来,加入一位$x$对莫队的影响就是$ans+=cnt_{a[x]\hat{} k}$,删去一位对莫队的影响就是$ans-=cnt_{a[x]\hat{} k}$

  当然还要维护$cnt$,加入时先统计影响,再将$cnt_{a[x]}++$;删除时先从$cnt$里抹掉:$cnt_{a[x]}--$,再统计影响。

Tips:

  1.原本$[l,r]$的询问,转换后最大要考虑到$[l-1,r]$,所以把询问的左端点都-1.

  2.异或后值可能大于1000000,需多开一倍。


 #include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=;
int n,m,k,di,a[N],cnt[];
ll now,out[N];
struct Query{
int l,r,id;
friend bool operator < (Query x,Query y){
x.l++; y.l++;
if(x.l/di!=y.l/di) return x.l/di<y.l/di;
return x.r<y.r;
}
}q[N];
void add(int x){
now+=cnt[a[x]^k];
cnt[a[x]]++;
}
void dec(int x){
cnt[a[x]]--;
now-=cnt[a[x]^k];
}
int main(){
scanf("%d%d%d",&n,&m,&k);
di=(int)sqrt(n);
for(int i=,x;i<=n;i++){
scanf("%d",&x);
a[i]=a[i-]^x;
}
for(int i=;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].l--; q[i].id=i;
}
sort(q+,q++m);
int l=,r=;
cnt[a[]]=;
for(int i=;i<=m;i++){
while(r<q[i].r) add(++r);
while(r>q[i].r) dec(r--);
while(l<q[i].l) dec(l++);
while(l>q[i].l) add(--l);
out[q[i].id]=now;
}
for(int i=;i<=m;i++) printf("%lld\n",out[i]);
return ;
}

奇妙代码

XOR (莫队)的更多相关文章

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

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

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

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

  4. XOR and Favorite Number(莫队算法+分块)

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

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

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

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

  7. XOR Queries(莫队+trie)

    题目链接: XOR Queries 给出一个长度为nn的数组CC,回答mm个形式为(L, R, A, B)(L,R,A,B)的询问,含义为存在多少个不同的数组下标k \in [L, R]k∈[L,R] ...

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

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

随机推荐

  1. TDD入门demo

    OK,前面的博客整理了一系列的junit相关内容,这里举一个例子TDD实际的编码例子,不管实际编码中是否使用TDD,个人觉得这种思想必须要有. 我们不一定在写业务代码之前一定要说是把测试类都写出来,至 ...

  2. junit源码解析--捕获测试结果

    OK,前面的博客我们整理了junit运行完了所有的测试用例,那么OK了,现在开始该收集测试结果了. 在这最后一步中,junit主要是玩一个类,TestResult.这里类中封装了几个参数,在初始化这个 ...

  3. jdk源码->集合->HashSet

    类的属性 public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, ...

  4. mybatis支持oracle批量插入

    问题:mysql使用mybatis批量插入时,通过foreach标签,将每条记录按照逗号","连接即可. 但是,oracle不支持. oracle支持如下写法: <inser ...

  5. 使用locate 的正则查询 查找所有main.c

    locate支持正则查询的功能, 只需输入locate -r 正则表达式     即可. 现在我想查找所有main.c怎么做? 打开终端,输入shell: locate -r main.c$ PS:' ...

  6. 接口中定义变量必须为public static final的原因

    在interface里面的变量默认都是public static final 的,原因如下: 1.   接口是一种高度抽象的"模版",,而接口中的属性也就是’模版’的成员,就应当是 ...

  7. 使用hbase小结

    背景 hbase中一张表的rowkey定义为时间戳+字符串 需求 根据时间戳和列簇中某列的值为"abc",导出一天内的数据到excel中. 使用FilterList FilterL ...

  8. quartz的一些记录

    定时任务总会遇到任务重叠执行的情况,比如一个任务1分钟执行一次,而任务的执行时间超过了1分钟,这样就会有两个相同任务并发执行了.有时候我们是允许这种情况的发生的,比如任务执行的代码是幂等的,而有时候我 ...

  9. GitHub入门之路(1)

    介绍 从本篇文章开始,是一系列介绍GitHub相关内容以及Git的一些基本操作的文章,记录了自己的学习过程. 概要 简单介绍GitHub是什么,Git又是什么. 1.Git是什么 Git是一款分散型的 ...

  10. POJ Christmas Game [树上删边游戏 Multi-SG]

    传送门 题意: 有N 个局部联通的图.Harry 和Sally 轮流从图中删边,删去一条边后,不与根节点相连的部分将被移走.Sally 为先手.图是通过从基础树中加一些边得到的.所有形成的环保证不共用 ...