CodeForces - 617E XOR and Favorite Number (莫队+前缀和)
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, ..., ajis equal to k.
Input
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.
Output
Print m lines, answer the queries in the order they appear in the input.
Examples
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 题意:
询问区间内异或和刚好为k的字段个数。
思路:
莫队+前缀和。
这个前缀和比较套路,用的是前缀异或和。
字段【l,r】的异或和就是pre[r]^pre[l-1],
这种情况下我们在莫队的过程中记录l,r的pre[i]出现的次数,就可以完成更新了。 注意当L<q[i].l时,要先让记录per[L]出现次数的个数减一。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int num[maxm*],pre[maxn],a[maxn]; struct node{
int l,r;
int id;
}q[maxn];
ll ans[maxn];
ll anss;
int block; bool cmp(node a,node b){
if(a.l/block!=b.l/block){return a.l<b.l;}
return a.r<b.r;
} int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
pre[i]=pre[i-]^a[i];
} block=sqrt(n); for(int i=;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
} sort(q+,q++m,cmp);
int L=,R=;
anss=;
num[]=;
for(int i=;i<=m;i++){
while(L<q[i].l){
int t=k^pre[L-];
num[pre[L-]]--;///注意语句顺序
anss-=num[t];
L++;
}
while(R>q[i].r){
int t=k^pre[R];
num[pre[R]]--;
anss-=num[t];
R--;
}
while(L>q[i].l){
L--;
int t=k^pre[L-];
anss+=num[t];
num[pre[L-]]++;
}
while(R<q[i].r){
R++;
int t=k^pre[R];
anss+=num[t];
num[pre[R]]++;
}
ans[q[i].id]=anss;
}
for(int i=;i<=m;i++){
printf("%lld\n",ans[i]);
}
return ;
}
CodeForces - 617E XOR and Favorite Number (莫队+前缀和)的更多相关文章
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- Codeforces 617E XOR and Favorite Number莫队
http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- 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 ...
- 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 617E E. XOR and Favorite Number(莫队算法)
题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-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 ...
- 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 ...
随机推荐
- flask 与celery
在flask 中使用celery 是特别简单的,celery官网都没有特别介绍如何使用. 使用celery首先要知道怎么配置celery. 1. 实例化celery celery = Celery ...
- JAVA高级特性--String/StringBuffer/Builder
String String对象一旦创建就不能改变 是常量 需要进行大量字符串操作应采用StringBuffer/StringBuilder 最终结果转换成String对象 StringBuffer ...
- C# 从零开始写 SharpDx 应用 初始化dx修改颜色
原文:C# 从零开始写 SharpDx 应用 初始化dx修改颜色 版权声明:博客已迁移到 https://blog.lindexi.com 欢迎访问.如果当前博客图片看不到,请到 https://bl ...
- python 字典创建
- HSV转换
HSV中H为色调(Hue).S为饱和度(Saturation).V为亮度(Value)三个分量构成 RGB和HSV颜色空间中进行图像处理的案例,HSV颜色空间分离图像中每一个像素的值或V分量.这个分量 ...
- uda 4.C++面向对象编程
Python vs C++ 对比课 在本课中,你将学习如何用 C++ 编写类.像以前的课程一样,你需要比较 Python 的编程方式和 C++ 中编程方式的不同. 我们直接看例子.下面是一个名为 ...
- @codeforces - 590E@ Birthday
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 n 个互不相同的只包含 'a', 'b' 的字符串. 请选 ...
- 如何入门 C++ AMP 教程
本文告诉大家如何写一个 Helloworld 程序. 首先打开 VisualStudio ,大概现在也没有人还在用 VisualStudio 2013 了,所以我就不需要告诉大家需要用哪个版本的 Vi ...
- js循环遍历数组(对象)
1,for循环 对于循环应该是最常用的一种遍历方式了,通常用来遍历数组结构. let arr = [a,b,d];for (let i=0; i<arr.length; i++){ consol ...
- css 文字超出部分隐藏
未做隐藏处理 执行结果: 1.1行超出部分省略号 效果: 2.多行超出部分隐藏(目前只能在chrome浏览器中使用,其他浏览器不兼容) 效果: -webkit-line-clamp 属性定义显示行数可 ...