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.
题目:http://codeforces.com/problemset/problem/617/E
题意:求i-j区间内异或值等于k的个数。
输入过程中异或a[i]^=a[i-1];则有^[i...j] = a[i-1]^a[j]=k , 则a[j]^k=a[i-1];莫队处理区间个数。
卿学姐莫队视频讲解:http://www.bilibili.com/video/av4291097/
莫队参考:http://blog.youkuaiyun.com/bossup/article/details/39236275
要知道我们算完[L,R]的答案后现在要算[L',R']的答案。由于可以在O(1)的时间下得到[L,R-1]和[L,R+1]和[L-1,R]和[L+1,R]的答案.所以计算[L',R']的答案花的时间为|L-L'|+|R-R'|。如果把询问[L,R]看做平面上的点a(L,R).询问[L',R']看做点b(L',R')的话。那么时间开销就为两点的曼哈顿距离。所以对于每个询问看做一个点。我们要按一定顺序计算每个值。那开销就为曼哈顿距离的和。要计算到每个点。那么路径至少是一棵树。所以问题就变成了求二维平面的最小曼哈顿距离生成树。
关于二维平面最小曼哈顿距离生成树。感兴趣的可以参考点击打开链接
这样只要顺着树边计算一次就ok了。可以证明时间复杂度为n*sqrt(n)这个我不会证明。
但是这种方法编程复杂度稍微高了一点。所以有一个比较优雅的替代品。那就是先对序列分块。然后对于所有询问按照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)了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include <iomanip>
using namespace std;
#define ll long long
const int N = 1e6+1e6+10;
int a[N];
int belong[N];
struct node
{
int a,b;
int id;
}q[N];
bool cmp(node x,node y)
{
if(belong[x.a]==belong[y.a])
return x.b<y.b;
return belong[x.a]<belong[y.a];
}
long long ans[N];
long long sum[N];
int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
int sz=sqrt(n);
a[0]=0;
for(int i=1; i<=n; ++i)
{
scanf("%d",&a[i]);
a[i]^=a[i-1];
belong[i]=i/sz;
}
for(int i=0; i<m; ++i)
{
scanf("%d%d",&q[i].a,&q[i].b);
q[i].id=i;
}
sort(q,q+m,cmp);
memset(sum,0,sizeof(sum));
int A=1,B=0;
long long ANS=0;
sum[0]=1;
for(int i=0; i<m; ++i)
{
int l=q[i].a;
int r=q[i].b;
while(B<r)
{
++B;
ANS+=sum[ a[B]^k ];
sum[a[B]]++;
}
while(B>r)
{
sum[a[B]]--;
ANS-=sum[a[B]^k];
--B;
}
while(A<l)
{
sum[ a[A-1] ]--;
ANS-=sum[a[A-1]^k];
++A;
}
while(A>l)
{
--A;
ANS+=sum[a[A-1]^k];
sum[a[A-1]]++;
}
ans[q[i].id]=ANS;
}
for(int i=0; i<m; ++i)
cout<<ans[i]<<endl;
return 0;
}
/*
6 2 1
1 2 1 1 0 3
1 1
3 4
1
2
*/