莫队算法例题codeforces617E

                                                       XOR and Favorite Number

time limit                      per test                                     4 seconds

memory limit                 per test                               256 megabytes

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 nm 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

Copy

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

题意:求区间l——r的异或和为k的对数;

由于不涉及修改操作,可以考虑使用莫队算法;那么已知[L,R]怎么O(1)的得到[L`,R`]?

这里用前缀处理一下;A[i]表示前i个数的异或和,[L,R]的异或和就是A[R]^A[L-1];

公式:X^Y=K   =====    Y^K=X;只需要知道这区间有几个A[L-1]就知道了异或和为K的对数,

这样知道[L,R]的异或和为K的对数,对于R+1,就是NOW+num[A[R+1]^K],当前对数加上,a[r+1]异或k得到的数在这个区间的个数

 

#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN=100005;
const int MAXM=100005;
int sz;//块的大小
int n,m,k;
struct query
{
    int l,r,id;
}q[MAXM];
long long num[1<<20];//每个前缀和(异或和)出现的次数
long long res[MAXN];
int a[MAXN];
bool cmp(query a,query b)
{
    if(a.l/sz==b.l/sz)return a.rq[i].l)
        {
            l--;
            add(l-1);
        }
        while(r>q[i].r)
        {
            del(r);
            r--;
        }
        while(l

 

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