Codeforces 617E XOR and Favorite Number (區間異或和 莫隊演算法 分塊暴力)

_TCgogogo_發表於2016-02-25
E. 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 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.

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: (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.

題目連結:http://codeforces.com/problemset/problem/617/E

題目大意:求給定區間的子區間異或和等於k的子區間個數

題目分析:對於l到r的異或和根據異或的性質可以通過sum[l - 1] ^ sum[r]得到,sum是字首異或和,預處理完字首先把左區間減1方便後面的運算,題目的運算滿足在知道[l,r]的條件下可以O(1)得到[l-1, r],[l+1, r],[l, r-1],[l, r+1],因此可以用莫隊演算法,一種分塊優化的思想,先記錄要查詢的區間然後排序,這樣每次只需要控制兩端端點的移動即可更新答案,因為已經排過序,所以端點移動的次數比無序時大大減少,時間複雜為O(n * sqrt(n)),不做證明,再回到這題,根據異或性質sum[l - 1] ^ sum[r] == k ==> sum[r] ^ k = sum[l - 1],我們可以用一個cnt動態記錄sum[l - 1]出現的次數即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;
int const MAXN = 100005;
int const MAXM = 10000005;
int const CON = sqrt(MAXN);
ll sum[MAXN], a[MAXN], ans[MAXN], num;
ll cnt[MAXM];
int n, m, k;

struct DATA
{
    int l, r, id;
}d[MAXN];

bool cmp(DATA a, DATA b)
{
    if(a.l / CON == b.l / CON)
        return a.r / CON < b.r / CON;
    return a.l / CON < b.l / CON;
}

void Add(int x) //加的時候先看其之前出現了多少個sum[l - 1],再更新當前點
{
    num += cnt[x ^ k]; 
    cnt[x] ++;
}

void Sub(int x) //減的時候先去掉該點再看其之前sum[l - 1]出現的次數,和加時對應
{
    cnt[x] --;
    num -= cnt[x ^ k];
}

int main()
{
    scanf("%d %d %d", &n, &m, &k);
    for(int i = 1; i <= n; i++)
    {
        scanf("%I64d", &a[i]);
        sum[i] = sum[i - 1] ^ a[i];
    }
    for(int i = 0; i < m; i++)
    {
        scanf("%d %d", &d[i].l, &d[i].r);
        d[i].id = i;
        d[i].l -= 1;
    }
    sort(d, d + m, cmp);
    int l = 0, r = 0;
    num = 0;
    cnt[0] = 1;
    for(int i = 0; i < m; i++)
    {
        while(l < d[i].l)
            Sub(sum[l ++]); 
        while(l > d[i].l)
            Add(sum[-- l]); //考慮到邊界,左端點始終更新前一個
        while(r < d[i].r)
            Add(sum[++ r]); //右端點始終更新後一個
        while(r > d[i].r)
            Sub(sum[r --]);
        ans[d[i].id] = num;
    }
    for(int i = 0; i < m; i++)
        printf("%I64d\n", ans[i]);
}


相關文章