Codeforces 703D D. Mishka and interesting sum (tree-like array)

Source: Internet
Author: User

Topic Links:

D. Mishka and Interesting sum

Time limit per test3.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Little Mishka enjoys programming. Since her birthday had just passed, she friends decided to present she with array of non-negative integers a C3>1, a2, ..., an of n elements!

Mishka loved the array and she instantly decided to determine it beauty value, but she is too little and can ' t process la rge arrays. Right because of this she invited you to visit her and asked to process m queries.

Each query was processed in the following:

  1. Integers l and R (1≤ l ≤ r ≤ n) are specified-bounds of Qu ery segment.
  2. integers, presented in array segment [l, R] (in sequence of integers al , al + 1, ..., aR) even number of times, is written down .
  3. Xor-sum of written down integers was calculated, and this value was the answer for a query. Formally, if integers written down with point 2 is x1, x2, ..., xk, the N Mishka wants to know the value, Where-operator of exclusive bitwise OR.

Since only the little bears know the definition of an array beauty, all your be to does is to answer each of the queries presented.

Input

The first line of the input contains a single integer n (1≤ n ≤1)-the number of Ele ments in the array.

The second line of the input contains n integers a1, a2, ..., a n ( 1≤ ai ≤109)-array elements.

The third line of the input contains single integer m (1≤ m ≤1)-the number of Queries.

Each of the next m lines describes corresponding query by a pair of integers l and r (1≤ l ≤ r ≤ n)-the bounds of query segment.

Output

Print m non-negative integers-the answers for the queries of the order they appear in the input.

Examplesinput
3
3 7 8
1
1 3
Output
0
Input
7
1 2 1 3 3 2 3
5
4 7
4 5
1 3
1 7
1 5
Output
 0 
3
1
3
2

Test instructions:

Ask what is the number of an even number of times in an interval;

Idea:

an interval of XOR and get is the occurrence of odd number of the number of XOR, and now again XOR or all of the numbers of the XOR is the answer; offline processing, sorted by interval,
combined with a map to save the nearest front position with the same number of each digit, Then use the tree-like array to delete the unused precursor position every time;

AC code:

/************************************************┆┏┓┏┓┆┆┏┛┻━━━┛┻┓┆┆┃┃┆┆┃━┃┆┆┃┳┛┗┳┃┆┆┃┃ ┆┆┃┻┃┆┆┗━┓┏━┛┆┆┃┃┆┆┃┗━━━┓┆┆┃ac Horse ┣┓┆┆┃┏┛┆┆┗┓┓┏━┳┓┏┛┆┆┃┫┫┃┫┫┆ ┆┗┻┛┗┻┛┆************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include < Stack> using namespace std; #define for (i,j,n) for (int i=j;i<=n;i++) #define MST (SS,B) memset (ss,b,sizeof (ss)); typedef long Long LL;    Template<class t> void Read (t&num) {char CH; bool F=false; For (Ch=getchar (); ch< ' 0 ' | | Ch> ' 9 ';    f= ch== '-', Ch=getchar ()); for (num=0;    ch>= ' 0 ' &&ch<= ' 9 '; num=num*10+ch-' 0 ', Ch=getchar ()); F && (num=-num);}    int stk[70], tp;template<class t> inline void print (T p) {if (!p) {puts ("0"); return;} while (p) stk[++ TP] = p%10, p/=10;    while (TP) Putchar (stk[tp--] + ' 0 '); Putchar (' \ n ');} Const LL Mod=1e9+7;const double Pi=acos ( -1.0); const int INF=1E9;CONST int N=1e6+10;const int Maxn=2e3+14;const double eps= 1e-12;int n,a[n],sum[n],ha[n];int lowbit (int x) {return x& (-X);}        void update (int x,int temp) {while (x<=n) {sum[x]^=temp;    X+=lowbit (x);    }}int query (int x) {int s=0;        while (x) {s^=sum[x];    X-=lowbit (x); } return s;} struct node{int l,r,id;}    Po[n];int CMP (node A,node b) {//if (A.R==B.R) return a.l<b.l; return A.R&LT;B.R;}        Map<int,int>mp;int Pre[n],ans[n];int Main () {read (N);            for (i,1,n) {read (a[i]);            Ha[i]=ha[i-1]^a[i];            Pre[i]=mp[a[i]];        Mp[a[i]]=i;        } int q;        Read (q);            for (I,1,Q) {read (PO[I].L);            Read (PO[I].R);        Po[i].id=i;        } sort (po+1,po+q+1,cmp);//cout<< "# #" <<endl;  int p=1;      for (I,1,Q) {while (P&LT;=PO[I].R) {if (pre[p]) update (pre[p],a[pre[p]);                Update (p,a[p]);            p++;            } ans[po[i].id]= (Ha[po[i].r]^ha[po[i].l-1]^query (PO[I].R) ^query (PO[I].L-1)); cout<<po[i].id<< "" <<sum[po[i].l-1]<< "<<sum[3]<<" "&LT;&LT;SUM[PO[I].R"        <<endl;        } for (i,1,q) printf ("%d\n", Ans[i]); return 0;}

  

Codeforces 703D D. Mishka and interesting sum (tree-like array)

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.