Question: Given a sequence, how many different numbers are in an interval
A positive solution is a tree array that sorts all the intervals by the left endpoint and then counts the first number of each color at the left end of the line. Maintain with a tree array
I'm writing about the MO team. Obviously it's a slow m√m, but it's still a complicated one to accept.
At first, the discretization array opens up a small number of seconds re ... On my knees
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define M 50500using namespace std;struct abcd{int L,r,pos;bool operator < (const ABCD &x) const;} Q[200200];int n,m,block,l=1,r=0,now,a[m],map[1001001],tot;int cnt[m],ans[200200];bool ABCD:: operator < (const ABCD &x) const{if ((l-1)/block = = (X.L-1)/block) return R < X.R; return (L-1)/block < (X.L-1)/block;} inline void Update (int x) {cnt[x]++;if (cnt[x]==1) ++now;} inline void downdate (int x) {cnt[x]--;if (!cnt[x])--now;} int main () {int i,x;cin>>n;for (i=1;i<=n;i++) {scanf ("%d", &x), if (!map[x]) map[x]=++tot;a[i]=map[x];} Cin>>m;for (i=1;i<=m;i++) scanf ("%d%d", &Q[I].L,&Q[I].R),q[i].pos=i;block=static_cast<int> (sqrt (n) +1e-7), sort (q+1,q+m+1), for (i=1;i<=m;i++) {while (R<Q[I].R) update (A[++r]), while (L>Q[I].L) update ( A[--L]), while (R>Q[I].R) downdate (a[r--]), while (L<Q[I].L) downdate (a[l++]); ans[q[i].pos]=now;} for (i=1;i<=m;i++) printf ("%d\n", Ans[i]);}
Bzoj 1878 SDOI2009 hh necklace tree-like array/mo-Team algorithm