codeforces 617E E. XOR and Favorite Number(莫队算法)
题目链接:
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.
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]之间有多少对i和j满足a[i]^a[i+1]^...^a[j]=k;
思路:暴力绝对绝对绝对是不行的,所有就要用复杂度更低的算法啦,所以我就去学了莫队算法,莫队算法处理的是一种离线算法,是对询问进行分块排序来优化查询的算法;
这题还要对异或运算要了解;我要去写个位运算的小总结;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+;
struct node
{
friend bool operator< (node x,node y)
{
if(x.pos==y.pos)return x.r<y.r;
return x.l<y.l;
}
int l,r,id;
int pos;
};
node qu[N];
int n,m,k;
int b[N],num[*N];
long long ans[N];
void solve()
{
memset(num,,sizeof(num));
int le=,ri=;
long long temp=;
for(int i=;i<=m;i++)
{
while(ri<qu[i].r)
{
ri++;
temp+=num[b[ri]^k];//num[]数组是当前le到ri内每个b[i](le<=i<=ri)异或一个要添加的数==k的数目;所以num[b[ri]^k]就是在已有的区间上再加一个
num[b[ri]]++;//ri前能和ri异或==k的数目;后面这些操作一样
}
while(ri>qu[i].r)
{
num[b[ri]]--;
temp-=num[b[ri]^k];
ri--;
}
while(le>qu[i].l-)
{
le--;
temp+=num[b[le]^k];
num[b[le]]++;
}
while(le<qu[i].l-)
{
num[b[le]]--;
temp-=num[b[le]^k];
le++;
}
ans[qu[i].id]=temp;
}
}
int main()
{
b[]=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&b[i]);
b[i]^=b[i-];
}
int s=sqrt(n);
for(int i=;i<=m;i++)
{
scanf("%d%d",&qu[i].l,&qu[i].r);
qu[i].id=i;
qu[i].pos=qu[i].l/s;
}
sort(qu+,qu+m+);
solve();
for(int i=;i<=m;i++)
{
cout<<ans[i]<<"\n";
}
return ;
}
codeforces 617E 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 ...
- 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 - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- Codeforces 617E:XOR and Favorite Number(莫队算法)
http://codeforces.com/problemset/problem/617/E 题意:给出n个数,q个询问区间,问这个区间里面有多少个区间[i,j]可以使得ai^ai+1^...^aj ...
- Codeforces 617E XOR and Favorite Number莫队
http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- 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 ...
- E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)
一直都说学莫队,直到现在才学,训练的时候就跪了 T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
随机推荐
- winerror.h中的内容(可以查看last error对应)
/************************************************************************* ** winerror.h -- error co ...
- SWD下载调试填坑,SWD连接丢失问题解决
野火SWD下载器,设置好以后,第一次下载成功,莫名其妙丢失连接,发现在复位状态可以连接(惊奇) 网络上搜索到把Boot0和Boot1置高,就可以把程序下载到RAM里, 能下载以后就好办了,把程序里SW ...
- java拾遗1----XML解析(一) DOM解析
XML解析技术主要有三种: (1)DOM(Document Object Model)文档对象模型:是 W3C 组织推荐的解析XML 的一种方式,即官方的XML解析技术. (2)SAX(Simple ...
- debian dhcp配置
1 将/etc/network/interfaces中设置成dhcp auto eth0iface eth0 inet dhcp 2 重启网络服务 /etc/init.d/networking res ...
- Effective Java 读书笔记(一):使用静态工厂方法代替构造器
这是Effective Java第2章提出的第一条建议: 考虑用静态工厂方法代替构造器 此处的静态工厂方法并不是设计模式,主要指static修饰的静态方法,关于static的说明可以参考之前的博文&l ...
- ElasticSearch(二十六)修改分词器及定制自己的分词器
1.默认的分词器 standard 分词器 standard tokenizer:以单词边界进行切分standard token filter:什么都不做lowercase token filter: ...
- [note]可持久化Trie
可持久化Trie 参考可持久化线段树的思想,修改的时候先直接复制,再对需要修改的点新建节点 可持久化Trie也是同样的做法,假设现在需要在原本Trie的基础上插入一个字符串 先把上个Trie的对应节点 ...
- genymotion device manager列表没有
1.第一种原因:链接Genymotion官网的网络超时,无法加载Genymotion device列表,解决办法百度一下:配置Genymotion代理服务器,联网下载 2.第二种可能:检查是否正确安装 ...
- 排序算法-python版
总结了一下常见集中排序的算法 归并排序 归并排序也称合并排序,是分治法的典型应用.分治思想是将每个问题分解成个个小问题,将每个小问题解决,然后合并. 具体的归并排序就是,将一组无序数按n/2递归分解成 ...
- linux下查找最耗iowait的进程
抓哪个进程干坏事前要先停掉syslogservice syslog stop 打开block dump:echo 1 > /proc/sys/vm/block_dump 统计:dmesg | e ...