传送门: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. 【linux日志】【日志分析】linux系统各日志文件的含义

    前段时间太忙,没有来得及管博客,最近时间充裕了,开始更新博客. 因为最近在看linux日志相关内容,把心得分享给大家 linux系统日志文件默认存放路径/var/log/ ls查看此路径下有哪些日志文 ...

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

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

  3. 【iOS】更换证书遇到的问题

    今天给一个项目换证书的时候遇到了这个问题: Code Sign error: Provisioning profile does not match bundle identifier: The pr ...

  4. 二进制文件安装安装flannel

    二进制文件安装安装flannel overlay网络简介 覆盖网络就是应用层网络,它是面向应用层的,不考虑或很少考虑网络层,物理层的问题. 详细说来,覆盖网络是指建立在另一个网络上的网络.该网络中的结 ...

  5. java8-流的操作

    流的操作 流的使用一般包括三件事: 一个数据源来执行一个查询; 一个中间操作链,形成一条流的流水线; 一个终端操作,执行流水线,并能生成结果 中间操作 操作 类型 返回类型 操作参数 函数描述符 fi ...

  6. 用python实现九九乘法表输出-两种方法

    2019-08-05 思考过程:九九乘法表需要两层循环,暂且称之为内循环和外循环,因此需要写双层循环来实现. 循环有for和while两种方式. for循环的实现 for i in range(1,1 ...

  7. C#使用LitJson解析Json数据

    //接受MQ服务器返回的值 private void jieshou(string zhiling, string can1, string can2, string can3, string can ...

  8. The philosophy of ranking

    In the book Decision Quality, one will be trained to have three decision making system; one of them ...

  9. java学习-NIO(二)Buffer

    当我们需要与 NIO Channel 进行交互时, 我们就需要使用到 NIO Buffer, 即数据从 Buffer读取到 Channel 中, 并且从 Channel 中写入到 Buffer 中.缓 ...

  10. Sql Or NoSql,看完这一篇你就懂了

    前言 你是否在为系统的数据库来一波大流量就几乎打满CPU,日常CPU居高不下烦恼?你是否在各种NoSql间纠结不定,到底该选用那种最好?今天的你就是昨天的我,这也是写这篇文章的初衷. 这篇文章是我好几 ...