[Topic link]
https://www.lydsy.com/JudgeOnline/problem.php?id=2743
Algorithm
First preprocess the nxt[] array, where Nxt[i] represents the next and the I-bit color of the same location, and then offline, will be asked to sort by the left endpoint, each time nxt[i] minus one, nxt[nxt[i]) plus a
Use a tree-like array for maintenance, see Code
Complexity of Time: O (MLOGN)
Code
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e6 +Ten;structque{intL, R, id;} Q[MAXN];intN, c, M;intA[MAXN], ANS[MAXN], PRE[MAXN], NXT[MAXN];BOOLVISITED[MAXN];structbinary_indexed_tree{intC[MAXN]; InlineintLowbit (intx) {returnX & (-x); } InlinevoidAddintPosintvalue) { if(!pos)return; for(inti = pos; I <= N; i + =lowbit (i)) C[i]+=value; } InlineintQueryintPOS) { intRET =0; if(!pos)return 0; for(inti = pos; I I-=lowbit (i)) RET+=C[i]; returnret; } InlineintQueryintLintr) {returnQuery (r)-Query (L-1); }} bit;template<typename t> InlinevoidChkmax (T &x,t y) {x =Max (x, y);} Template<typename t> InlinevoidChkmin (T &x,t y) {x =min (x, y);} Template<typename t> InlinevoidRead (T &x) {T F=1; x =0; Charc =GetChar (); for(;!isdigit (c); c = GetChar ())if(c = ='-') F =-F; for(; IsDigit (c); c = GetChar ()) x = (x <<3) + (x <<1) + C-'0'; X*=F;} InlineBOOLCMP (Que a,que b) {returnA.L <B.L;}intMain () {read (n); Read (c); Read (m); for(inti =1; I <= N; i++) read (A[i]); for(inti = n; I >=1; i--) { if(!Visited[a[i]]) {Visited[a[i]]=true; Pre[a[i]]=i; Continue; } Else{Nxt[i]=Pre[a[i]]; Pre[a[i]]=i; }} memset (visited,false,sizeof(visited)); for(inti =1; I <= N; i++) { if(!Visited[a[i]]) {Bit.add (nxt[i],1); Visited[a[i]]=true; } } for(inti =1; I <= m; i++) {read (Q[I].L); Read (Q[I].R); Q[i].id=i; } sort (Q+1, q + M +1, CMP); intCur =0; for(inti =1; I <= m; i++) { while(Cur <Q[I].L) {Bit.add (nxt[cur],-1); Bit.add (Nxt[nxt[cur]),1); ++cur; } Ans[q[i].id]= Bit.query (Q[I].R)-Bit.query (Q[I].L-1); } for(inti =1; I <= m; i++) printf ("%d\n", Ans[i]); return 0; }
[Heoi 2012] Picking Flowers