Codeforces617E(莫队)
E. XOR and Favorite Number
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.
Input
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.
Output
Print m lines, answer the queries in the order they appear in the input.
Examples
input
6 2 3
1 2 1 1 0 3
1 6
3 5
output
7
0
input
5 3 1
1 1 1 1 1
1 5
2 4
1 3
output
9
4
4
Note
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.
题意:询问区间[l,r]内有多少个子区间,其亦或和等于k。
思路:莫队,对于区间[a,b],区间[a,b+1]的ans等于[a,b]的ans加上区间[a,b]内OXR[b+1]^k的个数
对于[a,b]的亦或和,即为XOR[b]^XOR[a-1]
XOR[b]^XOR[a-1] == k <==> XOR[b]^k == XOR[a-1]
因此寻找有多少个XOR[a-1]满足XOR[b]^XOR[a-1] == k ,即寻找有多少个XOR[b]^k
使用一个cnt数组记录当前状态下不同区间亦或和的值出现的次数。
//2017-11-14
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
const int LEN = ; int n, m, k, L, R, a[N], XOR[N], block[N];
long long ans, ANS[N], cnt[N];
struct Node{
int l, r, id;
bool operator<(const Node x) const {
if(block[l] == block[x.l])
return r < x.r;
return block[l] < block[x.l];
}
}q[N]; void add(int x){
ans += cnt[XOR[x]^k];
cnt[XOR[x]]++;
} void del(int x){
cnt[XOR[x]]--;
ans -= cnt[XOR[x]^k];
} int main()
{
//freopen("input.txt", "r", stdin);
while(~scanf("%d%d%d", &n, &m, &k)){
XOR[] = ;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
XOR[i] = XOR[i-] ^ a[i];
block[i] = (i-)/LEN;
}
for(int i = ; i < m; i++){
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q, q+m);
L = , R = , ans = ;
cnt[] = ;
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){
R++;
add(R);
}
while(R > q[i].r){
del(R);
R--;
}
ANS[q[i].id] = ans;
}
for(int i = ; i < m; i++)
printf("%lld\n", ANS[i]);
} return ;
}
Codeforces617E(莫队)的更多相关文章
- Codeforces617E【莫队算法+前缀异或】
题意: 给出一系列数,对每个查询区间,计算有多少个子区间异或为k. 思路: 可以先预处理异或前缀,一个区间[L,R]的异或值=sum[R]^sum[L-1]; 如果当前区间是[a,b],加一个右端点b ...
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- 【填坑向】bzoj2038小Z的袜子 莫队
学莫队必做题,,,但是懒得写.今天来填个坑 莫队水题 莫队实际上就是按一个玄学顺序来离线计算询问,保证复杂度只会多一个n1/2,感觉是玄学(离线算法都很玄学) 易错点:要开long long(卡我半天 ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- NPY and girls-HDU5145莫队算法
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...
随机推荐
- 【Windows】Git自动拉取
原文:https://blog.csdn.net/qq_38375394/article/details/80093003 bat脚本.windows的schtasks,也就是类似于linux的cro ...
- Spring 通知和顾问进行增强
使用顾问增加前置增强和后置增强 <bean id="1" class="目标对象"></bean> <bean id=" ...
- TypeError: Cannot red property 'style' of null 错误解决
错误信息如下: JSP代码如下: <c:if test ="${not empty excelErrors}"> <div id="excelError ...
- Android布局:宽度适应的横向跟随,防止挤掉重要视图
不知道这样的布局该怎么描述,标题也是乱取的..直接上图吧 最近遇到了这样要求的布局: 1.上图中的“标题”长度不定,“状态”标签可能有多个并紧跟在标题右边,“属性”一直居右显示: 2.当“标题”过长, ...
- 【原创】STM32工程新建步骤
1. 新建文件夹 DOC文件夹: 可以存放readme.txt等项目文档 Library文件夹: 可以存放ST库源码,直接复制ST库的源代码到Librar ...
- 使用Laya引擎开发微信小游戏(上)
本文由云+社区发表 使用一个简单的游戏开发示例,由浅入深,介绍了如何用Laya引擎开发微信小游戏. 作者:马晓东,腾讯前端高级工程师. 微信小游戏的推出也快一年时间了,在IEG的游戏运营活动中,也出现 ...
- Go signals 信号
Go中通过os/signals包,可以接受系统信号. package main import "fmt" import "os" import "os ...
- 项目详解2—LVS负载均衡详解
一.负载均衡集群介绍 1.集群 ① 集群(cluster)技术是一种较新的技术,通过集群技术,可以在付出较低成本的情况下获得在性能.可靠性.灵活性方面的相对较高的收益,其任务调度则是集群系统中的核心技 ...
- es6入门2--对象解构赋值
解构赋值:ES6允许按照一定规则从数组或对象中提取值,并对变量进行赋值.说直白点,等号两边的结构相同,右边的值会赋给左边的变量. 一.数组的解构赋值: 1.基本用法 let [a, b, c] = [ ...
- 痞子衡嵌入式:蓝牙芯片厂商三强(Qualcomm&CSR, TI, Nordic)产品一览
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是蓝牙芯片三强厂商的芯片. IoT物联网是未来的趋势,半导体厂商作为IoT产业的上游,主要提供核心的无线芯片,蓝牙是比较主流的协议,下面痞 ...