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.

题目链接

http://codeforces.com/problemset/problem/617/E

题意

给你一段长度为n的区间

有m次询问

和一个要求的数K

m次查询区间L~R内有多少对 i j 满足 ai^ai+1......^aj =k;

#include<bits/stdc++.h>
using namespace std;
const int maxn = <<;
/*
莫队算法:
只有查询没有修改的操作
O(1)查询
n^1.5 */
struct node{
int l,r,id;
// 左 右 第几个询问
}Q[maxn]; int pos[maxn];
long long ans[maxn];
long long flag[maxn]; //每个前缀出现的次数
int a[maxn];
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]; //按块排序
} int n,m,k;
int L=,R=;
long long Ans=;
void add(int x){
Ans+=flag[a[x]^k]; //前缀和异或
flag[a[x]]++;
}
void del(int x){
flag[a[x]]--;
Ans-=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]=Ans; //第i次查询的结果
}
for(int i=;i<=m;i++){
cout<<ans[i]<<endl;
} }

莫队算法初识~~CodeForces - 617E的更多相关文章

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

    http://codeforces.com/problemset/problem/617/E 题意:给出n个数,q个询问区间,问这个区间里面有多少个区间[i,j]可以使得ai^ai+1^...^aj ...

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

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

  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 莫队算法

    E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...

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

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

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

    题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, ...

  8. [莫队算法 线段树 斐波那契 暴力] Codeforces 633H Fibonacci-ish II

    题目大意:给出一个长度为n的数列a. 对于一个询问lj和rj.将a[lj]到a[rj]从小到大排序后并去重.设得到的新数列为b,长度为k,求F1*b1+F2*b2+F3*b3+...+Fk*bk.当中 ...

  9. Codeforces 86D Powerful array (莫队算法)

    题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s< ...

随机推荐

  1. NuGet管理和还原程序包

    在很多开源的程序下载下来不能使用,一般都是平台X86 和X64没有修改,还一个就是程序缺少资源包文件.用Nuget还原即可: 一般建议先修改好平台,然后用NuGet还原程序包.

  2. C#基础-面向对象-多态

    多态,不同对象对同一方法的不同实现 使用abstract关键字表示抽象类 // 表示是一个抽象类 public abstract class Animal { private string name; ...

  3. 为什么C++编译器不能支持对模板的分离式编译

    首先,一个编译单元(translation unit)是指一个.cpp文件以及它所#include的所有.h文件,.h文件里的代码将会被扩展到包含它的.cpp文件里,然后编译器编译该.cpp文件为一个 ...

  4. WPF仿QQ聊天框表情文字混排实现

    原文:WPF仿QQ聊天框表情文字混排实现 二话不说.先上图 图中分别有文件.文本+表情.纯文本的展示,对于同一个list不同的展示形式,很明显,应该用多个DataTemplate,那么也就需要Data ...

  5. Maven学习 (三) 使用m2eclipse创建web项目

    1.首先确认你的eclipse已经安装好m2eclipse的环境,可以参照上两篇Maven学习内容 2.新建一个maven的项目 3.下一步默认配置,使用默认的工作空间,或者你可以自己选择其他的空间 ...

  6. Hadoop学习笔记系列

    Hadoop学习笔记系列   一.为何要学习Hadoop? 这是一个信息爆炸的时代.经过数十年的积累,很多企业都聚集了大量的数据.这些数据也是企业的核心财富之一,怎样从累积的数据里寻找价值,变废为宝炼 ...

  7. HTML5技巧

    HTML5技巧   HTML5 技巧一:当今科技发展的速度真惊人,稍不留神,就可能无法跟上它的步伐.新一代的HTML-HTML5的发展也不停的带给我们新的惊喜,我们将通过这篇文章向大家介绍一些HTML ...

  8. WebApi实现Ajax模拟Multipart/form-data方式多文件上传

    前端页面代码: <input type="file" class="file_control" /><br /> <input t ...

  9. VSX-3 VSCT文件

    关于VSPackage中的VSCT,算是VSX开发中比较重要的一个成员. 我这里给出LearnVSXNow!系列文章关于VSCT的链接,除了#14有译文. #14 #18 #25 看完上面几篇文章,也 ...

  10. IIS Express mime type 列表。

    C:\Users\Administrator\Documents\IISExpress\config\applicationhost.config -------------------------- ...