题目链接:CodeForces - 617E
题目描述:
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, ..., ajis 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.
Example
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
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]之间有多少对i和j满足a[i]^a[i+1]^...^a[j]=k;
思路:
莫队,对于区间[a,b],区间[a,b+1]的ans等于[a,b]的ans加上区间[a,b]内OXR[b+1]^k的个数
对于[a,b]的亦或和,即为XOR[b]^XOR[a-1]
XOR[b]^XOR[a-1] == k <==> XOR[b]^k == XOR[a-1]
因此寻找有多少个XOR[a-1]满足XOR[b]^XOR[a-1] == k ,即寻找有多少个XOR[b]^k
使用一个cnt数组记录当前状态下不同区间亦或和的值出现的次数。
这边讲一下亦或的小知识:
(1)x^y=k <==> x^k=y <==> y^k=x。
(2)sumxor[r]^sum[l-1]=sum[l-r];
则插入一个数时,答案加上该数异或k的cnt值,该数的cnt值加一,删除一个数时,先该数的cnt值减一,再答案减去该数异或k的cnt值
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#define ri(n) scanf("%d",&n)
#define oi(n) printf("%d\n",n)
#define rl(n) scanf("%lld",&n)
#define ol(n) printf("%lld\n",n)
#define rep(i,l,r) for(i=l;i<=r;i++)
#define rep1(i,l,r) for(i=l;i<r;i++)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int epg=10-8;
const int mod=1e9+7;
const int maxn=1e6+10;
ll ans[maxn];
int a[maxn];
int b[maxn];
int n,m,k;
int num[2*maxn];
struct node
{
int l,r,id;
bool operator<(const node &x)const
{
return b[l]<b[x.l]||(b[l]==b[x.l]&&r<x.r);
}
}p[maxn];
void solve()
{
int le=1,ri=0;
ll temp=0;
memset(ans,0,sizeof(ans));
memset(num,0,sizeof(num));
//cout<<1<<endl;
for(int i=1;i<=m;i++)
{
while(le<p[i].l)
{
num[a[le]]--;
temp-=num[a[le]^k];
le++;
}
while(le>p[i].l-1)
{
le--;
temp+=num[a[le]^k];
num[a[le]]++;
}
while(ri<p[i].r)
{
ri++;
temp+=num[a[ri]^k];
num[a[ri]]++;
}
while(ri>p[i].r)
{
num[a[ri]]--;
temp-=num[a[ri]^k];
ri--;
}
ans[p[i].id]=temp;
//cout<<"1"<<endl;
}
for(int i=1;i<=m;i++)
printf("%lld\n",ans[i]);
}
int main()
{
//int n,m,k;
while(scanf("%d%d%d",&n,&m,&k)==3)
{
a[0]=0;
int len=sqrt(n);
for(int i=1;i<=n;i++)
b[i]=(i-1)/len+1;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]^=a[i-1];
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&p[i].l,&p[i].r);
p[i].id=i;
}
sort(p+1,p+1+m);
//for(int i=1;i<=m;i++)
//printf("%d %d %d\n",p[i].l,p[i].r,p[i].id);
solve();
}
return 0;
}