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,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...
随机推荐
- July 28th, Week 31st Thursday, 2016
Time is a bird flying into eternity. 时间是一只永远在飞翔的鸟儿. Time waits for nobody. Vitality shows in not onl ...
- 【转载】C++ 值传递、指针传递、引用传递详解
原文链接:http://www.cnblogs.com/yanlingyin/ 值传递: 形参是实参的拷贝,改变形参的值并不会影响外部实参的值.从被调用函数的角度来说,值传递是单向的(实参->形 ...
- MySQL主备库切换(MHA)演练与总结
演练包括被动切换和主动切换两部分.被动切换是主库宕机,主动切换是人工手动触发. 演练步骤大致如下: 1 先停掉主库,模拟主库宕机 2 mha将vip切到备库,备库变成主库, ...
- 安装及升级node
一.mac下安装 1. 可直接在官网下载(http://nodejs.cn/),可使用命令查看版本: node -v node --version 同样npm同时也安装下来,可使用下面命令查看: np ...
- hp unix_ssh
http://www.cyberciti.biz/faq/howto-hpux-sshd-service-startup-shutdown/ http://searchnetworking.techt ...
- Android之shape属性详解
有时候 ,为了满足一些需求,我们要用到 shape 去定义 一些背景,shape 的用法 跟图片一样 ,可以给View设置 Android:background="@drawable/sha ...
- Mysql or Mongodb LBS快速实现方案
http://www.wubiao.info/470 前两篇文章: 查找附近的xxx 球面距离以及Geohash方案探讨 (http://www.wubiao.info/372) 微信.陌陌 架构方案 ...
- .net学习之多线程、线程死锁、线程通信 生产者消费者模式、委托的简单使用、GDI(图形设计接口)常用的方法
1.多线程简单使用(1)进程是不执行代码的,执行代码的是线程,一个进程默认有一个线程(2)线程默认情况下都是前台线程,要所有的前台线程退出以后程序才会退出,进程里默认的线程我们叫做主线程或者叫做UI线 ...
- ytu 1938:首字母变大写(水题)
首字母变大写 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 110 Solved: 43[Submit][Status][Web Board] Desc ...
- [转]C# Winform ListView使用
以下内容均来自网上,个人收集整理,具体出处也难确认了,就没写出处了: 一.基本使用: listView.View = View.Details;//设置视图 listView.SmallImageLi ...