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

Sample test(s)

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次询问,每一询问会有一个L和R,表示所询问的区间,问在这个区间中有多少个连续的子区间的亦或和为k

思路:本题只有询问没有修改,所以比较适合离线处理,而莫队算法是离线处理一类区间不修改查询类问题的算法。就是如果你知道了[L,R]的答案。你可以在O(1)的时间下得到[L,R-1]和[L,R+1]和[L-1,R]和[L+1,R]的答案的话。就可以使用莫队算法。,第一次接触莫队算法感觉是一种很优雅的暴力,莫队算法是莫涛发明的。先对序列分块。然后对于所有询问按照L所在块的大小排序。如果一样再按照R排序。然后按照排序后的顺序计算。为什么这样计算就可以降低复杂度呢。

一、i与i+1在同一块内,r单调递增,所以r是O(n)的。由于有n^0.5块,所以这一部分时间复杂度是n^1.5。

二、i与i+1跨越一块,r最多变化n,由于有n^0.5块,所以这一部分时间复杂度是n^1.5

三、i与i+1在同一块内时变化不超过n^0.5,跨越一块也不会超过2*n^0.5,不妨看作是n^0.5。由于有n个数,所以时间复杂度是n^1.5于是就变成了O(n^1.5)了。

对于这道题,假设我们现在有一个前缀异或和数组sum[],现在我们要求区间[L,R]的异或的值,用sum数组表示就是sum[L-1]^sum[R]==K,或者说是K^sum[R]==sum[L-1]

详细见代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <algorithm>
#define LL long long using namespace std; const int Max = 1100000; const int MAXM = 1<<22; typedef struct node
{
int L ,R; int Id;
}Point ; Point a[Max]; LL sum[Max]; LL ans[Max]; int n,m; LL k; int L,R; LL cnt[MAXM],ant; bool cmp(Point b,Point c)//将区间分块排序
{
if(b.L/400==c.L/400)
{
return b.R<c.R;
}
else
{
return b.L<c.L;
}
} void Dec(LL s) //将多算的数目去除
{
--cnt[s]; ant-=cnt[s^k];
} void Inc(LL s)//将没有遍历的点对应的数目加上
{
ant += cnt[s^k]; cnt[s]++;
} int main()
{
scanf("%d %d %lld",&n,&m,&k); LL data; for(int i=1;i<=n;i++) //
{
scanf("%lld",&sum[i]); sum[i]^=sum[i-1]; } for(int i=1;i<=m;i++)
{
scanf("%d %d",&a[i].L,&a[i].R);
a[i].Id = i;
a[i].L--;// 在这里提前处理
}
sort(a+1,a+m+1,cmp); L=0,R=0,cnt[0]=1,ant=0; for(int i=1;i<=m;i++)
{
while(R<a[i].R)
{
Inc(sum[++R]);
} while(R>a[i].R)
{
Dec(sum[R--]);
}
while(L<a[i].L)
{
Dec(sum[L++]);
}
while(L>a[i].L)
{
Inc(sum[--L]);
} ans[a[i].Id]=ant;
}
for(int i=1;i<=m;i++)
{
printf("%lld\n",ans[i]);
}
return 0;
}

Codeforces617 E . XOR and Favorite Number(莫队算法)的更多相关文章

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

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

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

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

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

  5. Codeforces 617E XOR and Favorite Number莫队

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

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

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

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

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

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

  9. Codeforces Round #340 (Div. 2) E XOR and Favorite Number 莫队板子

    #include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...

随机推荐

  1. Linux一些零碎

    1.设置时间和市区 1.tzselect 2.sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  2. SEO

    白帽SEO 内容优化 网站标题.关键字.描述 网站内容优化 Robot.txt文件 网站地图 增加外链引用 2. 网站结构布局优化 网站加载速度:一个页面<100k 扁平化:网站 目录层次 少, ...

  3. cloudera learning7:Hadoop资源管理

    Linux Control Groups(cgroups):在操作系统级别进行资源分配,可通过Cloudera Static Service Pools配置. YARN调度器配置:对运行在YARN上的 ...

  4. Redis数据类型介绍

    Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) st ...

  5. 正经学C#_介绍与其编写基础:《c#入门经典》

    本文所讲内容,均可在<c#入门经典>中可以查询.如有错误,敬请指出.谢谢! C#:全称C Shar.是微软.Net Framework平台下最为主要的客户语言之一.个人理解,c#是微软最为 ...

  6. Mongodb基本操作说明

    Mongodb基本操作说明 1.首先cmd(管理员方式运行)下启动mongo服务(类似初始化工具): Mongod.exe 默认文件夹为 :c:\data\db 如果没有创建该文件夹的话,需要先创建该 ...

  7. Quartz2D 编程指南(四)位图与图像遮罩、CoreGraphics 绘制 Layer

    概览 图形上下文 路径 颜色与颜色空间 变换 图案 阴影 渐变 透明层 Quartz 2D 中的数据管理 位图与图像遮罩 CoreGraphics 绘制 Layer 位图与图像遮罩 简介 位图与图像遮 ...

  8. 如何理解C#委托

    一:从下面的例子开始,理解委托变量本质 如上图,Condition是我定义的委托变量.这个委托变量的本质就是地址变量(即C语言当中的指针变量),它保存的是方法的入口地址. 当函数的调用者传递实参给这个 ...

  9. ASP.NET MVC 4 视图页去哪里儿

    这里特别感谢 swagon 提到了Displaymodeprovider,所以才有了本篇博客,也使我对[View的呈现]中寻找视图页的过程有了清晰的认识! 前戏 在MVC中,执行完Action之后,会 ...

  10. git 添加 文件到GitHub

    用pycharm写代码,暂时不太了解它的版本控制方式,使用git托管到 GitHub. 1. 在被提交的文件夹下,运行 git  init,初始化git; 2. 添加需要提交的文件, git add ...