For one LR, each color contributes to the right-most position that appears in the (1,r) interval, so recording a B array indicates where the color of the current point appears.
Then take the inquiry offline and sort by R in ascending order
Each time you move the right endpoint to the right, put this point in the tree array +1, and at the current point of the B position-1 (indicating no use), and then the tree array prefix and minus a bit can be
I wrote the mo team will t
#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;const int N=1000005;int n,m,a[n],b[n],la[n],t[n],ans[n];struct qwe{int l,r,id;} Q[n];bool CMP (const qwe &a,const qwe &b) {return A.R<B.R;} int read () {int r=0,f=1; Char P=getchar (); while (p> ' 9 ' | | p< ' 0 ') {if (p== '-') f=-1; P=getchar (); } while (p>= ' 0 ' &&p<= ' 9 ') {r=r*10+p-48; P=getchar (); } return r*f;} void Update (int x,int W) {if (x==0) return; for (int i=x;i<=n;i+= (i& (-i))) t[i]+=w;} int ques (int x) {int r=0; for (int i=x;i>=1;i-= (i& (-i))) r+=t[i]; return r;} int main () {n=read (); for (int i=1;i<=n;i++) A[i]=read (), b[i]=la[a[i]],la[a[i]]=i; M=read (); for (int i=1;i<=m;i++) Q[i].l=read (), Q[i].r=read (), q[i].id=i; Sort (q+1,q+1+m,cmp); int r=0; for (int i=1;i<=m;i++) {while (R<Q[I].R) R++,updatE (b[r],-1), update (r,1); Ans[q[i].id]=ques (Q[I].R)-ques (Q[I].L-1); } for (int i=1;i<=m;i++) printf ("%d\n", Ans[i]); return 0;}
Bzoj 1878: [Sdoi2009]hh's Necklace "Tree array"