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.

Example

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[],和m个询问 Q(L,R),回答每个询问中有多少对(L<=i<=j<=R) ,使得异或为k。

异或转化为前缀和处理。然后就差不多交给分块处理了。

离线处理里比较好理解的一种,高中就会了,注意这里不多BB了。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
const int maxn=;
int a[maxn],pre[maxn],num[<<],n,m,k,sqrtn;
struct Query{
int id,l,r; ll ans;
}q[maxn];
bool cmp(const Query a,const Query b)
{
if(a.l/sqrtn==b.l/sqrtn) return a.r<b.r;
return a.l<b.l;
}
bool cmp2(const Query a,const Query b)
{
return a.id<b.id;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
sqrtn=(int)sqrt(n);
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
pre[i]=pre[i-]^a[i];
}
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=;
num[pre[]]++;num[]++;
ll cur=(a[]==k?:);
for(int i=;i<m;i++)
{
while(r<q[i].r){
cur+=num[pre[r+]^k];
r++;
num[pre[r]]++;
}
while(l<q[i].l){
num[pre[l-]]--;
cur-=num[pre[l-]^k];
l++;
}
while(l>q[i].l){
cur+=num[pre[l-]^k];
num[pre[l-]]++;
l--;
}
while(r>q[i].r){
num[pre[r]]--;
cur-=num[pre[r]^k];
r--;
}
q[i].ans=cur;
}
sort(q,q+m,cmp2);
for(int i=;i<m;i++) printf("%lld\n",q[i].ans);
return ;
}

Codeforces617E XOR and Favorite Number(分块 异或)的更多相关文章

  1. [CF617E]XOR and Favorite Number/[CQOI2018]异或序列

    题目大意: 给定一个长度为$n(n\leq10^5)$的数列$A$和数$k$$(A_i,k\leq10^6)$.$m$组询问,每次询问区间$[l,r]$中有多少对$i,j(l\leq i\leq j\ ...

  2. 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 【莫队算法 + 异或和前缀和的巧妙】

    任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...

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

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

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

  6. 题解-CF617E XOR and Favorite Number

    题面 CF617E XOR and Favorite Number 给定 \(n,m,k\) 和 \(n\) 个数的序列 \(a_i\),\(m\) 次求区间 \([l,r]\) 中异或值为 \(k\ ...

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

  8. FJNU 1153 Fat Brother And XOR(胖哥与异或)

    FJNU 1153 Fat Brother And XOR(胖哥与异或) Time Limit: 1000MS   Memory Limit: 257792K [Description] [题目描述] ...

  9. CF617E XOR and Favorite Number

    CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...

随机推荐

  1. 缷载vs2015后项目不能加载问题

    当加载项目时出现MSBuildToolsPath is not specified for the ToolsVersion "14.0" defined at "HKE ...

  2. Runtime.getRuntime().exec()----记录日志案例

    Runtime.getRuntime().exec()方法主要用于运行外部的程序或命令. Runtime.getRuntime().exec共同拥有六个重载方法: 1.public Process e ...

  3. [项目构建 十一]babasport 购物车的原理及实现.

    今天来开始写一下关于购物车的东西, 这里首先抛出四个问题: 1)用户没登陆用户名和密码,添加商品, 关闭浏览器再打开后 不登录用户名和密码 问:购物车商品还在吗? 2)用户登陆了用户名密码,添加商品, ...

  4. Layout布局位置

    - - GUILayout 这个本身就是用于自动布局的. 不用给定位置的,这也是它与GUI的区别所在.通常默认开始的位置是屏幕的左上角. 当然你也可以限定开始自动布局的位置.用 GUILayout.B ...

  5. NDK以及C语言基础语法(二)

    一.字符串类:(属于类类型) -String (在C++中才有) 使用之前必学引入String 类型: 引入String头文件(系统的头文件): #include <string>   p ...

  6. 【BZOJ3796】Mushroom追妹纸 二分+hash

    [BZOJ3796]Mushroom追妹纸 Description Mushroom最近看上了一个漂亮妹纸.他选择一种非常经典的手段来表达自己的心意——写情书.考虑到自己的表达能力,Mushroom决 ...

  7. 【BZOJ2597】[Wc2007]剪刀石头布 最小费用流

    [BZOJ2597][Wc2007]剪刀石头布 Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之 ...

  8. php计算数组的维数

    function array_dim($arr){ if(!is_array($arr)) return 0; else{ $max1 = 0; foreach($arr as $item1){ $t ...

  9. Tomcat学习笔记【5】--- 项目部署详解

    本文主要讲在Tomcat中部署项目的几种方式:静态部署.动态部署. 一 静态部署 静态部署项目有好几种方式,比较典型的有如下4种: 1.1 方式一:将Web项目放到webApps目录下 直接将web项 ...

  10. while 循环中的break continue pass 的用法

    while break:跳出最近的循环 continue:跳到最近所在循环的开头处 pass:什么也不做,只是空占位语句,它本身与循环没什么关系,但属于简单的单个单词语句的范畴: pass 语句是无运 ...