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 nm 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.

题目链接: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(莫队+小技巧)的更多相关文章

  1. CodeForces - 617E XOR and Favorite Number 莫队算法

    https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...

  2. Codeforces 617E XOR and Favorite Number莫队

    http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...

  3. codeforces 617E. XOR and Favorite Number 莫队

    题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...

  4. 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 ...

  5. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)

    一直都说学莫队,直到现在才学,训练的时候就跪了   T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...

随机推荐

  1. mysql生成varchar类型主键排序

    用uuid生成20位的主键 SELECT LEFT(REPLACE(UUID(), '-', ''),20) FROM DUAL 另一种方法: 因为数据库中有字母 需要排序的时候去除字母,重新取最大值 ...

  2. JS控制DIV隐藏显示

    转载自:http://blog.sina.com.cn/s/blog_6c3a67be0100ldbe.html JS控制DIV隐藏显示 一,需求描述: 现在有3个DIV块,3个超链接,需要点击一个链 ...

  3. org.apache.catalina.session.StandardManager doLoad

    转载自:http://www.cnblogs.com/java727/p/3300613.html SEVERE: IOException while loading persisted sessio ...

  4. php 用封装类的方法操作数据库和批量删除

    封装类 <?php class DBDA { public $host="localhost"; //服务器地址 public $uid="root"; ...

  5. 【转】什麼是 Team Explorer Everywhere 2010 ?TFS 專用的 Eclipse 整合套件的安裝與設定

    前言- 大家都知道 版本管控是一件很重要的事情!而且也知道分別有 VSS , SVN , TFS 等- 多數人都會覺得, .NET 的開發工具要用 VSS . TFS .SVN 而 Java 的 Ec ...

  6. 解决页面插入HTML代码后错位(HTML代码里的标签不完整导致错位)

    这个的例子是从数据库读取出来的数据内容包含HTML导致页面错位问题! 解决办法如下: 首先过滤掉会跟JS冲突的字符,C#代码如下: string htmlc = Model.HtmlContents. ...

  7. Man简单介绍

    转自:http://os.51cto.com/art/201312/425525.htm Linux系统提供了相对比较丰富的帮助手册(man),man是manual的缩写,在日常linux系统管理中经 ...

  8. Java Hour 59 JVM Heap

    程序没有方法区释放一段Heap 上的内存,只有JVM 本身可以去回收内存,这个工作单位就是GC. Garbage Collection GC 用来清理对象,同时也用来移动对象减少内存碎片. JVM 指 ...

  9. Java Hour 19 List

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为19 Hour,请各位不吝赐教. List Arr ...

  10. java程序员必须会的技能

    1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...