任意门:http://codeforces.com/problemset/problem/617/E

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 that l ≤ 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
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.

题目大意:

有一串长度为 N 的数列,M次查询 ( l,r )内有多少对 ( i, j )使得 ai ^ ai+1 ^ ... ^ aj = K;

解题思路:

这里的区间查询是离线的,可以用传说中的莫队算法(优雅而华丽的暴力算法)。

异或的题目有一个很巧妙的地方就是利用 a^b^a = b 这个性质。

这道题目我们也要预处理一下 sum(i) 前 i 个数的异或和, 那么 ai ^ ai+1 ^ ... ^ aj = sum( i - 1) ^ sum( j ) 了。

接下来我们就可以按照查询的区间用莫队算法遍历一遍,同时记录当前区间某个前缀和的出现次数;

根据 sum( i - 1) ^ sum( j ) = K ,K  ^ sum( i - 1 ) = sum( j ) || K ^ sum( j ) = sum( i - 1) ,由符合条件的前缀和次数来推出符合条件的( i,j )的个数啦。

Tip:

听了大神的课,这道题有两个坑:

1、答案的数据的数据范围是爆 int 的;

2、虽然是1e6 的 K,但异或的结果要大于 1e6 ;

AC code:

 #include <bits/stdc++.h>
#define LL long long int
using namespace std;
const int MAXN = <<; struct node
{
int l, r, id; //区间和查询编号
}Q[MAXN]; //记录查询数据(离线) int pos[MAXN]; //记录分块
LL ans[MAXN]; //记录答案
LL flag[MAXN]; //维护前缀异或和出现的次数
int a[MAXN]; //原本数据
int L=, R; //当前区间的左右结点
LL res; //储存当前区间的值
int N, M, K;
bool cmp(node a, node b) //排序
{
if(pos[a.l]==pos[b.l]) return a.r < b.r; //只有左结点在同一分块才排右结点
return pos[a.l] < pos[b.l]; //否则按照左结点分块排
}
void add(int x)
{
res+=flag[a[x]^K];
flag[a[x]]++;
}
void del(int x)
{
flag[a[x]]--;
res-=flag[a[x]^K];
}
int main()
{
scanf("%d%d%d", &N, &M, &K);
int sz = sqrt(N);
for(int i = ; i <= N; i++){ //读入数据
scanf("%d", &a[i]);
a[i] = a[i]^a[i-]; //计算前缀异或和
pos[i] = i/sz; //分块
}
for(int i = ; i <= M; i++){ //读入查询
scanf("%d%d", &Q[i].l, &Q[i].r);
Q[i].id = i;
}
sort(Q+, Q++M, cmp);
flag[] = ;
for(int i = ; i <= M; i++){
while(L < Q[i].l){ //当前左结点比查询结点小
del(L-);
L++;
}
while(L > Q[i].l){ //当前左结点比查询左结点大
L--;
add(L-);
}
while(R < Q[i].r){ //当前右结点比查询右结点小
R++;
add(R);
}
while(R > Q[i].r){ //当前右节点比查询右节点大
del(R);
R--;
}
ans[Q[i].id] = res;
}
for(int i = ; i <= M; i++){
printf("%lld\n", ans[i]);
}
}

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】的更多相关文章

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

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

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

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

    题目链接:http://codeforces.com/contest/617/problem/E 题目大意:有n个数和m次查询,每次查询区间[l, r]问满足ai ^ ai+1 ^ ... ^ aj ...

  5. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

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

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

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

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

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

  9. [Codeforces Round #340 (Div. 2)]

    [Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...

随机推荐

  1. 2019.03.26 读书笔记 关于for与foreach

    for 是索引器,foreach是迭代器 foreach在movenext()中增加了对集合版本(一个整数,每次对集合修改都+1)的验证,另外反编译后的效果是使用了using(是try finally ...

  2. linux 期中架构之 nginx 安装与排错

    1, 安装 nginx 所需要的pcre库 即:perl 兼容正则表达式 yum install pcre pcre-devel -y rpm -qa pcre pcre-devel 检查是否安装好p ...

  3. 日志收集之nxlog

    一,软件介绍 nxlog 是用 C 语言写的一个开源日志收集处理软件,它是一个模块化.多线程.高性能的日志管理解决方案,支持多平台.可以处理来自许多不同来源的大量事件日志.支持的日志处理类型包括重写, ...

  4. shell 实现文件改名

    修改文件名可以有不同的命令方式,mv 可以实现,但是使用rename 这种专业的改名字很好 对于单个的文件,可以直接使用以上的命令,那如果有大量的类似格式的文件名需要修改成其他格式的,该如何呢? 创建 ...

  5. thinkphp执行流程

    1. 入口文件index.php 用户对url的访问首先被定位到http://<serverIp>/<appName>/index.php, 这里的入口文件index.php做 ...

  6. Python 集合(set)类型的操作——并交差

    介绍 python的set是一个无序不重复元素集,基本功能包括关系测试和消除重复元素. 集合对象还支持并.交.差.对称差等. sets 支持 x in set. len(set).和 for x in ...

  7. jquery获取元素与屏幕高度距离

    a. onscroll事件 scroll是css样式中overflow的一个值,意思是显示滚动条;当一个元素的实际高度超过他的最大高度是,只要设置了overflow为scroll b. $(..).s ...

  8. 很有用的PHP笔试题系列三

    1. 什么事面向对象?主要特征是什么? 面向对象是程序的一种设计方式,它利于提高程序的重用性,使程序结构更加清晰.主要特征:封装.继承.多态. 2. SESSION 与 COOKIE的区别是什么,请从 ...

  9. 安装VMware,出现Microsoft Runtime DLL 安装程序未能完成安装,解决方法

    安装VMware Workstation 12 Player出现如下问题: 解决方法: 1.出现这个问题的时候不要点确定(如果点了确定,会找不到步骤4中的文件夹) 2.win+R调出 '运行' 3.输 ...

  10. UiPath进阶

    最近RPA比较火,UiPath工具排名前几位并且免费试用,很多朋友们都选择了学习自动化工具UiPath,今天我就向大家介绍一下UiPath的学习过程,希望对后来的学习这个工具的人有所帮助. UiPat ...