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

6 2 3
1 2 1 1 0 3
1 6
3 5

output

7
0

input

5 3 1
1 1 1 1 1
1 5
2 4
1 3

output

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.

题意:询问区间[l,r]内有多少个子区间,其亦或和等于k。

思路:莫队,对于区间[a,b],区间[a,b+1]的ans等于[a,b]的ans加上区间[a,b]内OXR[b+1]^k的个数

     对于[a,b]的亦或和,即为XOR[b]^XOR[a-1]

   XOR[b]^XOR[a-1] == k   <==>   XOR[b]^k == XOR[a-1]

   因此寻找有多少个XOR[a-1]满足XOR[b]^XOR[a-1] == k ,即寻找有多少个XOR[b]^k

   使用一个cnt数组记录当前状态下不同区间亦或和的值出现的次数。

 //2017-11-14
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
const int LEN = ; int n, m, k, L, R, a[N], XOR[N], block[N];
long long ans, ANS[N], cnt[N];
struct Node{
int l, r, id;
bool operator<(const Node x) const {
if(block[l] == block[x.l])
return r < x.r;
return block[l] < block[x.l];
}
}q[N]; void add(int x){
ans += cnt[XOR[x]^k];
cnt[XOR[x]]++;
} void del(int x){
cnt[XOR[x]]--;
ans -= cnt[XOR[x]^k];
} int main()
{
//freopen("input.txt", "r", stdin);
while(~scanf("%d%d%d", &n, &m, &k)){
XOR[] = ;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
XOR[i] = XOR[i-] ^ a[i];
block[i] = (i-)/LEN;
}
for(int i = ; i < m; i++){
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q, q+m);
L = , R = , ans = ;
cnt[] = ;
for(int i = ; i < m; i++){
while(L < q[i].l){
del(L-);
L++;
}
while(L > q[i].l){
L--;
add(L-);
}
while(R < q[i].r){
R++;
add(R);
}
while(R > q[i].r){
del(R);
R--;
}
ANS[q[i].id] = ans;
}
for(int i = ; i < m; i++)
printf("%lld\n", ANS[i]);
} return ;
}

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

  1. Codeforces617E【莫队算法+前缀异或】

    题意: 给出一系列数,对每个查询区间,计算有多少个子区间异或为k. 思路: 可以先预处理异或前缀,一个区间[L,R]的异或值=sum[R]^sum[L-1]; 如果当前区间是[a,b],加一个右端点b ...

  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. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  4. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  5. 【填坑向】bzoj2038小Z的袜子 莫队

    学莫队必做题,,,但是懒得写.今天来填个坑 莫队水题 莫队实际上就是按一个玄学顺序来离线计算询问,保证复杂度只会多一个n1/2,感觉是玄学(离线算法都很玄学) 易错点:要开long long(卡我半天 ...

  6. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  7. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  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. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

随机推荐

  1. Senparc.Weixin.MP SDK 微信公众平台开发教程(十九):MessageHandler 的未知类型消息处理

    这是<微信开发深度解析:微信公众号.小程序高效开发秘籍>出版之后写的第一篇微信相关的文章.从这一篇开始,将介绍第一版出版之后添加或修改的功能,或者对书上内容需要做的补充. MP v14.8 ...

  2. [转]kaldi ASR: DNN训练

    作者:zqh_zy链接:http://www.jianshu.com/p/c5fb943afaba來源:简书著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本文通过简单kaldi ...

  3. 终于明白word-break属性——break-all和break-word的区别

      其实一直不明白,也没有认真去想过 word-break 属性的 break-all 和 break-word 有什么区别 后来看了一个大神写的一篇博客,写得很详细,看了豁然开朗. 所以,我也就不在 ...

  4. TX-LCN分布式事务Demo实战

    1. TX-LCN分布式事务Demo实战 1.1. 原理介绍 1.1.1. 事务控制原理 TX-LCN由两大模块组成, TxClient.TxManager,TxClient作为模块的依赖框架,提供T ...

  5. 性能瓶颈之Mapping

    如果Source和Target都不存在性能上的瓶颈,则问题可能会出在Mapping 如何判定Mapping存在性能瓶颈 1)  在session log中读取thread statistics和wor ...

  6. ALL_DB_LINKS

    类型:View Owner:SYS 内容:记录了当前用户下可访问的所有的DB links 字段: OWNER : DB Link的owner DB_LINK : DB Link名称 USERNAME ...

  7. 金九银十,史上最强 Java 面试题整理。

    以下会重新整理所有 Java 系列面试题答案.及各大互联网公司的面试经验,会从以下几个方面汇总,本文会长期更新. Java 面试篇 史上最全 Java 面试题,带全部答案 史上最全 69 道 Spri ...

  8. Kubernetes集群搭建之CNI-Flanneld部署篇

    本次系列使用的所需部署包版本都使用的目前最新的或最新稳定版,安装包地址请到公众号内回复[K8s实战]获取 Flannel是CoreOS提供用于解决Dokcer集群跨主机通讯的覆盖网络工具.它的主要思路 ...

  9. 使用eclipse创建maven+动态web的项目

    windows7操作系统 提前安装java jdk1.8版本+apache-maven-3.3.3+wildfly-10.0.0.Final 1.新建maven项目,到other里面找一下 2.使用默 ...

  10. [疑难杂症]__当你的Cortana搜索无法使用,显示纯白界面(ps:已解决).

    前言 这个问题是在前不久解决关于我电脑点击屏幕上方快捷方式不久后出现的问题,之前并没有出现过这样的错误,但因为使用到的情况比较少,就一直没有去解决,但在一点时间后,发现没有Cortana搜索栏还是十分 ...