莫队

题目链接: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 nm and k (1 ≤ n, m ≤ 100 0000 ≤ 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
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: (12), (14), (15), (23), (36), (56), (66). 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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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;ip[i].l-1)
         {
             le--;
             temp+=num[a[le]^k];
             num[a[le]]++;
         }
         while(rip[i].r)
         {
             num[a[ri]]--;
             temp-=num[a[ri]^k];
             ri--;
         }
         ans[p[i].id]=temp;
         //cout<<"1"<


你可能感兴趣的:(莫队算法)