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 thatl ≤ 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个数和m次询问,每一询问会有一个L和R,表示所询问的区间,
问在这个区间中有多少个连续的子区间的亦或和为k
假设我们现在有一个前缀异或和数组sum[],现在我们要求区间[L,R]的异或的值,
用sum数组表示就是sum[L-1]^sum[R]==K,或者说是K^sum[R]==sum[L-1]
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e6 + ;
int n, m, k, L, R, sz, a[maxn];
LL sum[maxn], ans, ANS[maxn];
struct node {
int l, r, id;
node() {}
node(int l, int r, int id): l(l), r(r), id(id) {}
bool operator <(const node & a)const {
if (l / sz == a.l / sz) return r < a.r;
return l < a.l;
}
} qu[maxn];
void add(int x) {
ans += sum[a[x] ^ k];
sum[a[x]]++;
}
void del(int x) {
sum[a[x]]--;
ans -= sum[a[x] ^ k];
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i <= n ; i++) {
scanf("%d", &a[i]);
a[i] ^= a[i - ];
}
for (int i = ; i <= m ; i++) {
scanf("%d%d", &qu[i].l, &qu[i].r);
qu[i].l--;
qu[i].id = i;
}
sz = (int)sqrt(n);
sort(qu + , qu + m + );
L = , R = ;
for (int i = ; i <= m ; i++) {
while(L > qu[i].l) add(--L);
while(R < qu[i].r) add(++R);
while(L < qu[i].l) del(L++);
while(R > qu[i].r) del(R--);
ANS[qu[i].id] = ans;
}
for (int i = ; i <= m ; i++)
printf("%lld\n", ANS[i]);
return ;
}
最新文章
- Tomcat部署的时候 unpackWARs=";true";
- Jdbc如何从PostgreSql读取海量数据?PostgreSql源代码分析纪录
- 10、面向对象以及winform的简单运用(isMdicontainer的设置、timer控件进行倒计时的制作)
- Java Collections的排序之二
- jquery css快捷方法
- ECSHOP 数据库结构说明 (适用版本v2.7.3)
- poj2484--A Funny♂Game
- PowerShell_零基础自学课程_9_高级主题:静态类和类的操作
- UIImage 图片处理:截图,缩放,设定大小,存储
- Qt入门之基础篇(1):Qt4及Qt5的下载与安装
- Tcl与Design Compiler (七)——环境、设计规则和面积约束
- Python 列表(List) 的三种遍历(序号和值)方法
- PMBook - 以考PMP为项目,整理项目章程
- vue.js学习资料
- 【UML】NO.55.EBook.8.UML.3.001-【UML和模式应用 第3版】
- java并发编程系列一、多线程
- javascript性能优化之避免重复工作
- Chapter 3 Phenomenon——15
- Java学习笔记之:Spring MVC 环境搭建
- 创建ros的程序包--3