XOR and favorite number (mo team algorithm + Chunking)

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 that lij 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, 0≤ k ≤1)-the length of the array, the number of queries and Bob's favorite num ber respectively.

The second line contains n integers ai (0≤ a I ≤1)-bob ' s array.

Then m lines follow. The i-th line contains integers li and Ri (1≤ L irin)-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.

Test instructions: give you a sequence of size n, then give you a number k, and then ask the M group

Ask for an interval, asking how many logarithm or result is k in this interval. Practice: we can pre-preprocess all the XOR results, a[n] represents the first n number of different or results. What is the benefit of doing this? We know x^x=0, so if we ask for an XOR or result on the interval [i,j], we can get it with a[j]^a[i-1. therefore, we can start from the left endpoint, enumerate each point, enumerate the way with flag[] array to record the prefix and occurrences of the number of occurrences, such as the prefix and 2 appeared once, that flag[2]++; at the time of the query, a[i]^k is the prefix and size we are looking for, because we know that if we ask for the interval [i,j] on the XOR result, can be obtained with a[j]^a[i-1], so we want to find how many prefixes, and now the prefix XOR value is k, corresponding to the flag array to find the number of a[i]^k, and update the answer on the LINE. The above is a different or processing part. Next is the MO team Algorithm. The MO team algorithm is a brute force algorithm used to solve interval problems. His principle is that if I know the value of [l,r], I can find the values of [l+/-1,r] and [l,r+/-1] at O1 time, then the values of query [l+/-k,r] and [l,r+/-k] need K-step. If we were to query multiple intervals, it would be a minimal Manhattan spanning tree problem (minimum Benquan and all points), and the smallest Manhattan spanning tree algorithm could be used to process the Team. But in fact there are more convenient algorithms, that is, chunking. We can divide the block into sqrt (n) blocks based on the left endpoint of the interval, and the elements inside the block are sorted by the right Endpoint. After this sort, consider the left endpoint, because each block only sqrt (n) size, the left end of the movement of the most also sqrt (n) complexity, n intervals of n*sqrt (n) Complexity. For the right end point, because the right endpoint of each block is ordered, we query for the value on a block, up to n (the maximum of the interval, this is 10*n), and the maximum query sqrt (n) block, The complexity is n*sqrt (n), the total complexity n*sqrt (n). The specific code is as Follows:
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<queue>#include<stack>#include<map>#include<Set>#defineX First#defineY Second#defineCLR (u,v); Memset (u,v,sizeof (u));using namespaceStd;typedefLong LongLl;typedef pair<int,int>pii;Const intmaxn=1<< -;Const intinf=0x3f3f3f3f; ll Pos[maxn];ll flag[maxn],ans[maxn];inta[maxn];structnode{intl,r,id;} q[maxn];BOOLcmp (node a,node b) {if(pos[a.l]==Pos[b.l]) {        returna.r<b.r; }    returnpos[a.l]<pos[b.l];}intn,m,k;intL=1, r=0; ll Ans=0;voidAddintx) {Ans+=flag[a[x]^k]; flag[a[x]]++;}voidDelintx) {flag[a[x]]--; Ans-=flag[a[x]^k];}intmain () {scanf ("%d%d%d",&n,&m,&k); intsz=sqrt (n);  for(intI=1; i<=n;i++) {scanf ("%d",&a[i]); a[i]^=a[i-1]; pos[i]=i/sz; }     for(intI=1; i<=m;i++) {scanf ("%d%d",&q[i].l,&q[i].r); Q[i].id=i; } flag[0]=1; Sort (Q+1, q+m+1, cmp);  for(intI=1; i<=m;i++)    {         while(l<Q[i].l) {del (L-1); L++; }         while(l>Q[i].l) {L--; Add (L-1); }         while(r<Q[i].r) {R++;        Add (R); }         while(r>Q[i].r)            {del (R); R--; } ans[q[i].id]=Ans; }     for(intI=1; i<=m;i++) printf ("%i64d\n", ans[i]); return 0;}

2016-09-25 03:31:38

XOR and favorite number (mo team algorithm + Chunking)

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.