codeforce617E-XOR and Favorite Number莫队+异或前缀和
传送门:http://codeforces.com/contest/617/problem/E
参考:https://blog.csdn.net/keyboarderqq/article/details/55807154
题意:
给出一系列数,对每个查询区间,计算有多少个子区间异或为k。
思路:
可以先预处理异或前缀,一个区间[L,R]的异或值=a[R]^a[L-1];
其中,a为异或前缀和数组;
如果当前区间是[A,B],加一个右端点B+1,那么这个 B+1 的贡献就是[A,B]区间内有多少个a[x] = a[B+1]^k
那么我们可以每次记录cnt[a[x]]即cnt[a[B+1]^k],并记录cnt[a[b+1]]++,同理左区间。
那么我们就可以使用莫队算法。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = <<; ll a[maxn], flag[maxn];
int pos[maxn];
ll Ans = ;
ll res[maxn];
int m,n,k; struct node {
int l,r;
int id;
}Q[maxn]; bool cmp(node a,node b)
{
if(pos[a.l]==pos[b.l])
return a.r < b.r;
else return pos[a.l] < pos[b.l];
} 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("%I64d", &a[i]);
a[i] = a[i-] ^ a[i];
pos[i] = (i-) / sz + ;
}
flag[] = ;
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 = ;
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){
del(r);
r--;
}
while(r < Q[i].r){
r++;
add(r);
}
res[Q[i].id] = Ans;
}
for(int i=; i<=m; i++)
{
printf("%I64d\n",res[i]);
}
return ;
}
codeforce617E-XOR and Favorite Number莫队+异或前缀和的更多相关文章
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- 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 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 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 莫队算法
E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...
- 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的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)
一直都说学莫队,直到现在才学,训练的时候就跪了 T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...
- [CQOI2018]异或序列 (莫队,异或前缀和)
题目链接 Solution 有点巧的莫队. 考虑到区间 \([L,R]\) 的异或和也即 \(sum[L-1]~\bigoplus~sum[R]\) ,此处\(sum\)即为异或前缀和. 然后如何考虑 ...
随机推荐
- javaweb入门---web服务器与HTTP协议基础
上文web基础简介了web到底是什么,以及身为Java开发人员需要掌握的地方.本文将解答web服务器是什么,怎么使用?还有关于http协议的基础知识. web服务器 web服务器的大概念很广泛,但是通 ...
- 【Android】INSTALL_FAILED_UPDATE_INCOMPATIBLE
多是因为已经安装过该 apk 文件了,一般卸载了重新运行就 OK 了.
- Downgrade extraction on phones running Android 7/8/9
Now it's more and more difficult for forensic tools to extract evidence from smartphone running Andr ...
- 一道看似简单的go程序的深入分析
先上代码: func main() { var a [10]int for i := 0; i < 10; i++ { go func(i int) { for { a[i]++ } }(i) ...
- JavaSE之——并没有多维数组
近日在读<疯狂Java讲义>精粹第二版,部分语述摘自其中,自己边敲边理解 前言 我们知道,Java语言支持的类型有两种: 1.基本类型(即八大基本数据类 ...
- 最新 Flutter 团队工程师中文演讲 | Flutter 的性能测试和理论
本视频为 Google Flutter 团队的软件工程师 Xiao Yu 在 2018 谷歌开发者大会做的演讲,演讲题目是<Flutter 的性能测试和理论>. 这个视频里将会通过近半个小 ...
- h5微信浏览器复制粘贴--ios兼容问题的解决方法(clipboard.js插件)
前段时间在做微信h5的时候,遇到了ios兼容,使用clipboard.js插件完美解决 下载地址:下载地址: https://github.com/zenorocha/clipboard.js cnd ...
- volatile关键字的详解-并发编程的体现
xl_echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!! 参 ...
- rabbitmq生产者的消息确认
通过Publisher Confirms and Returns机制,生产者可以判断消息是否发送到了exchange及queue,而通过消费者确认机制,Rabbitmq可以决定是否重发消息给消费者,以 ...
- MyBatis的parameterType传入参数类型
在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配置相应的 ...