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. JDBC中使用Properties类及配置文件的操作

    同时发布于:https://blog.csdn.net/Activity_Time/article/details/81149710 一.properties配置文件 开发中获得连接的4个参数(驱动. ...

  2. 搜索二维矩阵 II

    描述 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复的整数. 样例 ...

  3. 蓝桥杯算法训练 区间k大数查询

    算法训练 区间k大数查询   问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二行包含n个正整数,表示给定的序列. 第三个 ...

  4. Logistic回归和SVM的异同

    这个问题在最近面试的时候被问了几次,让谈一下Logistic回归(以下简称LR)和SVM的异同.由于之前没有对比分析过,而且不知道从哪个角度去分析,一时语塞,只能不知为不知. 现在对这二者做一个对比分 ...

  5. LeetCode 135——分发糖果

    1. 题目 2. 解答 初始化左序奖赏全为 1,从左往右遍历,如果右边的人评分比左边高,右边奖赏比左边奖赏增 1. 初始化右序奖赏全为 1,从右往左遍历,如果左边的人评分比右边高,左边奖赏比右边奖赏增 ...

  6. Thunder团队第三周 - Scrum会议4

    Scrum会议4 小组名称:Thunder 项目名称:i阅app Scrum Master:邹双黛 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...

  7. UML建模语言入门 -- 静态图详解 类图 对象图 包图 静态图建模实战

    发现个好东西思维导图, 最近开始用MindManager整理博客 . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/deta ...

  8. CentOS基础命令

    为网卡配置静态IP地址建议通过交互式界面nmtui进行配置 firewalld和iptablesCentOS7使用firewald取代原来的iptables,但实际上底层还是iptables,在上层加 ...

  9. [OS] 操作系统基本类型

    ·批处理系统:(用户脱机使用.成批处理.多道程序运行) 批处理系统,又名批处理操作系统.批处理是指用户将一批作业提交给操作系统后就不再干预,由操作系统控制它们自动运行.这种采用批量处理作业技术的操作系 ...

  10. TDDL剖析

    前言 在开始讲解淘宝的TDDL(Taobao Distribute Data Layer)技术之前,请允许笔者先吐槽一番.首先要开喷的是淘宝的社区支持做的无比的烂,TaoCode开源社区上面,几乎从来 ...