Title: https://www.lydsy.com/JudgeOnline/problem.php?id=1878
Offline tree-like array, ingenious idea of it;
For each necklace, record the last occurrence of LST, according to whether the necklace appears in the interval to calculate the type number;
Therefore, the tree-like array is used to obtain the prefix and the result.
The code is as follows:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;int Constmaxn=50005, maxm=200005;intn,m,a[maxn],f[maxn],lst[1000005];structn{intL,r,bh,w;} Q[MAXM];BOOLCMP (N x,n y) {returnx.r<Y.R;}BOOLCMP2 (N x,n y) {returnx.bh<y.bh;}voidAddintXintk) { for(; x<=n;x+= (x&-x)) f[x]+=K;}intQueryintx) { intret=0; for(; x;x-= (x&-x)) ret+=F[x]; returnret;}intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); scanf ("%d",&m); for(intI=1; i<=m;i++) scanf ("%d%d", &Q[I].L,&Q[I].R), q[i].bh=i; Sort (q+1, q+m+1, CMP); inttot=1; for(intI=1; i<=n;i++) { if(Lst[a[i]]) Add (lst[a[i]],-1); Lst[a[i]]=i;add (I,1); while(I==Q[TOT].R) Q[tot].w=query (i)-query (q[tot].l-1), tot++; } sort (Q+1, q+m+1, CMP2); for(intI=1; i<=m;i++) printf ("%d\n", Q[I].W); return 0;}
bzoj1878 [Sdoi2009]hh's Necklace--tree-shaped array