XOR and favorite number (Mo pair algorithm)

Source: Internet
Author: User

E. XOR and favorite numbertime limit per test4 secondsmemory limit per test256 megabytesinputstandard Inputoutputstandard 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 thatl? ≤? 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.

Examplesinputcopy
6 2 3
1 2 1 1 0 3
1 6
3 5
Outputcopy
7
0
Inputcopy
5 3 1
1 1 1) 1 1
1 5
2 4
1 3
Outputcopy
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.

Test instructions: There are n number and M inquiry, each inquiry will have a L and R, indicating the interval of inquiry,

Ask how many consecutive sub-ranges are in this interval or K

Suppose we now have a prefix XOR or array sum[], now we ask for the XOR value of the interval [l,r],

Using the sum array is sum[l-1]^sum[r]==k, or k^sum[r]==sum[l-1.

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Const intMAXN = 2e6 +Ten;5 intN, M, K, L, R, SZ, A[MAXN];6 LL Sum[maxn], ans, ans[maxn];7 structNode {8     intL, R, id;9 node () {}TenNodeintLintRintID): L (L), R (r), ID (ID) {} One     BOOL operator< (ConstNode & A)Const { A         if(L/sz = = A.l/sz)returnR <A.R; -         returnL <A.L; -     } the } QU[MAXN]; - voidAddintx) { -Ans + = sum[a[x] ^K]; -sum[a[x]]++; + } - voidDelintx) { +sum[a[x]]--; AAns-= sum[a[x] ^K]; at } - intMain () { -scanf"%d%d%d", &n, &m, &k); -      for(inti =1; I <= N; i++) { -scanf"%d", &a[i]); -A[i] ^= a[i-1]; in     } -      for(inti =1; I <= m; i++) { toscanf"%d%d", &AMP;QU[I].L, &QU[I].R); +qu[i].l--; -Qu[i].id =i; the     } *SZ = (int) sqrt (n); $Sort (qu +1, Qu + M +1);Panax NotoginsengL =1, R =0; -      for(inti =1; I <= m; i++) { the          while(L > Qu[i].l) Add (--L); +          while(R < QU[I].R) Add (+ +R); A          while(L < QU[I].L) del (l++); the          while(R > Qu[i].r) del (r--); +Ans[qu[i].id] =ans; -     } $      for(inti =1; I <= m; i++) $printf"%lld\n", Ans[i]); -     return 0; -}

XOR and favorite number (Mo pair algorithm)

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.