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

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,A3An。有 m 个询问,每次询问 [Li,Ri] 之间有多少个区间 [Li,Ri] 满足 ALi xor ALi+1 xor  ARi=k

Solution

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

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

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

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

就这样,我们完成了这一道题……
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[N],B[N];
int cnta[M],cntb[M];
ll sum[N];
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[N];
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]);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值