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 thatl ≤ 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

Copy
6 2 3
1 2 1 1 0 3
1 6
3 5
output

Copy
7
0
input

Copy
5 3 1
1 1 1 1 1
1 5
2 4
1 3
output

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

题意:有n个数和m次询问,每一询问会有一个L和R,表示所询问的区间,

问在这个区间中有多少个连续的子区间的亦或和为k

假设我们现在有一个前缀异或和数组sum[],现在我们要求区间[L,R]的异或的值,

用sum数组表示就是sum[L-1]^sum[R]==K,或者说是K^sum[R]==sum[L-1]

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e6 + ;
int n, m, k, L, R, sz, a[maxn];
LL sum[maxn], ans, ANS[maxn];
struct node {
int l, r, id;
node() {}
node(int l, int r, int id): l(l), r(r), id(id) {}
bool operator <(const node & a)const {
if (l / sz == a.l / sz) return r < a.r;
return l < a.l;
}
} qu[maxn];
void add(int x) {
ans += sum[a[x] ^ k];
sum[a[x]]++;
}
void del(int x) {
sum[a[x]]--;
ans -= sum[a[x] ^ k];
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i <= n ; i++) {
scanf("%d", &a[i]);
a[i] ^= a[i - ];
}
for (int i = ; i <= m ; i++) {
scanf("%d%d", &qu[i].l, &qu[i].r);
qu[i].l--;
qu[i].id = i;
}
sz = (int)sqrt(n);
sort(qu + , qu + m + );
L = , R = ;
for (int i = ; i <= m ; i++) {
while(L > qu[i].l) add(--L);
while(R < qu[i].r) add(++R);
while(L < qu[i].l) del(L++);
while(R > qu[i].r) del(R--);
ANS[qu[i].id] = ans;
}
for (int i = ; i <= m ; i++)
printf("%lld\n", ANS[i]);
return ;
}

XOR and Favorite Number (莫对算法)的更多相关文章

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

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

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

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

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

  5. CodeForces - 617E XOR and Favorite Number 莫队算法

    https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...

  6. Codeforces 617E XOR and Favorite Number莫队

    http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...

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

  8. E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)

    一直都说学莫队,直到现在才学,训练的时候就跪了   T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...

  9. codeforces 617E. XOR and Favorite Number 莫队

    题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...

  10. Codeforces Round #340 (Div. 2) E XOR and Favorite Number 莫队板子

    #include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...

随机推荐

  1. 【WXS全局对象】JSON

    方法: 原型:JSON.stringify( Object ) 说明:将 object 对象转换为 JSON 字符串,并返回该字符串. 返回:[String] 原型:JSON.parse( [Stri ...

  2. 【循环控制器】-(针对中间部分要循环的场景,相当于loadrunner的action部分)

    一般使用 setup线程组 + teardown组 针对中间要循环的部分   使用循环处理器    单独循环中间的部分,相当于loadrunner的action部分

  3. 如何使用AEditor制作一个简单的H5交互页demo

    转载自:http://www.alloyteam.com/2015/06/h5-jiao-hu-ye-bian-ji-qi-aeditor-jie-shao/ 本教程演示如何使用AEditor制作一个 ...

  4. adb 在windows7中的使用

    我的系统环境是win7 x64 首先放上资源链接:https://pan.baidu.com/s/1eTV5qX8 密码:2ejw 第一步: 配置环境变量,将adb.exe的路径添加到PATH里面去: ...

  5. 隐马尔科夫模型(hidden Markov Model)

    万事开头难啊,刚开头确实不知道该怎么写才能比较有水平,这篇博客可能会比较长,隐马尔科夫模型将会从以下几个方面进行叙述:1 隐马尔科夫模型的概率计算法  2 隐马尔科夫模型的学习算法 3 隐马尔科夫模型 ...

  6. CentOS6 安装VNCserver

    1.下载vncserver yum install tigervnc tigervnc-server -y 2.配置 vncserver vi /etc/sysconfig/vncserver 在文件 ...

  7. Python中的赋值语法

    Python中复制语法有6种 Basic Form >>>spam = 'spam' Tuple assignment >>>spam, ham = 'spam', ...

  8. C语言特殊符号

    -> ->在C语言中称为间接引用运算符,是二目运算符,优先级同成员运算符“.”.用法:p->a,其中p是指向一个结构体的指针,a是这个结构体类型的一个成员.表达式p->a引用了 ...

  9. error LNK2019: 无法解析的外部符号 该符号在函数 中被引用 解决方案

    需要添加lib或者dll库.项目-属性-配置属性-链接器-输入-附件依赖项,添加需要的lib. 例如我在运行OSG程序的时候,忘记添加了附件依赖项就会报这个错. 解决方案如图.

  10. 判断两个字符串是否相等【JAVA】

    if(A.equals(B)){ } 之前总是用"=="来判断,但是在JAVA里面好像不行.所以,用equals(). 查了下资料. 原因:equal()比较的是对象的内容,&qu ...