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 题意:
询问区间内异或和刚好为k的字段个数。
思路:
莫队+前缀和。
这个前缀和比较套路,用的是前缀异或和。
字段【l,r】的异或和就是pre[r]^pre[l-1],
这种情况下我们在莫队的过程中记录l,r的pre[i]出现的次数,就可以完成更新了。 注意当L<q[i].l时,要先让记录per[L]出现次数的个数减一。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int num[maxm*],pre[maxn],a[maxn]; struct node{
int l,r;
int id;
}q[maxn];
ll ans[maxn];
ll anss;
int block; bool cmp(node a,node b){
if(a.l/block!=b.l/block){return a.l<b.l;}
return a.r<b.r;
} int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
pre[i]=pre[i-]^a[i];
} block=sqrt(n); for(int i=;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
} sort(q+,q++m,cmp);
int L=,R=;
anss=;
num[]=;
for(int i=;i<=m;i++){
while(L<q[i].l){
int t=k^pre[L-];
num[pre[L-]]--;///注意语句顺序
anss-=num[t];
L++;
}
while(R>q[i].r){
int t=k^pre[R];
num[pre[R]]--;
anss-=num[t];
R--;
}
while(L>q[i].l){
L--;
int t=k^pre[L-];
anss+=num[t];
num[pre[L-]]++;
}
while(R<q[i].r){
R++;
int t=k^pre[R];
anss+=num[t];
num[pre[R]]++;
}
ans[q[i].id]=anss;
}
for(int i=;i<=m;i++){
printf("%lld\n",ans[i]);
}
return ;
}

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 340 XOR and Favorite Number 莫队模板题

    原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...

  5. Codeforces Round #340 (Div. 2) E 莫队+前缀异或和

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

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

  7. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

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

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

随机推荐

  1. Leetcode747.Largest Number At Least Twice of Others至少是其他数字两倍的最大数

    在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums = [3, ...

  2. GIL锁更加深刻理解

    参考链接:http://www.cnblogs.com/ajaxa/p/9111884.html

  3. 【NS2】TCL debug (转载)

    1.使用NS2进行模拟,就不可避免的会接触TCL/OTCL和C/C++.两者配合使用.一般设置场景啊,业务流啊,都使用TCL/OTCL来编写脚 本.要进行路由实验模拟的话,同一类的实验,这些脚本基本上 ...

  4. python的if循环和嵌套

    1.    if 条件:    if语句块   执行流程:判断条件是否为真. 如果真. 执行if语句块 money = int(input('请输入你兜里的钱:')) if money >500 ...

  5. CNN网络改善的方法——池化

    一个能降低卷积金字塔中特征图的空间维度,目前为止,我们通过调整步幅,将滤镜每次移动几个像素.图1 从而降低特征图的尺寸.这是降低图像采样率的一种非常有效的方法. 图1 它移除了很多信息,如果我们不采用 ...

  6. Bitmap的recycle问题

        虽然Android有自己的垃圾回收机制,对于是不是要我们自己调用recycle,还的看情况而定.如果只是使用少量的几张图片,回收与否关系不大.可是若有大量bitmap需要垃圾回收处理,那必然垃 ...

  7. uniapp点击底部tabbar不跳转页面

    一个项目,其设想是这样的,当我进入页面,发现有新版本,提示用户之后,用户点击确定跳转到下载页面. 弹出框要用自己封装的,因为uniapp的弹出框不同的手机上展示的样子不一样,领导的是华为(在这里悄悄吐 ...

  8. winform(C#)里弹出“确定”“取消”对话框

    //消息框中需要显示哪些按钮,此处显示“确定”和“取消” MessageBoxButtons messButton = MessageBoxButtons.OKCancel; //"确定要退 ...

  9. html5的开发

    1.html5的开发组织者: (1)WHATWG:由Apple.Mozilla.Google.Opera等浏览器开发者组成,成立于2004年.WHATWG开发HTML和Web应用API,同时为各浏览器 ...

  10. js如何获取地址栏上的参数id

    直接上js代码   方法1: 父页面跳转地址栏带id>>>>window.location.href='${ctxAdmin}/study/checkForm?id='+id+ ...