Codeforces Round #340 (Div. 2) E. XOR and favorite number "Mo Team algorithm + xor and prefix and ingenious"

Source: Internet
Author: User

Any door: Http://codeforces.com/problemset/problem/617/E

E. XOR and favorite number Time limit per test 4 seconds memory limit per test Megabytes input Standard Input Output Standard Output

Bob has a favorite numberkanda i of length n. Now he asks your to answer m queries. Each query was given by a pair li and Ri and asks you to count th E 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? ≤? L i? ≤? R i? ≤? n)-the parameters of the I-th query.

Output

Print m lines, answer the queries in the order they appear in the input.

Examplesinput
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 is: (1, 2), ( 1, 4), (1, 5), ( 2,3 ), (3, 6), (5, 6), (6, 6). Not a single of these pairs are suitable for the second query.

In the second, the sample XOR equals 1 for any subarrays of an odd length.

Main topic:

There is a series of N of length, M query (L,R) within how many pairs (i, j) make Ai ^ ai+1 ^ ... ^ aj = K;

Problem Solving Ideas:

The interval query here is offline and can be used in the legendary MO team algorithm (elegant and gorgeous brute force algorithm).

One of the most ingenious parts of an XOR topic is the use of A^b^a = B as a property.

This topic we have to preprocess the sum (i) before I number of XOR and, then ai ^ ai+1 ^ ... ^ aj = sum (i-1) ^ sum (j).

Next we can follow the query interval with the MO team algorithm to traverse through, while recording the current interval a prefix and the number of occurrences;

According to sum (i-1) ^ sum (j) = k, k ^ sum (i-1) = SUM (j) | | K ^ sum (j) = SUM (i-1), the number of qualifying (I,J) is introduced by the prefix and number of times that match the condition.

TIP:

Listening to the lesson of the great God, this problem has two pits:

1, the data of the answer data range is burst int;

2, although the 1e6 K, but the result of the XOR is greater than 1e6;

AC Code:

1#include <bits/stdc++.h>2 #defineLL Long Long int3 using namespacestd;4 Const intMAXN =1<< -;5 6 structnode7 {8     intL, R, id;//interval and query number9}Q[MAXN];//Record query data (offline)Ten  One intPOS[MAXN];//record chunking ALL ANS[MAXN];//Record Answers -LL FLAG[MAXN];//maintain prefix XOR and occurrences - intA[MAXN];//Original Data the intL=1R//the left and right nodes of the current interval -LL Res;//stores the value of the current interval - intN, M, K; - BOOLCMP (Node A, Node B)//Sort + { -     if(POS[A.L]==POS[B.L])returnA.R < B.R;//only the left node is the right node in the same block . +     returnPOS[A.L] < POS[B.L];//Otherwise, the left node is divided into blocks. A } at voidAddintx) - { -res+=flag[a[x]^K]; -flag[a[x]]++; - } - voidDelintx) in { -flag[a[x]]--; tores-=flag[a[x]^K]; + } - intMain () the { *scanf"%d%d%d", &n, &m, &K); $     intSZ =sqrt (N);Panax Notoginseng      for(inti =1; I <= N; i++) {//read in Data -scanf"%d", &a[i]); theA[i] = a[i]^a[i-1];//computes the prefix XOR or +Pos[i] = I/sz;// chunking A     } the      for(inti =1; I <= M; i++) {//read-in Query +scanf"%d%d", &AMP;Q[I].L, &Q[I].R); -Q[i].id =i; $     } $Sort (q+1, q+1+M, CMP); -flag[0] =1; -      for(inti =1; I <= M; i++){ the          while(L < Q[I].L) {//The current left node is smaller than the query node. -Del (L-1);Wuyil++; the         } -          while(L > Q[I].L) {//The current left node is larger than the left node of the query. Wul--; -Add (l1); About         } $          while(R < Q[I].R) {//The current right node is smaller than the query right node. -r++; - Add (R); -         } A          while(R > Q[I].R) {//Current right node is larger than query right node + del (R); ther--; -         } $Ans[q[i].id] =Res; the     } the      for(inti =1; I <= M; i++){ theprintf"%lld\n", Ans[i]); the     } -}
View Code

Codeforces Round #340 (Div. 2) E. XOR and favorite number "Mo Team algorithm + xor and prefix and ingenious"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.