Codeforces Round #340 (Div. 2) E. XOR and Favorite Number
4 seconds
256 megabytes
standard input
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.
The first line of the input contains integers n, m 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.
Print m lines, answer the queries in the order they appear in the input.
6 2 3
1 2 1 1 0 3
1 6
3 5
7
0
5 3 1
1 1 1 1 1
1 5
2 4
1 3
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [Codeforces Round #340 (Div. 2)]
[Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...
- 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 ...
- 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. 这题 ...
- Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力
C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...
随机推荐
- 【Spring】Spring的数据库开发 - 1、Spring JDBC的配置和Spring JdbcTemplate的解析
Spring JDBC 文章目录 Spring JDBC Spring JdbcTemplate的解析 Spring JDBC的配置 简单记录-Java EE企业级应用开发教程(Spring+Spri ...
- 2V转3V的电源芯片电路图,2.4V转3V电路
两节镍氢电池1.2V+1.2V是2.4V的标称电压,2.4V可以转3V输出电路应用. 在2.4V转3V和2V转3V的应用中,输出电流可最大600MA. 2V的低压输入,可以采用PW5100低压输入专用 ...
- uni-app调用wifi接口
微信小程序条件渲染 在小程序app.json中添加 需要先获取位置信息 "permission": { "scope.userLocation": { &quo ...
- JVM(三)从JVM源码角度看类加载器层级关系和双亲委派
类加载器我们都知道有如下的继承结构,这个关系只是逻辑上的父子关系. 我们一直听说引导类加载器没有实体,为什么没有实体呢? 因为引导类加载器只是一段C++代码并不是什么实体类,所谓的引导类加载器就是那一 ...
- JavaScript中的深拷贝和浅拷贝!【有错误】还未修改!请逛其他园子!
JavaScript中的深拷贝和浅拷贝! 浅拷贝 1.浅拷贝只是拷贝一层,更深层次对象级别的只拷贝引用.{也就是拷贝的是地址!简而言之就是在新的对象中修改深层次的值也会影响原来的对象!} // 2.深 ...
- B树、B+树索引算法原理(下)
B树.B+树索引算法原理(下) - codedump的网络日志 https://www.codedump.info/post/20200615-btree-2/
- 可视化Go内存管理
小结: 1. Go不需要VM,Go应用程序二进制文件中嵌入了一个小型运行时(Go runtime),可以处理诸如垃圾收集(GC),调度和并发之类的语言功能 Go does not need a VM ...
- Centos 7 Rabbitmq 安装并开机启动
准备工作 安装wget yum install -y wget rabbitmq安装需要依赖erlang,erlang安装参考:https://www.cnblogs.com/swyy/p/11582 ...
- 浅谈自动化构建之gulp
一.gulp的基本使用 gulp是目前最流行的前端自动化构建系统,核心特点高效易用.(这块不过多的废话了,直接上干货了,有兴趣的话,可以查下gulp简介) 步骤如下: yarn init -y yar ...
- 那些我们不知道的 Python 免费学习资料
作者:小R编辑:AI 兔兔 Python 语言因为其易学,以及强大的功能,是很多刚开始学习编程的入门语言的选择之一. Python 语言被列入中小学教材后引起了越来越多人的关注. 希望孩子学习编程的家 ...