To give you an array of length n, define the Val of the interval [l,r] as the sum of all the different values in the interval. Now there are M queries, each asking for an interval, asking what is the Val of the interval.
Idea: Sort all queries according to the right endpoint. Then the violent enumeration of the right interval, and then the previous occurrence of the Val to make a mark, each time the tag is updated. Concrete and HDU 58,691 kind, only 5869 also have to preprocess, comparatively difficult
//see if it explodes. int! array will not be one dimension less! //You must be careful about the conditions of the winning.#include <bits/stdc++.h>using namespacestd;#defineLL Long Long#defineAll (a) A.begin (), A.end ()#definePB Push_back#defineMk Make_pair#defineFi first#defineSe SecondConst intMAXN =50000+5;Const intMAXM =200000+5;Const intMaxval =1000000+5; LL TREE[MAXN], A[MAXN], Ans[maxm];vector<pair<int,int> >Q[MAXN];intN, M;intPre[maxval];inlineintLowbit (intx) {returnX &-x;}voidUpdateintXintval) { for(inti = x; I <= N; i + =lowbit (i)) Tree[i]+=Val;} LL sum (intx) {LL ans=0; for(inti = x; i >0; I-=lowbit (i)) {ans+=Tree[i]; } returnans;}intMain () {intT CIN >>T; while(t--) {memset (PRE,0,sizeof(pre)); memset (Tree,0,sizeof(tree)); scanf ("%d", &N); for(inti =1; I <= N; i++) {scanf ("%i64d", A +i); Q[i].clear (); } scanf ("%d", &m); for(inti =1; I <= m; i++){ intL, R; scanf"%d%d", &l,&R); Q[R].PB (MK (L, i)); } for(inti =1; I <= N; i++){ if(Pre[a[i]]) {update (pre[a[i]),-1*A[i]); } Pre[a[i]]=i; Update (I, a[i]); intLen =q[i].size (); for(intj =0; J < Len; J + +) {pair<int,int> P =Q[i][j]; Ans[p.second]= SUM (i)-SUM (P.first-1); } } for(inti =1; I <= m; i++) {printf ("%i64d\n", Ans[i]); } } return 0;}
View Code
Numeric tagging issues offline + Tree array HDU 3938