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的序列,然后给你一个数字k,再给出m组询问
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=<<;
const int INF=0x3f3f3f3f;
ll pos[maxn];
ll flag[maxn],ans[maxn];
int a[maxn];
struct node
{
int l,r,id;
}Q[maxn];
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];
}
int n,m,k;
int L=,R=;
ll Ans=;
void add(int x)
{
Ans+=flag[a[x]^k];
flag[a[x]]++;
}
void del(int x)
{
flag[a[x]]--;
Ans-=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-];
pos[i]=i/sz;
}
for (int i=;i<=m;i++)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].id=i;
}
flag[]=;
sort(Q+,Q+m+,cmp);
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]=Ans;
}
for (int i=;i<=m;i++)
printf("%I64d\n",ans[i]);
return ;
}
2016-09-25 03:31:38
XOR and Favorite Number(莫队算法+分块)的更多相关文章
- 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 E. XOR and Favorite Number(莫队算法)
题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...
- 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 - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- 【bzoj3585/bzoj3339】mex/Rmq Problem 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805283.html 题目描述 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没 ...
- 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...
- 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
随机推荐
- es6笔记1^_^let、string、number、math
ECMAScript是一种由Ecma国际(前身为欧洲计算机制造商协会,英文名称是European Computer Manufacturers Association)通过ECMA-262标准化的脚本 ...
- CSS属性之absolute
0.脱离标准文档流 绝对定位的元素会脱离标准文档流,拥有z-index属性,并且对于它的任何操作和改变都不会影响它的兄弟元素和父级元素,这里就不过多介绍. 不过值得注意的是,虽然绝对定位元素脱离的标准 ...
- 【.NET】字符串处理类库
类名:DealString,方法清单列好在头上. /// 1.截取字符串,最后加3个小数点 /// 2.获得指定Url的参数的string类型值 /// 3.判断数据类型 /// 4.过滤JS标记 / ...
- Kattis - Fenwick Tree(树状数组区间更新单点求值)
Fenwick Tree Input The first line of input contains two integers NN, QQ, where 1≤N≤50000001≤N≤500000 ...
- 转载一篇nm命令使用的文章,虽然没用用这个方法解决但是文章很好
http://blog.csdn.net/acs713/article/details/13505931
- GourdScan & sqlmapapi
0x01 Windows下配置GourdScan 0x0101 GourdScan项目地址:https://github.com/code-scan/GourdScan PHP环境 + ...
- Top 20 JavaScript Projects of 2017
https://www.youtube.com/watch?v=SUMn8y3pi28 20. AngularJS 1 19. Passport 18. Pug 17. Socket.IO 16. J ...
- Linux下常用的压缩与解压命令
.tar (注:tar是打包,不是压缩!) 解包: tar xvf FileName.tar 打包: tar cvf FileName.tar DirName .gz 解压1: gunzip File ...
- 更改系统相机UIImagePickerController导航栏的cancle为自定义按钮
有时候需要对系统相册里面的取消按钮进行自定义,并获取点击事件做一些操作,那么你可以这样做. 第一:实现navigationController代理 - (void)navigationControll ...
- Web中的无状态含义
REST架构设计是目前非常火热的概念,已经成为构建web服务时应该遵循的事实标准.REST约束中有一条很重要的规则是“无状态”,但“无状态”是个很抽象的概念,对刚刚接触的人来讲,很难深刻形象的理解.今 ...