Codeforeces 617E 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.
题目链接:CF 617E
这题感觉比一般的莫队要难很多,主要问题就出在转化上,一开始看感觉没办法O(1)进行拓展,后来看了大牛的思路原来是用了一个前缀异或和的数组pre[ ]和一个维护当前区间内异或和个数的数组cnt[ ],用pre[i-1]^pre[j]来表示ai^……aj的异或和,这样才能写出add和del函数。
假设当前已知【L,R】,如果区间增加,假设增加的那个位置的值为pre[x],那么显然pre[x]可以和区间内所有值为 k^pre[x]的数进行两两组合,即会多出cnt[ k^pre[x] ]个组合,此时cnt就可以用到,直接加上cnt[k^pre[x]]即可,然后再更新pre[x](顺序不能反,万一pre[x]^k和pre[x]是相同的,更新前是4,更新后是5,但是只能多出4个组合而不是5个组合);如果区间减小,由于被减掉的那个pre[x]本来可以组合cnt[k^pre[x]]个,因此要减掉这么多个数,但是假如pre[x]=pre[x]^k的话,比如5个0去了一个0变成了4个0,组合数减少了4,因此要先自减1再更新此时的答案
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1<<20; struct info
{
int l,r;
int b,idd;
bool operator<(const info &t)const
{
if(b==t.b)
return r<t.r;
return b<t.b;
}
}; info query[N];
int pre[N];
LL cnt[N];
LL ans[N];
void init()
{
CLR(pre,0);
CLR(cnt,0);
CLR(ans,0);
}
inline void add(const int &val,LL &temp,const int &k)
{
temp+=cnt[val^k];
++cnt[val];
}
inline void del(const int &val,LL &temp,const int &k)
{
--cnt[val];
temp-=cnt[val^k];;
}
int main(void)
{
int n,m,k,i,j,val;
while (~scanf("%d%d%d",&n,&m,&k))
{
init();
for (i=1; i<=n; ++i)
{
scanf("%d",&val);
pre[i]=pre[i-1]^val;
}
int unit=sqrt(n);
for (i=0; i<m; ++i)
{
scanf("%d%d",&query[i].l,&query[i].r);
query[i].b=query[i].l/unit;
query[i].idd=i;
}
sort(query,query+m);
int L=1,R=0;
LL temp=0;
cnt[0]=1;
for (i=0; i<m; ++i)
{
while (L>query[i].l)
{
--L;
add(pre[L-1],temp,k);
}
while (L<query[i].l)
{
del(pre[L-1],temp,k);
++L;
}
while (R>query[i].r)
{
del(pre[R],temp,k);
--R;
}
while (R<query[i].r)
{
++R;
add(pre[R],temp,k);
}
ans[query[i].idd]=temp;
}
for (i=0; i<m; ++i)
printf("%I64d\n",ans[i]);
}
return 0;
}
Codeforeces 617E XOR and Favorite Number(莫队+小技巧)的更多相关文章
- 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 ...
- 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 ...
- 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 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 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 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 ...
- E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)
一直都说学莫队,直到现在才学,训练的时候就跪了 T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...
随机推荐
- codeforces A. Dima and Continuous Line 解题报告
题目链接:http://codeforces.com/problemset/problem/358/A 题目意思:在横坐标上给出n个不同的点,需要把前一个点跟后一个点(这两个点的顺序是紧挨着的)用一个 ...
- 教官的游戏(codevs 2793)
题目描述 Description 有N个学生刚吃完饭,准备出食堂. 国防学校有个规矩:必须2人一排或3人一列离开. 两个教官A,B轮流取2或3人,谁先取完谁就赢得游戏.(A先取) 若两人都用最优策略, ...
- 希尔排序( Shell Sort)
原文地址:http://www.stoimen.com/blog/,在此感谢作者! Insertion sort is a great algorithm, because it’s very int ...
- OpenStack Swift集群与Keystone的整合使用说明
之前已经介绍了OpenStack Swift集群和Keystone的安装部署,最后来讲一讲Swift集群与Keystone的整合使用吧. 1. 简介 本文档描述了Keystone与Swift集群的整合 ...
- sybase常用SQL语句,工作中积累的
-------创建sybase设备 语句--------- disk init name="DEV_DB_CLIENT_DAT26", physname="F:\syba ...
- GRE学习日志
发现开博客园真的很有督促作用,今天也顺便开个GRE学习日志吧 2015-02-09:单词 2015-02-10:单词 2015-02-11:单词 2015-03-02:阅读 2015-03-04:阅读 ...
- Codeforces Gym 100187K K. Perpetuum Mobile 构造
K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- Intent的七大属性
1.Action Action属性代表系统要执行的动作 系统提供如下常用的Action属性 *ACTION_MAIN:应用程序入口点 *ACTION_VIEW:显示指定数据 *ACTION_EDIT: ...
- JavaScript案例三:动态显示时间
用JavaScript实现在页面上动态的显示时间 <!DOCTYPE html> <html> <head> <title>JavaScript动态显示 ...
- [hive小技巧]使用limit查询变成抽样,而不是全盘扫描
将set hive.limit.optimize.enable=true 时,limit限制数据时就不会全盘扫,而是根据限制的数量进行抽样. 同时还有两个配置项需要注意: 1.hive.limit.r ...