Codeforces Round #340 (Div. 2) E 莫队+前缀异或和
4 seconds
256 megabytes
standard input
standard output
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, ..., aj is equal to k.
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.
Print m lines, answer the queries in the order they appear in the input.
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
In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query.
In the second sample xor equals 1 for all subarrays of an odd length.
题意:给你一个长度为n的序列 q个查询[l,r] 问[l,r] 有多少个子区间的异或和为k
题解:莫队+前缀异或和
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#define ll __int64
using namespace std;
int n,m,k;
struct node
{
int l,r,id;
}N[];
int p[];
int block;
int a[];
int x[];
int mp[];
ll ans=;
ll re[];
int cmp(struct node aa,struct node bb)
{
if(p[aa.l]==p[bb.l])
return aa.r<bb.r;
else
return p[aa.l]<p[bb.l];
}
void update(int w,int h)
{
if(h==){
ans=ans+mp[x[w]^k];
mp[x[w]]++;
}
else
{
mp[x[w]]--;
ans=ans-mp[x[w]^k];
}
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
x[]=;
mp[]=;
for(int i=;i<=n;i++)
x[i]=a[i]^x[i-];
for(int i=;i<=m;i++){
scanf("%d %d",&N[i].l,&N[i].r);
N[i].id=i;
}
block=(int)sqrt((double)n);
for(int i=;i<=n;i++)
p[i]=(i-)/block+;
sort(N+,N++m,cmp);
ans=;
for(int i=,l=,r=;i<=m;i++)
{
for(;r<N[i].r;r++) update(r+,);
for(;l>N[i].l;l--) update(l-,);// 取异或的原因
for(;r>N[i].r;r--) update(r,-);
for(;l<N[i].l;l++) update(l-,-);//
re[N[i].id]=ans;
}
for(int i=;i<=m;i++)
printf("%I64d\n",re[i]);
return ;
}
/*
10 2 0
0 0 0 0 0 0 0 0 0 0
2 8
2 8
*/
Codeforces Round #340 (Div. 2) E 莫队+前缀异或和的更多相关文章
- [Codeforces Round #340 (Div. 2)]
[Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...
- 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 ...
- 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 (莫队)
题目链接:http://codeforces.com/contest/617/problem/E 题目大意:有n个数和m次查询,每次查询区间[l, r]问满足ai ^ ai+1 ^ ... ^ aj ...
- 莫队 Codeforces Round #340 (Div. 2) E
题目大意:给你一个长度为n的序列,有m个询问,每次询问一个区间[L,R],表示这个区间内,有多少的a[i]^a[i+1].....^a[j]=k. 思路:莫队去搞就好了 我们定义pre[i]=a[1] ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Round #340 (Div. 2) E XOR and Favorite Number 莫队板子
#include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...
- Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力
C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...
随机推荐
- YAML 基础
YAML 基础 简介 对象 数组 常量 引用 1. 简介 YAML 是专门用来写配置文件的语言,非常简洁和强大! 它的基本语法规则有: 大小写敏感: 使用缩进表示层级关系: 缩进时不允许使用 Tab ...
- 2017年4月8日Office 365 使用CSV文件导入邮件组
国内版 第一步: Import-Module msonline Connect-MsolService 输入用户名密码 第二步: Get-MsolUser" 第三步: Set-Executi ...
- 导出Office365中的组及成员
Set-ExecutionPolicy unrestricted $cred = Get-Credential $session = New-PSSession -ConfigurationName ...
- 4.hive的外部表和内部表
1.外部表和内部表区别 创建表时:创建内部表时,会将数据移动到数据仓库指向的路径:若创建外部表,仅记录数据所在的路径, 不对数据的位置做任何改变. 删除表时:在删除表的时候,内部表的元数据和数据会被一 ...
- Scrum立会报告+燃尽图(Beta阶段第六次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2388 项目地址:https://coding.net/u/wuyy694 ...
- 第一次c++团队合作作业期间第一篇随笔
分析了自己分到的任务,我的理解是这样的:首先要生成程序主框架,在主框架中进行地图的描绘.我应该是先进行地图的拼接,把建筑物和地面都拼接好.然后再在地图上显示出英雄和小兵.同时还要在主框架中分析了自己分 ...
- 第二次c++作业
用c语言实现电梯问题的方法: 先用一堆变量存储各种变量,在写一个函数模拟电梯上下移动载人放人的过程. c++: 构造一个电梯的类,用成员函数实现电梯运作的过程. 对c和c++的理解太浅,并没有感觉到用 ...
- java沙盒入门
程序员写一个Java程序,默认的情况下你可以访问任意的机器资源,比如读取,删除一些文件或者网络操作等.当你把程序部署到正式的服务器上,系统管理员要为服务器的安全承担责任,那么他可能不敢确定你的程序会不 ...
- 如何在一台 web 服务器上注册CA证书
试验环境介绍(CA的主机为192.168.23.10.httpd的主机为:192.168.23.11) 1:新建一台web服务器,主机名为www yum install -y httpd 2:生成 ...
- openssl 编程
背景: 生成私钥.公钥 --> 生成AES-key seed[32], iv[16] --> 公钥加密ASE-key, IV,传给Server --> Server用私钥解密,得到A ...