【第400篇题解纪念2016年10月28日】【28.10%】【codeforces 617E】XOR and Favorite Number
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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个询问li,ri;
要让你在[li,ri]这个区间里面找到下标对i,j;
使得a[i]xor a[i+1] xor a[i+2]..xor a[j] == k;
让你输出在li,ri内这样的i,j对的个数;
n=10W;
m=100W;
每个数字ai最大为100W为非负数;
设sum[i]表示前i个数字的异或值;
离线处理询问;左端升序排;左端相同右端升序排;
然后从第一个询问开始处理;
设区间为L..R;
则i从L->R递增flag[sum[i]]
然后遇到一个sum[i]则递增答案flag[k^sum[i]];
假设k^sum[i] = sum[x] (x小于i);
则有sum[x]^sum[i] = k;
而sum[i]^sum[x]实际上就是a[x+1]^a[x+2]..^a[i];
所以这个方式是可行的;
这样我们就能找出所有的点对了;
然后因为询问经过排序处理;
所以相邻询问的l和r和我们刚处理过的L,R是很接近的;
如果l
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long
using namespace std;
const int MAXN = 209999;
const int MAX_SIZE = 1009999;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
struct abc
{
int l,r,id;
};
int n,m,k,sum[MAXN];
LL ans[MAXN];
int flag[MAX_SIZE*2] = {0};
abc Q[MAXN];
void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
bool cmp(abc a,abc b)
{
if (a.l/400!=b.l/400)
return a.l/400<b.l/400;
else
return a.r < b.r;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_int(n);input_int(m);input_int(k);
for (int i = 1;i <= n;i++)
{
int x;
input_int(x);
sum[i] = sum[i-1] ^ x;
}
for (int i = 1;i <= m;i++)
input_int(Q[i].l),input_int(Q[i].r),Q[i].l--,Q[i].id = i;
sort(Q+1,Q+1+m,cmp);
int L,R;
LL s = 0;
L = Q[1].l,R=Q[1].r;
for (int i = L;i <= R;i++)
{
s+= flag[k^sum[i]];
flag[sum[i]]++;
}
ans[Q[1].id] = s;
for (int i = 2;i <= m;i++)
{
int l = Q[i].l,r=Q[i].r;
while (L>l)
{
L--;
s+=flag[k^sum[L]];
flag[sum[L]]++;
}
while (L<l)
{
flag[sum[L]]--;
s-=flag[k^sum[L]];
L++;
}
while (R>r)
{
flag[sum[R]]--;
s-=flag[k^sum[R]];
R--;
}
while (R<r)
{
R++;
s+=flag[k^sum[R]];
flag[sum[R]]++;
}
ans[Q[i].id] = s;
}
for (int i = 1;i <= m;i++)
printf("%I64d\n",ans[i]);
return 0;
}
【第400篇题解纪念2016年10月28日】【28.10%】【codeforces 617E】XOR and Favorite Number的更多相关文章
- 2016年12月15日 星期四 --出埃及记 Exodus 21:10
2016年12月15日 星期四 --出埃及记 Exodus 21:10 If he marries another woman, he must not deprive the first one o ...
- 2016年11月19日 星期六 --出埃及记 Exodus 20:10
2016年11月19日 星期六 --出埃及记 Exodus 20:10 but the seventh day is a Sabbath to the LORD your God. On it you ...
- 2016年10月31日 星期一 --出埃及记 Exodus 19:16
2016年10月31日 星期一 --出埃及记 Exodus 19:16 On the morning of the third day there was thunder and lightning, ...
- 2016年10月30日 星期日 --出埃及记 Exodus 19:15
2016年10月30日 星期日 --出埃及记 Exodus 19:15 Then he said to the people, "Prepare yourselves for the thi ...
- 2016年10月29日 星期六 --出埃及记 Exodus 19:14
2016年10月29日 星期六 --出埃及记 Exodus 19:14 After Moses had gone down the mountain to the people, he consecr ...
- 2016年10月27日 星期四 --出埃及记 Exodus 19:12
2016年10月27日 星期四 --出埃及记 Exodus 19:12 Put limits for the people around the mountain and tell them, `Be ...
- 2016年10月26日 星期三 --出埃及记 Exodus 19:10-11
2016年10月26日 星期三 --出埃及记 Exodus 19:10-11 And the LORD said to Moses, "Go to the people and consec ...
- 2016年10月25日 星期二 --出埃及记 Exodus 19:9
2016年10月25日 星期二 --出埃及记 Exodus 19:9 The LORD said to Moses, "I am going to come to you in a dens ...
- 2016年10月24日 星期一 --出埃及记 Exodus 19:8
2016年10月24日 星期一 --出埃及记 Exodus 19:8 The people all responded together, "We will do everything th ...
随机推荐
- HDU 6217 BBP Formula (数学)
题目链接: HDU 7217 题意: 题目给你可以计算 \(π\) 的公式: \(\pi = \sum_{k=0}^{\infty}[\frac{1}{16^k}(\frac{4}{8k+1})-(\ ...
- 硬件——nrf51822第一篇,GPIO的使用
未完,待续...... 本实现是基于一个开发箱,包括:综合应用开发系统主板XT-EDU-AK 1套: 手持终端系统 XT-EDU-HK 1套: GPIO操作 工程: 这是一个关于流水灯的程序: 我 ...
- sea.js五分钟上手
SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架.本文给大家分享sea.js知识总结,感兴趣的朋友一起学习吧http://reactjs.cn/http://reactjs. ...
- (转)利用openfiler实现iSCSI
转自:http://czmmiao.iteye.com/blog/1735417 openfiler openfiler是一个基于浏览器的网络存储管理工具.来自于Linux系统.openfiler在一 ...
- bow lsa plsa
Bag-of-Words (BoW) 模型是NLP和IR领域中的一个基本假设.在这个模型中,一个文档(document)被表示为一组单词(word/term)的无序组合,而忽略了语法或者词序的部分.B ...
- IOS越狱开发环境搭建
1:安装 mac ports 2:安装DPKG, 在终端输入sudo port -f install dpkg即可安装 3:安装theos Theos是一个基于Make的编译环境,我们正是用它来编译生 ...
- swift项目第七天:构建访客界面以及监听按钮点击
一:访客界面效果如图 二:xib封装访客视图的view 1:业务逻辑分析:1:由于用户未登录时要显示访客视图,要先进行判断用户是否登录,未登录则显示访客视图,登录则显示正常的登陆界面,由于要在四个子控 ...
- POJ 1065 Wooden Sticks(zoj 1025) 最长单调子序列
POJ :http://poj.org/problem?id=1065 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId= ...
- 11、DMA操作说明
先理解cache的作用CPU在访问内存时,首先判断所要访问的内容是否在Cache中,如果在,就称为“命中(hit)”,此时CPU直接从Cache中调用该内容:否则,就 称为“ 不命中”,CPU只好去内 ...
- 无意中发现Markdown,最终解放了我
文件夹 概述 换行 删除线 链接自己主动识别 表格 代码块高亮 定义列表 脚注 自己主动生成文件夹 參考资料 正文 概述 大部分情况下,Markdown的基本的语法已够我们使用,比方随性记录点东西.非 ...