XOR and Favorite Number(莫队算法+分块)

时间:2023-03-10 07:06:52
XOR and Favorite Number(莫队算法+分块)
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.

题意:给你一个大小为n的序列,然后给你一个数字k,再给出m组询问

 询问给出一个区间,问这个区间里面有多少个区间的异或结果为k。
做法:我们可以先预处理出所有异或结果的前缀结果,a[n]表示前n个数的异或结果。这样做有什么好处?我们知道x^x=0,所以如果我们要求区间[i,j]上的异或结果,可以用a[j]^a[i-1]得到。因此,我们可以从左端点开始,枚举每一个点,枚举的途中用flag[]数组记录前缀和出现的次数(hash),比如前缀和2出现过一次,那flag[2]++;在查询的时候,a[i]^k就是我们要找的前缀和的大小,因为前面知道如果我们要求区间[i,j]上的异或结果,可以用a[j]^a[i-1]得到,所以我们要找之前有多少个前缀,和现在的前缀异或值为k,对应到flag数组去找a[i]^k的个数,并更新答案就行了。以上是异或处理部分。
接下来是莫队算法。莫队算法是一个暴力算法,用于解决区间问题。本质上是对询问分块。他的原理是假设我知道了[L,R]的值,我可以在o1的时间求出[L+/-1,R]和[L,R+/-1]的值,那么查询[L+/-k,R]和[L,R+/-k]的值需要k步。如果我们要查询多个区间的问题,其实就是一个曼哈顿最小生成树问题了(最少的边权和走完所有点),可以用曼哈顿最小生成树算法来处理莫队。不过其实还有更方便的算法,就是分块。我们可以根据区间左端点先进行分块,分为sqrt(n)块,块里面的元素按照右端点进行排序。这样排序后,考虑左端点,由于每个块只有sqrt(n)的大小,左端点移动最多也就sqrt(n)的复杂度,n个区间就n*sqrt(n)的复杂度。对于右端点,由于每个块的右端点是有序的,所以我们查询一个块上的值,最多是n(区间的最大值,这题是10*n),又有最多查询sqrt(n)块,复杂度也是n*sqrt(n),总复杂度n*sqrt(n)。
补充:有种叫回滚莫队的算法,可以避免减法。有时候可能会遇到减法困难,比如用并查集维护区间联通块数的时候,合并很容易,但是从并查集里去掉一个数就没那么容易了,这个时候就需要回滚莫队。回滚莫队也是先对询问排序,方法同普通莫队,对于每个询问的[l,r],分两种情况(我感觉这两种状态可以直接合并起来,不过这样要多记录右端点的最小值):1、l和r在同一块,直接暴力查询。2、l和r不在同一块,那我们枚举每一个块,一共sqrtn块,右端点起始位置为块的最右端,对于每个询问先把右端点拓展到r,记录一下此时的状态a,然后左端点拓展到l,记录答案后把状态回滚到状态a,继续下一个询问。先不考虑回滚的复杂度,每次左端点最多移动sqrtn步,有q个左端点,左端点复杂度为qsqrtn。每一块的右端点最多移动n步,共有sqrtn块,复杂度nsqrtn。回滚的话,可以记录下每次被影响的状态,然后左端点回到状态a时的位置,再拓展一次左端点回复状态。顺便讲下上面提到的并查集维护区间联通块,可以按秩合并后记录下父节点情况,暴力拆边,或者直接可持久化并查集。还有一种双并查集做法,就是左边拓展和右边拓展各自维护一个并查集,右边并查集是正常并查集,左边并查集合并(u, v)时是先去右边并查集里面寻找fu = find(u), fv = find(v),即uv的祖先节点,然后在左边并查集里合并(fu, fv),这样每次进行并查集回滚的时候,只要清空左边并查集就可以了,就是把经过的点初始化就可以。
具体代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=<<;
const int INF=0x3f3f3f3f;
ll pos[maxn];
ll flag[maxn],ans[maxn];
int a[maxn];
struct node
{
int l,r,id;
}Q[maxn];
bool cmp(node a,node b)
{
if (pos[a.l]==pos[b.l])
{
return a.r<b.r;
}
return pos[a.l]<pos[b.l];
}
int n,m,k;
int L=,R=;
ll Ans=;
void add(int x)
{
Ans+=flag[a[x]^k];
flag[a[x]]++;
}
void del(int x)
{
flag[a[x]]--;
Ans-=flag[a[x]^k];
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
int sz=sqrt(n);
for (int i=;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]^=a[i-];
pos[i]=i/sz;
}
for (int i=;i<=m;i++)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].id=i;
}
flag[]=;
sort(Q+,Q+m+,cmp);
for (int i=;i<=m;i++)
{
while (L<Q[i].l)
{
del(L-);
L++;
}
while (L>Q[i].l)
{
L--;
add(L-);
}
while (R<Q[i].r)
{
R++;
add(R);
}
while (R>Q[i].r)
{
del(R);
R--;
}
ans[Q[i].id]=Ans;
}
for (int i=;i<=m;i++)
printf("%I64d\n",ans[i]);
return ;
}

2016-09-25 03:31:38