您的位置:首页 > 其它

【CodeForces-617E】XOR and Favorite Number 莫队(好玩题)

2017-12-23 18:46 561 查看

Description

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.

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.

Translation

题目大意:给出 n,m,k 和一个长度为 n 的序列 A1,A2,A3…An。有 m 个询问,每次询问 [Li,Ri] 之间有多少个区间 [L′i,R′i] 满足 AL′i xor AL′i+1 xor … AR′i=k

Solution

这道题其实不难,可以说是一道莫队的模板题而且还十分好玩。

由于原题目要求满足的式子太过冗长和难以计算,我们把它变一个形。我们像前缀和一样,处理一个前缀 xor 数组 A′ ,这样,Ai xor Ai+1 xor … Aj 就可以等效替代为 A′j xor A′i−1,可以O(1)计算了!那么我们看一下要满足什么条件才可以计数。

设 [L,R] 满足条件,也就是说 A′R xor A′L−1=k,两边同时 xor k,也就是 A′R xor A′L−1 xor k=0 ,我们保存一个数组 Bi=A′i xor k,原式可以替代为 A′R xor BL−1=0 ,等价于 A′R=BL−1,我们将 B 数组平移一下变成 B′ 使得 B′i=Bi−1,那么 A′R=B′L 是 AL xor AL+1 xor … AR=k 的充要条件

所以现在我们的任务就变成了查询[Li,Ri]中,有多少组 (i,j) 使得 A′i=B′j 且 j≤i ,很凑巧,莫队刚好可以解决这个问题!我们移动l1和r1时,同时维护一下每个数在A′和B′中出现的情况(cnta和cntb),对于一个向左扩展的l1,我们加上cnta[B′l1],对于一个向右扩展的r1,我们加上cntb[A′r1],向内收缩则减去即可。

就这样,我们完成了这一道题……

PS: 我的代码里面没有平移B,所以使用的时候下标都减了1

Code:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100005;
const int M=3000005;
int n,m,unit,k,A
,B
;
int cnta[M],cntb[M];
ll sum
;
struct MoTeam
{
int l,r,id;
bool operator<(MoTeam b)const
{
return l/unit==b.l/unit?r/unit<b.r/unit:l/unit<b.l/unit;
}
}S
;
int main()
{
scanf("%d%d%d",&n,&m,&k);
B[0]=k;
for(int i=1;i<=n;i++)
{
scanf("%d",&A[i]);
A[i]^=A[i-1];
B[i]=A[i]^k;
}
unit=sqrt(n);
for(int i=1;i<=m;i++)scanf("%d%d",&S[i].l,&S[i].r),S[i].id=i;
sort(S+1,S+m+1);
int l1=1,r1=1;
ll ans=0;
cntb[B[0]]++;cnta[A[1]]++;
if(A[1]==k)ans++;
for(int i=1;i<=m;i++)
{
int l=S[i].l,r=S[i].r;
while(l1>l)
l1--,cntb[B[l1-1]]++,cnta[A[l1]]++,ans+=cnta[B[l1-1]];
while(r1<r)
r1++,cntb[B[r1-1]]++,cnta[A[r1]]++,ans+=cntb[A[r1]];
while(l1<l)
ans-=cnta[B[l1-1]],cnta[A[l1]]--,cntb[B[l1-1]]--,l1++;
while(r1>r)
ans-=cntb[A[r1]],cnta[A[r1]]--,cntb[B[r1-1]]--,r1--;
sum[S[i].id]=ans;
}
for(int i=1;i<=m;i++)printf("%I64d\n",sum[i]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: