Topic links
Test instructions: Sequence with a sequence length of n (1<= n <= 200,000), Q (<=200,000) sub-interval query, q interval [l,r], how many different successive increments of ternary group .
IDEA: Continuous ternary group, recursive O (n) will be the first occurrence of the ternary group subscript records into a tree array, and a next[] to represent a recursive relationship, that is, the next occurrence of the same ternary position is next[x]; This is critical, when you follow the left edge processing ( A bit like the MO Team algorithm), has been recursive on the left side of the l+1 repeated occurrences of the ternary group, in order to push the ternary group to appear in [L,r] or [r+1,..] , it is not heavy and does not leak.
Use map to maintain the ID that appears, the topic is very classic ~ ~
#include <bits/stdc++.h>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1typedef pair<int,int>PII;#defineA First#defineB Second#defineMK Make_pairtypedef __int64 LL;TYPEDEF unsignedint UINT; template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}intT,kase =1, I,j,k,n,m,l,r;#defineN 200200Map<PAIR<PII,int,int>MP;intA[n],b[n],next[n],x[n],y[n],o[n],ans[n];BOOLcmpintAintb) {returnX[a] <x[b];}#defineLowbit (x) (x& (×))voidUpdateintPintd) { while(P <= N) B[P] + = d,p + =lowbit (P);}intQueryintx) { intAns =0; while(x) ans + = b[x],x-=lowbit (x); returnans;}intMain () {//freopen ("Data.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);Read1 (T); while(t--) {mp.clear (); Read1 (n); REP1 (i,1, n) b[i] = next[i] =0; REP1 (i,1, N) read1 (A[i]); REP1 (i,3, N) { if(A[i] >= a[i-1] && a[i-1] >= a[i-2]){ intx = MP[MK (MK (a[i],a[i-1]), a[i-2])]; if(x = =0) Update (I,1); ElseNEXT[X] = i;//RecursiveMP[MK (MK (a[i],a[i-1]), a[i-2])] =i; }} read1 (M); Rep0 (i,0, m) read2 (X[i],y[i]), o[i] =i; Sort (O,o+m,cmp); intp =1; Rep0 (i,0, M) { intL = X[o[i]],r =Y[o[i]]; while(P <= L +1){ if(Next[p]) update (Next[p],1);//push to the next ~ ~p++; } if(R > L +1) Ans[o[i]] = query (r)-query (L +1); ElseAns[o[i]] =0; } rep0 (I,0, M) {printf ("%d\n", Ans[i]); } } return 0;}
Hdu 5654 Xiaoxin and his watermelon candy tree array maintain interval unique tuples