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, ..., ajis 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.

题解:首先要知道 a^b=c 等价于 a=c^b;   我们用a[i]记录前I个数的异或和,然后离线处理所有区间(对于所有区间我们按L所在块为第一排序,该询问的r为第二排序,对所有询问区间排序);

对于新增加的一个数我们加上前区间异或等于k^s的数的个数,然后更新一下异或为s的数量,对于一个需要去掉的数,我需要先新更新这个数的数量,然后减去k^s的数量即可;

离线跑一遍,

参考代码为:

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int Max = ;
const int MAXM = <<;
struct Point
{
int L ,R,Id;
} a[Max]; LL sum[Max],ans[Max],k;
int n,m,L,R;
LL cnt[MAXM],ant;
bool cmp(Point b,Point c)
{
return b.L/==c.L/? b.R<c.R:b.L<c.L;
}
void Dec(LL s)
{
--cnt[s];
ant-=cnt[s^k];
}
void Inc(LL s)
{
ant += cnt[s^k];
cnt[s]++;
}
int main()
{
scanf("%d %d %lld",&n,&m,&k);
LL data;
for(int i=;i<=n;i++) scanf("%lld",&sum[i]),sum[i]^=sum[i-];
for(int i=;i<=m;i++) scanf("%d %d",&a[i].L,&a[i].R),a[i].Id = i,a[i].L--;
sort(a+,a+m+,cmp);
L=,R=,cnt[]=,ant=;
for(int i=;i<=m;i++)
{
while(R<a[i].R) Inc(sum[++R]);
while(R>a[i].R) Dec(sum[R--]);
while(L<a[i].L) Dec(sum[L++]);
while(L>a[i].L) Inc(sum[--L]);
ans[a[i].Id]=ant;
}
for(int i=;i<=m;i++) printf("%lld\n",ans[i]);
return ;
}

CodeForces-617E XOR And Favorite Numbers(莫队)的更多相关文章

  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. XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)

    题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...

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

  6. Codeforces - 617E 年轻人的第一道莫队

    我对莫队算法最为纠结的地方就是区间端点处,应该是像代码里那样理解吧 cnt[i]表示i出现的次数 maxn开2e6比较保险 /*H E A D*/ struct Query{ int l,r,id; ...

  7. Codeforces - 617E 年轻人的第一道莫队·改

    题意:给出\(n,m,k,a[1...n]\),对于每次询问,求\([l,r]\)中\(a[i] \ xor \ a[i+1] \ xor \ ...a[j],l<=i<=j<=r\ ...

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

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

随机推荐

  1. Java自学基础、进阶、项目实战网站推荐

    推荐一个自学的好平台,有Java基础,前端,后端,基础的内容都有讲解,还有框架的讲解和实战项目,特别适合自学 JAVA 自学网站 JAVA 练习题 Spring 教程 Mybatis 教程 Sprin ...

  2. java遍历一个实体

    //遍历order,得到属性值不为空的属性,type:操作类型.0是新增,1是更新 private Map<String, Object> reflect(Order order,Stri ...

  3. MathType转Word公式(OMML)

    背景 由于之前个人喜欢在Word里做笔记,而有很多笔记里存在着大量的公式.在早期,由于对Word自身的公式的不理解,所以便使用了MathType这个工具来编写公式.但是现在本人已经转战到LatTeX了 ...

  4. [增强for循环] 格式

    比如:

  5. thinkphp在模板中使用php的函数

    thinkphp在模板中使用php的函数 使用 {:函数名} 的形式 例如: // 获取 session 中存的值 {:session('admin.loginname')} // 输出当前日期 {: ...

  6. MySQL如何永久解决由dos编码格式导致MySQ的显示乱码

    MySQL如何永久解决由dos编码格式导致MySQ的显示乱码 Ⅰ.新建文件 changeCode.txt Ⅱ.粘贴下面代码(作用:将默认的GBK(936)修改为UTF-8(65001)) Window ...

  7. 网站统计IP PV UV

    ###我只是一个搬运工 网站流量统计可以帮助我们分析网站的访问和广告来访等数据,里面包含很多数据的,比如访问使用的系统,浏览器,ip归属地,访问时间,搜索引擎来源,广告效果等. PV(访问量):Pag ...

  8. PostGIS 结合Openlayers以及Geoserver实现最短路径分析(二)

    前文讲述了怎么用ArcMap制作了测试数据,并导入了PostGIS,接下来我们需要结合PgRouting插件,对入库的数据再进行一下处理. 1.在pgAdmin中,执行下面的sql语句 --添加起点字 ...

  9. 【翻译】全球用尽IPv4的一点思考

    作者:Dimple 公众号:奔跑吧攻城狮 简介:专属于Java和Android开发,和你聊聊职场话题,一同展望未来 作为小小号主的我表示很无力啊,这几天,天天都是热点.前有网易员工勇敢发声维护自己的利 ...

  10. Vmware EXSI服务迁移无法访问故障处理

    Vmware EXSI服务迁移无法访问故障处理 我们在做微服务平台服务时经常在构建IAAS时,因为硬件资源的扩容.缩减等可维护性问题需要迁移或者复制方式扩容方式来快速扩建集群节点,提高微服务运营的可靠 ...