1. 程式人生 > >Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫隊算法 + 異或和前綴和的巧妙】

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫隊算法 + 異或和前綴和的巧妙】

lag ans integer ons lap stand 推出 open closed

任意門:http://codeforces.com/problemset/problem/617/E

E. XOR and Favorite Number

time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output

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 l

i 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.

題目大意:

有一串長度為 N 的數列,M次查詢 ( l,r )內有多少對 ( i, j )使得 ai ^ ai+1 ^ ... ^ aj = K;

解題思路:

這裏的區間查詢是離線的,可以用傳說中的莫隊算法(優雅而華麗的暴力算法)。

異或的題目有一個很巧妙的地方就是利用 a^b^a = b 這個性質。

這道題目我們也要預處理一下 sum(i) 前 i 個數的異或和, 那麽 ai ^ ai+1 ^ ... ^ aj = sum( i - 1) ^ sum( j ) 了。

接下來我們就可以按照查詢的區間用莫隊算法遍歷一遍,同時記錄當前區間某個前綴和的出現次數;

根據 sum( i - 1) ^ sum( j ) = K ,K ^ sum( i - 1 ) = sum( j ) || K ^ sum( j ) = sum( i - 1) ,由符合條件的前綴和次數來推出符合條件的( i,j )的個數啦。

Tip:

聽了大神的課,這道題有兩個坑:

1、答案的數據的數據範圍是爆 int 的;

2、雖然是1e6 的 K,但異或的結果要大於 1e6 ;

AC code:

技術分享圖片
 1 #include <bits/stdc++.h>
 2 #define LL long long int
 3 using namespace std;
 4 const int MAXN = 1<<20;
 5 
 6 struct node
 7 {
 8     int l, r, id;  //區間和查詢編號
 9 }Q[MAXN];       //記錄查詢數據(離線)
10 
11 int pos[MAXN];  //記錄分塊
12 LL ans[MAXN];   //記錄答案
13 LL flag[MAXN];  //維護前綴異或和出現的次數
14 int a[MAXN];    //原本數據
15 int L=1, R;     //當前區間的左右結點
16 LL res;         //儲存當前區間的值
17 int N, M, K;
18 bool cmp(node a, node b)     //排序
19 {
20     if(pos[a.l]==pos[b.l]) return a.r < b.r;  //只有左結點在同一分塊才排右結點
21     return pos[a.l] < pos[b.l];               //否則按照左結點分塊排
22 }
23 void add(int x)
24 {
25     res+=flag[a[x]^K];
26     flag[a[x]]++;
27 }
28 void del(int x)
29 {
30     flag[a[x]]--;
31     res-=flag[a[x]^K];
32 }
33 int main()
34 {
35     scanf("%d%d%d", &N, &M, &K);
36     int sz = sqrt(N);
37     for(int i = 1;  i <= N; i++){  //讀入數據
38         scanf("%d", &a[i]);
39         a[i] = a[i]^a[i-1];    //計算前綴異或和
40         pos[i] = i/sz;         //分塊
41     }
42     for(int i = 1; i <= M; i++){    //讀入查詢
43         scanf("%d%d", &Q[i].l, &Q[i].r);
44         Q[i].id = i;
45     }
46     sort(Q+1, Q+1+M, cmp);
47     flag[0] = 1;
48     for(int i = 1;  i <= M; i++){
49         while(L < Q[i].l){    //當前左結點比查詢結點小
50             del(L-1);
51             L++;
52         }
53         while(L > Q[i].l){    //當前左結點比查詢左結點大
54             L--;
55             add(L-1);
56         }
57         while(R < Q[i].r){    //當前右結點比查詢右結點小
58             R++;
59             add(R);
60         }
61         while(R > Q[i].r){    //當前右節點比查詢右節點大
62             del(R);
63             R--;
64         }
65         ans[Q[i].id] = res;
66     }
67     for(int i = 1; i <= M; i++){
68         printf("%lld\n", ans[i]);
69     }
70 }
View Code

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫隊算法 + 異或和前綴和的巧妙】