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\)即为异或前缀和. 然后如何考虑 ...
随机推荐
- [解决方案]IIS配置后报错404,500,502等系列问题
很多时候刚部署完服务器的IIS以后,第一次部署站点都会出现形形色色,各种各样的报错问题,但这些问题大同小异,我这里就给大家提供的解决的方案,以达到以不变应万变的效果 目的:让大家学会处理类似问题的方法 ...
- IIS网站服务器性能优化攻略
Windows Server自带的互联网信息服务器(Internet Information Server,IIS)是架设网站服务器的常用工具,它是一个既简单而又麻烦的东西,新手都可以使用IIS架设一 ...
- c&c服务器(command and control server)
远程命令和控制服务器,目标机器可以接收来自服务器的命令,从而达到服务器控制目标机器的目的.该方法常用于病毒木马控制被感染的机器.
- 【Git】Found a swap file by the name ".git/.MERGE_MSG.swp"
最近合并分支的时候总是遇到这个问题,导致合并之后还需要再提交一次--有点烦-- 解决方案: 在项目根目录(如/StudioProjects/demo/Leave)下,找到 .git/.MERGE_MS ...
- Spring JdbcTemplate之使用详解
最近在项目中使用到了 Spring 的 JdbcTemplate, 中间遇到了好多坑, 所以花一些时间对 JdbcTemplate 的使用做了一个总结, 方便以后自己的查看.文章中贴出来的API都是经 ...
- 网站安装SSL证书成为影响SEO排名的重要因素之一
百度谷歌先后发声明倡导站长们使用https链接,同样的网站,https站点要比http站点拥有更好的排名权重.https已经是网站SEO必须要考虑的环节之一了,而https的必要条件就是安装SSL证书 ...
- vue-cli3.0创建项目报npm install --loglevel error 踩坑的那把辛酸泪
创建项目 vue create vue-pro 然后如下图 一开始以为是npm的问题,卸载了Mac的node ,安装nvm,然后再安装node (可参考: Mac中nvm的安装和使用 https: ...
- 启动Eclipse提示找不到虚拟机
由于硬盘坏了,把所有东西都清光了,今天重新安装Eclipse,出现了一点小插曲 安装的时候出现了这个画面,以前安装也是照着[软件安装管家]的发布装的,幸好还懂得几个英文单词,看了一下提示信息,直译:[ ...
- 【原创】TextCNN原理详解(一)
最近一直在研究textCNN算法,准备写一个系列,每周更新一篇,大致包括以下内容: TextCNN基本原理和优劣势 TextCNN代码详解(附Github链接) TextCNN模型实践迭代经验总结 ...
- 使用selenium进行爬取掘金前端小册的数据
Selenium 简介 百度百科介绍: Selenium [1] 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, ...