Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】
任意门:http://codeforces.com/problemset/problem/617/E
E. XOR and Favorite Number
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.
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
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 的数列,M次查询 ( l,r )内有多少对 ( i, j )使得 ai ^ ai+1 ^ ... ^ aj = K;
解题思路:
这里的区间查询是离线的,可以用传说中的莫队算法(优雅而华丽的暴力算法)。
异或的题目有一个很巧妙的地方就是利用 a^b^a = b 这个性质。
这道题目我们也要预处理一下 sum(i) 前 i 个数的异或和, 那么 ai ^ ai+1 ^ ... ^ aj = sum( i - 1) ^ sum( j ) 了。
接下来我们就可以按照查询的区间用莫队算法遍历一遍,同时记录当前区间某个前缀和的出现次数;
根据 sum( i - 1) ^ sum( j ) = K ,K ^ sum( i - 1 ) = sum( j ) || K ^ sum( j ) = sum( i - 1) ,由符合条件的前缀和次数来推出符合条件的( i,j )的个数啦。
Tip:
听了大神的课,这道题有两个坑:
1、答案的数据的数据范围是爆 int 的;
2、虽然是1e6 的 K,但异或的结果要大于 1e6 ;
AC code:
#include <bits/stdc++.h>
#define LL long long int
using namespace std;
const int MAXN = <<; struct node
{
int l, r, id; //区间和查询编号
}Q[MAXN]; //记录查询数据(离线) int pos[MAXN]; //记录分块
LL ans[MAXN]; //记录答案
LL flag[MAXN]; //维护前缀异或和出现的次数
int a[MAXN]; //原本数据
int L=, R; //当前区间的左右结点
LL res; //储存当前区间的值
int N, M, K;
bool cmp(node a, node b) //排序
{
if(pos[a.l]==pos[b.l]) return a.r < b.r; //只有左结点在同一分块才排右结点
return pos[a.l] < pos[b.l]; //否则按照左结点分块排
}
void add(int x)
{
res+=flag[a[x]^K];
flag[a[x]]++;
}
void del(int x)
{
flag[a[x]]--;
res-=flag[a[x]^K];
}
int main()
{
scanf("%d%d%d", &N, &M, &K);
int sz = sqrt(N);
for(int i = ; i <= N; i++){ //读入数据
scanf("%d", &a[i]);
a[i] = a[i]^a[i-]; //计算前缀异或和
pos[i] = i/sz; //分块
}
for(int i = ; i <= M; i++){ //读入查询
scanf("%d%d", &Q[i].l, &Q[i].r);
Q[i].id = i;
}
sort(Q+, Q++M, cmp);
flag[] = ;
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] = res;
}
for(int i = ; i <= M; i++){
printf("%lld\n", ans[i]);
}
}
Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】的更多相关文章
- 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 莫队板子
#include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...
- 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. XOR and Favorite Number
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
- 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 E. XOR and Favorite Number(莫队算法)
题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...
- 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 Round #340 (Div. 2)]
[Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...
随机推荐
- oracle 备份恢复篇(二)---rman 增备恢复--不完全恢复
一,环境准备 全备脚本: export TMP=/tmp export TMPDIR=$TMP export ORACLE_BASE=/u01 export ORACLE_SID=prod expor ...
- vue中watch的使用
vue中watch的使用 vue中的watch是一个比较重要的概念,通过他我们可以检测data的变化,下面进行详细的介绍. watch定义方式如下: {[key: string]: string | ...
- Apache Beam的架构概览
不多说,直接上干货! Apache Beam是一个开源的数据处理编程库,由Google贡献给Apache的项目,前不久刚刚成为Apache TLP项目.它提供了一个高级的.统一的编程模型,允许我们通过 ...
- 【程序员技术练级】学习一门脚本语言 python(二)遍历本地文件系统
这篇将讲述怎么使用python来遍历本地文件系统,并把文件按文件大小从小到大排序的一个小例子 在这个例子中,主要会用到python内置的和OS模块的几个函数: os.walk() : 该方法用来遍历指 ...
- TOJ 2815 Connect them (kruskal+并查集)
描述 You have n computers numbered from 1 to n and you want to connect them to make a small local area ...
- 《HTTP权威指南》之HTTP连接管理及对TCP性能的考虑
在上一篇博客中(<HTTP权威指南>之HTTP相关概念详解)我们简单对HTTP相关的基本概念做了一些简单的了解,但未对HTTP连接管理的内容做一些详细的介绍.本篇博客我们就一起来看一下HT ...
- unity接入讯飞教程
[全流程]<按照这个流程做即可,有不懂得可以看下面的2个><这个是<eclipse>> http://blog.csdn.net/qq_15267341/artic ...
- IntelliJ IDEA 使用 LiveEdit 插件实现实时可视化前端开发
之前因为公司很多都是C#后台项目,所以一直用的Visual Studio开发.而在VS里会自带实时刷新功能,即:在IDE中修改的CSS代码会同步反映在页面上,而不用我们手动F5刷新. 先在因为在考虑做 ...
- MYSQL冷知识——ON DUPLICATE KEY 批量增删改
一 有个需求要批量增删改,并且是混合的,也就是仅不存在才增. 删简单,因为有个deleteStaute之类的字段,删除本质上就是就是一个修改 所以就是实现批量混合增改,然而组长说mysql不支持混合增 ...
- 吴恩达《Machine Learning Yearning》总结(31-40章)
31.解读学习曲线:其他情况 下图反映了高方差,通过增加数据集可以改善. 下图反映了高偏差和高方差,需要找到一种方法来同时减少方差和偏差. 32.绘制学习曲线 情况:当数据集非常小时,比如只有100个 ...