Reprinted from: http://www.cnblogs.com/icode-girl/p/5744409.html
Topic Links: CF #365 (Div. 2) D-mishka and interesting sum
Test instructions: gives the number of N and M queries, (1≤ N, m ≤1 000 000), asking for all occurrences of an even number of different values in each interval.
Idea: It is easy to think that all the numbers in the interval will be different or get the number of odd number of values, and then different or within the interval of all occurrences of the number (each count only once), get the ANS.
The first question: Get the XOR value of all the numbers of the asking interval, by a[1~r] ^ a[0~ (l-1)] = A[l~r] To save the XOR value of all the numbers of the all_xor[i] interval with the array a[1~i], each time the All_xor value of the left and right breakpoints of the asking interval is different or not.
The second problem: Get the XOR value of all occurrences in the interval, an offline tree array. Here's how:
First, follow the right section of the inquiry to save all inquiry G[r]. (l, id), the result of the current inquiry is saved in Ans[id].
Use the tree array One_xor[i] Save A[1~i] Interval all occurrences of the number (only once) the XOR value. Traverse A[i], if A[i] appeared, for the following query, the right interval must be >=i, that is, I before the interval is not concerned about whether this value has occurred,
Add_xor (Mp[a[i]], a[i]), so that the preceding interval no longer has this number. Then mp[a[i]] = i, Add_xor (Mp[a[i]], a[i]); This ensures that each subsequent query is found only where the last occurrence of the a[i] value a[i].
#include <stdio.h>#include<string.h>#include<iostream>#include<vector>#include<map>#defineMAXN 1000010using namespacestd;intA[MAXN], ONE_XOR[MAXN], ANS[MAXN], ALL_XOR[MAXN];//corresponding to the original array (0~i) all occurrences of the number of XOR (0~i) all the number of XOR structQuery {intl, id;}; Map<int,int>Mp;vector<Query>QUERY[MAXN];intN, M;voidinit () {mp.clear (); for(intI=1; i<=maxn; ++i) {query[i].clear (); } memset (One_xor,0,sizeof(One_xor));} voidAdd_xor (intXintval) { for(; x<=n; x+= (x& (-x))) One_xor[x]^=Val;} intSum_xor (intLintr) {intAns =0; for(; r>0; r-= (r& (-R))) ans ^=One_xor[r]; for(; l>0; l-= (l& (L))) ans ^=One_xor[l]; returnans;} intMain () { while(~SCANF ("%d", &N)) {init (); all_xor[0] =0; for(intI=1; i<=n; ++i) {scanf ("%d", &A[i]); All_xor[i]= all_xor[i-1]^A[i]; } scanf ("%d", &m); for(intI=1; i<=m; ++i) {intL, R; scanf ("%d%d", &l, &R); Query[r].push_back ({l, i}); } for(intI=1; i<=n; ++i) {if(Mp.count (A[i])) {Add_xor (Mp[a[i]], a[i]); } Mp[a[i]]=i; Add_xor (Mp[a[i]], a[i]); for(intj=0; J<query[i].size (); ++j) {Query now=Query[i][j]; Ans[now.id]= (All_xor[i] ^ all_xor[now.l-1]); Ans[now.id]^= Sum_xor (i, now.l-1); } } for(intI=1; i<=m; ++i) {printf ("%d\n", Ans[i]); } } return 0;}
This problem is also let me understand some of the different or operation it, original different or so interesting ~ ~
CF #365 (Div. 2) D-mishka and interesting sum offline tree array (GO)