CodeForces-617E XOR And Favorite Numbers(莫队)
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, ..., ajis 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
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.
题解:首先要知道 a^b=c 等价于 a=c^b; 我们用a[i]记录前I个数的异或和,然后离线处理所有区间(对于所有区间我们按L所在块为第一排序,该询问的r为第二排序,对所有询问区间排序);
对于新增加的一个数我们加上前区间异或等于k^s的数的个数,然后更新一下异或为s的数量,对于一个需要去掉的数,我需要先新更新这个数的数量,然后减去k^s的数量即可;
离线跑一遍,
参考代码为:
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<iostream>
- #include<algorithm>
- #define LL long long
- using namespace std;
- const int Max = ;
- const int MAXM = <<;
- struct Point
- {
- int L ,R,Id;
- } a[Max];
- LL sum[Max],ans[Max],k;
- int n,m,L,R;
- LL cnt[MAXM],ant;
- bool cmp(Point b,Point c)
- {
- return b.L/==c.L/? b.R<c.R:b.L<c.L;
- }
- void Dec(LL s)
- {
- --cnt[s];
- ant-=cnt[s^k];
- }
- void Inc(LL s)
- {
- ant += cnt[s^k];
- cnt[s]++;
- }
- int main()
- {
- scanf("%d %d %lld",&n,&m,&k);
- LL data;
- for(int i=;i<=n;i++) scanf("%lld",&sum[i]),sum[i]^=sum[i-];
- for(int i=;i<=m;i++) scanf("%d %d",&a[i].L,&a[i].R),a[i].Id = i,a[i].L--;
- sort(a+,a+m+,cmp);
- L=,R=,cnt[]=,ant=;
- for(int i=;i<=m;i++)
- {
- while(R<a[i].R) Inc(sum[++R]);
- while(R>a[i].R) Dec(sum[R--]);
- while(L<a[i].L) Dec(sum[L++]);
- while(L>a[i].L) Inc(sum[--L]);
- ans[a[i].Id]=ant;
- }
- for(int i=;i<=m;i++) printf("%lld\n",ans[i]);
- return ;
- }
CodeForces-617E XOR And Favorite Numbers(莫队)的更多相关文章
- 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/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)
题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...
- 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 ...
- Codeforces - 617E 年轻人的第一道莫队
我对莫队算法最为纠结的地方就是区间端点处,应该是像代码里那样理解吧 cnt[i]表示i出现的次数 maxn开2e6比较保险 /*H E A D*/ struct Query{ int l,r,id; ...
- Codeforces - 617E 年轻人的第一道莫队·改
题意:给出\(n,m,k,a[1...n]\),对于每次询问,求\([l,r]\)中\(a[i] \ xor \ a[i+1] \ xor \ ...a[j],l<=i<=j<=r\ ...
- 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 ...
- CodeForces - 617E 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 g ...
随机推荐
- matlab中的eval函数使用
matlab中的eval函数使用 在matlab的命令行窗口中输入help eval命令回车就可以看到eval函数的官方解释,大概的意思就是执行matlab中的表达式,计算expression表示的代 ...
- CSS(6)---通俗讲解浮动(float)
CSS(6)---通俗讲解浮动(float) CSS有三模块:盒子模型.浮动 .定位.上篇博客有讲到 盒子模型地址:CSS(5)---通俗讲解盒子模型 一.理解浮动 1.概念 概念 浮动可以理解为让某 ...
- Centos 6/7 常用命令总结 (基础)
Centos 6/7 常用命令总结 (基础): 参考链接:https://www.cnblogs.com/linhaifeng/p/6045600.html 目录介绍: a) bin目录:用来存放常用 ...
- (C#)WPF:Grid面板介绍
Grid:网格面板 Grid和其他各个Panel比较起来,功能最多也最为复杂.要使用Grid,首先要向RowDefinitions和ColumnDefinitions属性中添加一定数量的RowDefi ...
- nyoj 116 士兵杀敌(二)(线段树、单点更新)
士兵杀敌(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军经常 ...
- 搭建Nginx七层反向代理
基于https://www.cnblogs.com/Dfengshuo/p/11911406.html这个基础上,在来补充下七层代理的配置方式.简单理解下四层和七层协议负载的区别吧,四层是网络层,负载 ...
- ansible-template
template简介 template功能: 根据模板文件动态生成对应的配置文件 template文件必须存放于templates目录下,且命名为 .j2 结尾 ansible的template模板使 ...
- objc反汇编分析,block函数块为何物?
上一篇向大家介绍了__block变量的反汇编和它的伪代码,本篇函数块block,通常定义成原型(^){},它在反汇编中是什么东西. 我们先定义将要反汇编的例子,为减少篇幅例子采用non-arc环境. ...
- 使用navicat操作PostPreSql创建表并设置主键自增和触发器
使用navicat操作PostPreSql创建表并设置主键自增和触发器 1).创建递增序列 2).创建表,使用序列,设置主键递增 3)定义触发函数 自动生成时间戳函数 CREATE OR REPLAC ...
- ecryptfs
ecryptfs是一种加密文件系统.该文件系统的内容在传输和储存时以密文形式存在.只有在mount时用密钥解密才能得到明文.利用这个特性,我们可以用他来对软件镜像中的部分敏感文件系统进行加密,然后打包 ...