Codeforces Round #340 (Div. 2) E. XOR and Favorite Number
2016-03-01 10:07
429 查看
E. XOR and Favorite Number
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
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
input
output
input
output
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.
题意:有一个长度为n的数组,有m个查询,问每个查询l,r连续异或和为k的有多少种
分析:设a[i]为前面所有数的异或和,那么[l,r]之间的异或和便为a[l-1]^a[r]
如果知道了[l,r]的异或和为k的种数,接下来便可以在O(1)的时间内得到[l,r-1].[l.r+1],[l-1,r],
[l+1,r]的异或种数,利用莫队算法便能轻松解决
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
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
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.
题意:有一个长度为n的数组,有m个查询,问每个查询l,r连续异或和为k的有多少种
分析:设a[i]为前面所有数的异或和,那么[l,r]之间的异或和便为a[l-1]^a[r]
如果知道了[l,r]的异或和为k的种数,接下来便可以在O(1)的时间内得到[l,r-1].[l.r+1],[l-1,r],
[l+1,r]的异或种数,利用莫队算法便能轻松解决
#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define INF 0x3f3f3f3f #define inf -0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define PI acos(-1.0) #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define mem(a, b) memset(a, b, sizeof(a)) typedef long long ll; const int maxn=101000; const int MAXM=2000100; struct node{ int l,r,id; }line[maxn]; __int64 ans[maxn],num[MAXM]; int a[maxn]; int unit; bool cmp(node u,node v){ if(u.l/unit!=v.l/unit) return u.l/unit<v.l/unit; return u.r<v.r; } int n,m,k; void solve(){ int L=1,R=0; long long temp=0; for(int i=1;i<=m;i++){ while(R<line[i].r){ R++; temp+=num[a[R]^k]; num[a[R]]++; } while(R>line[i].r){ num[a[R]]--; //num数值的改变要特别注意 temp-=num[a[R]^k]; R--; } while(L>line[i].l){ L--; temp+=num[a[L]^k]; num[a[L]]++; } while(L<line[i].l){ num[a[L]]--; temp-=num[a[L]^k]; L++; } ans[line[i].id]=temp; } } int main(){ scanf("%d%d%d",&n,&m,&k); a[0]=0; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); a[i]=a[i]^a[i-1]; } for(int i=1;i<=m;i++){ scanf("%d%d",&line[i].l,&line[i].r); line[i].l--; line[i].id=i; } unit=(int)sqrt(n); sort(line+1,line+m+1,cmp); solve(); for(int i=1;i<=m;i++) printf("%I64d\n",ans[i]); return 0; }
相关文章推荐
- JAVA 生成随机数,并根据概率、比率
- 如何做到将apk大小减少6M
- Windows驱动开发(中间层)
- POJ 1384 Piggy-Bank(完全背包问题)
- tomcat架构分析(connector BIO 实现)
- 百度地图API,根据经纬度实现车辆移动轨迹绘制
- PAT-A1042 Shuffling Machine(简单模拟)
- Android xutil3.0完全解析
- Zookeeper实现集群和负载均衡---(5)Zabbix集成Zookeeper示例
- 在Ti的DSP程序中使用C++编程
- Java并发编程:线程池的使用
- Android自定义按钮点击效果
- jQuery validate插件的主要功能
- Linux 下使用 adb 和 fastboot 命令
- RecyclerView的itemView的点击效果
- 什么是 480i、576i、480p、1080i、720p?什么是 HDTV?
- MyBatis/Ibatis中#和$的区别
- 关于k3新增单据按F7选择物料非常缓慢的处理
- question_002-JAVA之Properties
- mongodb 学习笔记 09 -- shard分片