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. JDK1.7和JDK1.8对于异常的支持

    嗨喽,伙伴们,上次我们讲了<Java异常解读以及通过业务逻辑解决异常的方式>和<java异常处理方式try-catch-finally>,相信大家对java异常及处理方式有所了 ...

  2. 巧用这19条MySQL优化,效率至少提高3倍

    阅读本文大概需要 3.8 分钟. 作者丨喜欢拿铁的人 https://zhuanlan.zhihu.com/p/49888088 本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下: 1 ...

  3. HTTP 协议中 GET 和 POST 方法详解

    GET请求报文分析 1.请求行 请求方法 GET(描述该请求采用了什么请求方法),HTTP 1.0 和 1.1 协议中共包含10种请求方法.不过 HTTP 1.1 中只有8种方法. URI 请求WEB ...

  4. JMagic 操作 ImageMagick 处理图片

    项目描述 imagemagick是功能强大的图片处理库,以稳定及高效率著称,众多语言对该库进行封装处理.比如php.java.由于我们是java项目,直接使用java通过JNI技术调用ImageMag ...

  5. MQ 简单的使用

    需要创建两个控制台应用 引用用下面的包 (1)生产者 static void Main(string[] args) { ConnectionFactory factory = new Connect ...

  6. java项目引用证书文件(微信支付的p12文件)

    1. 绝对路径: // windows: public static String PATH1 = "E:\\project27_app_wuyoujie\\apiclient_cert.p ...

  7. dbvisual 9 使用自定义jdk版本运行

    dbvisual 9 不支持jdk1.8 ,当系统默认的jdk是1.8且不方便修改时,可以自行指定运行dbvisual9.2 的jdk版本 打开dbvisgui.bat 将set JAVA_EXEC= ...

  8. 请求被中止: 未能创建 SSL/TLS 安全通道

    出现“请求被中止: 未能创建 SSL/TLS 安全通道.”的问题. 在创建请求地址的前面加了这句代码就可以了 System.Net.ServicePointManager.SecurityProtoc ...

  9. 持续集成工具之Jenkins

    Jenkins是一个很好的持续集成工具,不光可以帮助开发进行自动打包,自动验证升级和安装,也可以帮助测试人员定时执行测试任务,或者在开自动打包安装之后自动执行测试任务,实现打包-安装-测试一条线服务, ...

  10. hibernate框架(2)---Hibernate的核心API

    Hibernate的核心API 一般我们通过hibernate进行操作的时候,都会遵循下面的流程,那么接下来我对每一个步骤进行讲解: 1 public void testInsert() { 2 // ...