HDU 3333:http://blog.csdn.net/julyana_lin/article/details/7877164
The two questions are similar, all of which are processed offline to sort the right endpoint of the interval for each query. Here we need to do discretization, mark the previous occurrence of this value, and then constantly update the last array (the array holds the position of each number of occurrences at the end). Finally, the tree-like array is maintained.
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 Const intMAXN =33333;5 Const intMAXQ =111111;6 structnode{7 intL,r,index;8 };9 node QUERY[MAXQ];Ten ll SUM[MAXN],ANS[MAXQ]; One ll A[MAXN],B[MAXN],LAST[MAXN]; A intN; - BOOLCMP (node A,node b) { - returnA.R <B.R; the } -ll Getsum (inti) { -ll s =0; - while(I >0){ +s + =Sum[i]; -I-= i& (-i); + } A returns; at } - voidAddinti,ll x) { - while(I <=N) { -Sum[i] + =x; -i + = i& (-i); - } in } - intMain () { to intT; +scanf"%d",&t); - while(t--){ thescanf"%d",&n); * for(inti =1; i<=n;i++){ $scanf"%i64d",&a[i]);Panax NotoginsengB[i] = A[i];//for discretization - } theSort (b +1, B +1+N);//sort, marked by the subscript of each number + intQ; Ascanf"%d",&q); the for(inti =1; i<=q;i++){ +scanf"%d%d",&query[i].l,&QUERY[I].R); -Query[i].index =i; $ } $Sort (query+1, query+1+q,cmp); -memset (SUM,0,sizeof(sum)); -Memset (Last,0,sizeof(last)); the intCNT =1;//Subscript for each query - for(inti =1; i<=n;i++){Wuyi intindex = Lower_bound (b +1, B +1+n,a[i])-b-1;//find the corresponding subscript for the number the if(Last[index])//to determine if the number has occurred, some words minus -Add (last[index],-a[i]); Wu Add (I,a[i]); -Last[index] =i; About while(Query[cnt].r==i && cnt<=q) { $Ans[query[cnt].index] = getsum (QUERY[CNT].R)-getsum (query[cnt].l-1); -cnt++; - } - } A for(inti =1; i<=q;i++) +printf"%i64d\n", Ans[i]); the } - return 0; $}
View Code
Codeforces 703d:http://codeforces.com/contest/703/problem/d
This problem needs to think more about the step is that the interval within the required number of odd or sum, equal to the interval of all the numbers of the XOR and XOR of the range of different numbers or sum, to 1, 2, 1, 3, 3, 2, 3 for example, the result is (1^2^1^3^3^2^3) ^ (1^2^3), The principle is that an even number of times the number of different or it itself equals itself, appearing odd number of different or it itself is 0. For the interval of the XOR, we can use the array is very convenient to find out, different numbers of the difference or and only need to make a change on the title of the better.
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 5 Const intMAXN =1111111;6 structnode{7 intL,r,index;8 };9 node QUERY[MAXN];Ten ll SUM[MAXN],A[MAXN],B[MAXN],C[MAXN],LAST[MAXN],ANS[MAXN]; One intN; A BOOLCMP (node A,node b) { - returnA.R <B.R; - } thell Getsum (inti) { -ll s =0; - while(I >0){ -s ^= Sum[i];//Note +I-= i& (-i); - } + returns; A } at voidAddinti,ll x) { - while(I <=N) { -Sum[i] ^= x;//Note -i + = i& (-i); - } - } in intMain () { -scanf"%d",&n); to for(inti =1; i<=n;i++){ +scanf"%i64d",&a[i]); -C[i] = a[i]^c[i-1];//To find the prefix xor or theB[i] =A[i]; * } $Sort (b +1, B +1+n);Panax Notoginseng intQ; -scanf"%d",&q); the for(inti =1; i<=q;i++){ +scanf"%d%d",&query[i].l,&QUERY[I].R); AQuery[i].index =i; the } +Sort (query+1, query+1+q,cmp); - intCNT =1; $ for(inti =1; i<=n;i++){ $ intindex = Lower_bound (b +1, B +1+n,a[i])-b-1; - if(Last[index]) - Add (Last[index],a[i]); theLast[index] =i; - Add (I,a[i]);Wuyi while(Query[cnt].r==i && cnt<=q) { theAns[query[cnt].index] = (c[query[cnt].r]^c[query[cnt].l-1]) ^ (Getsum (QUERY[CNT].R) ^getsum (query[cnt].l-1));//Note -cnt++; Wu } - } About for(inti =1; i<=q;i++) $printf"%i64d\n", Ans[i]); - return 0; -}
View Code
HDU 3333 | Codeforces 703D tree-like array, discretized