Topic: Given an array of length n, m times ask for a MEX value within a range
Angry write MO team 233
The weights are divided into √n blocks, recording the number of occurrences of each weight and how many weights appear in each block.
Modify O (1) to complete the query when you first scan to find the first block within the block that has no coverage of the point and then within the block violence find time complexity O (√n)
Set the total time complexity O (m√n) of the team
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define M 200200using namespace std;struct abcd{int L,r,id;bool operator < (const ABCD &x) const;} Queries[m];int n,m,b;int a[m],belong[m],l[m],r[m];int l=1,r=0,f[m],block_ans[m],ans[m];bool ABCD:: operator < ( const ABCD &x) const{if (BELONG[L]!=BELONG[X.L]) return L<x.l;return R<X.R;} void Update (int x) {if (x>n) return; if (!f[x]++) block_ans[belong[x]]++;} void downdate (int x) {if (x>n) return, if (!--f[x]) block_ans[belong[x]]--;} int Query () {int i;if (!f[0]) return 0;for (i=1;l[i];i++) if (block_ans[i]!=r[i]-l[i]+1) Break;int temp=i;for (i=l[temp];i <=r[temp];i++) if (!f[i]) return I;return N;} int main () {int i;cin>>n>>m;b=int (sqrt (n) +1e-7), for (i=1;i<=n;i++) belong[i]= (i-1)/b+1;for (i=1; (i-1) *b+1<=n;i++) l[i]= (i-1) *b+1,r[i]=min (I*b,n), for (i=1;i<=n;i++) scanf ("%d", &a[i]); for (i=1;i<=m;i++) scanf ("%d%d", &QUERIES[I].L,&QUERIES[I].R), Queries[i].id=i;sort (queries+1,queries+n+1), for (i=1;i<=m;i++) {while (R<QUERIES[I].R) Update (A[++r]); while (L>QUERIES[I].L) Update (A[--l]), while (R>QUERIES[I].R) downdate (a[r--]), while (L<QUERIES[I].L) Downdate (a[l++]); Ans[queries[i].id]=query (); L=QUERIES[I].L; R=QUERIES[I].R;} for (i=1;i<=m;i++) printf ("%d\n", Ans[i]); return 0;}
Bzoj 3585 Mex MO Team Algorithm + sub-block