传送门: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莫队+异或前缀和的更多相关文章

  1. Codeforces617 E . XOR and Favorite Number(莫队算法)

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

  2. 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 ...

  3. 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 ...

  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 ...

  5. 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 ...

  6. CodeForces - 617E XOR and Favorite Number 莫队算法

    https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...

  7. Codeforces 617E XOR and Favorite Number莫队

    http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...

  8. E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)

    一直都说学莫队,直到现在才学,训练的时候就跪了   T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...

  9. [CQOI2018]异或序列 (莫队,异或前缀和)

    题目链接 Solution 有点巧的莫队. 考虑到区间 \([L,R]\) 的异或和也即 \(sum[L-1]~\bigoplus~sum[R]\) ,此处\(sum\)即为异或前缀和. 然后如何考虑 ...

随机推荐

  1. 【iOS】stringWithFormat 保留小数点位数 float double

    以前就见过,如下: text = [NSString stringWithFormat:@"%.1f", percentageCompleted]; 但一直没在意.刚一时好奇,查了 ...

  2. 【经验分享】ASP.NET 的 Page_Load 执行了2次,真的!

    发现问题 这是来自一位网友的提问: 本着求真务实的态度,我打开了 AppBoxPro 项目,本地调试果然发现 Page_Load 进入了两次! 其实在没测试之前,我就有了大概的方向,因为AppBoxP ...

  3. kube-proxy源码解析

    kubernetes离线安装包,仅需三步 kube-proxy源码解析 ipvs相对于iptables模式具备较高的性能与稳定性, 本文讲以此模式的源码解析为主,如果想去了解iptables模式的原理 ...

  4. 第十章 Centos7-系统进程管理 随堂笔记

    第十章 Centos7-系统进程管理 本节所讲内容: 10.1 进程概述和ps查看进程工具 10.2 uptime查看系统负载-top动态管理进程 10.3 前后台进程切换- nice进程优先级-实战 ...

  5. JDK集合面试20问

    1. HashMap的内部实现原理是什么? HashMap内部实现原理是数组+链表,通过散列算法将key值散列到数组中,如果到相同的位置,则通过拉链法解决散列冲突.在JDK8中新增了红黑树结构,当Ha ...

  6. threejs 学习之

    主要内容: 使用 threejs 创建 20x20 的网格,鼠标移动时,方块跟随移动,点击时在网格任意位置放置方块,按 shift 时,删除当前位置方块. 流程如下: 创建网格 创建一个与网格同样尺寸 ...

  7. 【记忆化搜索】掷骰子 hpuoj

    B. 掷骰子 单点时限: 2.0 sec 内存限制: 512 MB 骰子,中国传统民间娱乐用来投掷的博具,早在战国时期就已经被发明. 现在给你 n 个骰子,求 n 个骰子掷出点数之和为 a 的概率是多 ...

  8. python面向对象初始进阶版 通过一道题带你认识面向对象

    定义一个类 class Person: #公共属性 animal='高级动物' soul='有灵魂' language='语言' def init(self,country,name,sex,age, ...

  9. 【redis】redis基础命令学习集合

    写在前面 Redis是一个高速的内存数据库,它的应用十分广泛,可以说是服务端必学必精的东西.然而,学以致用,无用则无为.学了的东西必须反复的去用,去实践,方能有真知.这篇文章记录了我在redis学习过 ...

  10. ES解决bootstrap checks failed, memory locking requested for elasticsearch process but memory is not locked问题

    问题描述: ERROR: [1] bootstrap checks failed[1]: memory locking requested for elasticsearch process but ...