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.

Sample test(s)
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

题意:给你n个数和m个询问以及k,每个询问给一个区间[l,r],问区间内有多少对(i,j),使得a[i]^a[i+1]^a[i+2]^...^a[j]=k.

思路:我们可以求出前缀异或和a[i],那么题目就变成问区间[l,r]内,有多少对(i,j)满足a[i-1]^a[j]=k,我们可以用cnt[i]记录异或值为i的个数,然后就能用莫队算法了。

#include<stdio.h>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef __int64 ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 100050
ll a[maxn],sum[maxn],unit;
struct node{
ll l,r,idx;
}b[maxn];
bool cmp(node a,node b){
if(a.l/unit == b.l/unit){
return a.r < b.r;
}
return a.l/unit < b.l/unit;
} ll cnt[1050000];
ll ans[maxn]; int main()
{
ll n,m,k;
int i,j;
while(scanf("%I64d%I64d%I64d",&n,&m,&k)!=EOF)
{
a[0]=0;
for(i=1;i<=n;i++){
scanf("%I64d",&a[i]);
a[i]=a[i-1]^a[i];
}
unit=(ll)sqrt(n);
for(i=1;i<=m;i++){
scanf("%I64d%I64d",&b[i].l,&b[i].r);
b[i].idx=i;
}
sort(b+1,b+1+m,cmp);
ll l=1,r=0;
ll num=0;
memset(cnt,0,sizeof(cnt));
cnt[0]=1; //这里要注意,一开始cnt[0]=0
for(i=1;i<=m;i++){ //对于询问区间[l,r],相当于在维护【a[l-1],a[r]】出现的次数。
while(r<b[i].r){ //每个while语句里的前后顺序要注意
r++;
num+=cnt[k^a[r] ];
cnt[a[r] ]++;
}
while(r>b[i].r){
cnt[a[r] ]--;
num-=cnt[k^a[r] ];
r--;
}
while(l<b[i].l){
cnt[a[l-1] ]--;
num-=cnt[k^a[l-1] ];
l++;
}
while(l>b[i].l){
l--;
num+=cnt[k^a[l-1] ];
cnt[a[l-1] ]++; }
ans[b[i].idx ]=num;
}
for(i=1;i<=m;i++){
printf("%I64d\n",ans[i]);
}
}
return 0;
}

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

    题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...

  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 莫队板子

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

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

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

  7. Codeforces Round #340 (Div. 2) E 莫队+前缀异或和

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

  8. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  9. Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力

    C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...

随机推荐

  1. 九:APP及其他资产

    APP提取一键反编译提取 APP抓数据包进行工具配合 各种第三方应用相关探针技术 各种服务器接口相关探针技术 APP提取及抓包及后续配合 某APK一键提取反编译 利用burp历史抓更多URL 某IP无 ...

  2. MySQL常用的数据类型和字段属性

    数据类型 数值 tinyint 十分小的数据 1个字节 smallint 较小的数据 2个字节 mediumint 中等大小的数据 3个字节 int 标准的整数 4个字节 常用 bigint 较大的数 ...

  3. 【Linux】saltstack 安装及简单使用

    准备三台server,一台为master(10.96.20.113),另两台为minion(10.96.20.117,10.96.20.118) 主机名(master.minion1.minion2) ...

  4. RAC上的DG搭建

    准备工作 修改rman_backup这个文件的所有者和所属组,修改为oracle用户的oinstall组的文件 #chown –R oracle:oinstall /rman_backup/ 主库和备 ...

  5. 绝对定位上下左右都为0 margin为auto为什么能居中

    老规矩,先来一段废话,我大学刚入门的时候觉得CSS很简单,记一记就会了,不就是盒模型嘛,现在想来觉得自己那时候真的很自以为是哈哈.但是随着工作沉淀,我明白了任何技术都有着它的深度和广度,正是因为不少人 ...

  6. 使用call、apply、bind继承及三者区别

    js里的继承方法有很多,比如:使用原型链的组合继承.es6的Class.寄生继承以及使用call.apply.bind继承.再说继承之前,我们先简单了解下它们的区别. 一.区别: 同:三者都是改变函数 ...

  7. Java异常处理场景中不同位置的返回值详细解析

    Java 异常处理中的返回值在不同位置不同场景下是有一些差别的,这里需要格外注意 具体分以下两种场景: 1 finally语句块没有return语句,即当代码执行到try或者catch语句块中的ret ...

  8. (08)-Python3之--类和对象

    1.定义 类:类是抽象的,一类事物的共性的体现.  有共性的属性和行为. 对象:具体化,实例化.有具体的属性值,有具体做的行为. 一个类 对应N多个对象. 类包含属性以及方法. class 类名: 属 ...

  9. MySQL中redo log、undo log、binlog关系以及区别

    MySQL中redo log.undo log.binlog关系以及区别 本文转载自:MySQL中的重做日志(redo log),回滚日志(undo log),以及二进制日志(binlog)的简单总结 ...

  10. php artisan db:seed 报错

    在laravel 5中执行,要执行数据填充时报如下错误 php artisan db:seed 错误: [ReflectionException]                        Cla ...