Bzoj 3585 Mex MO Team Algorithm + sub-block

Source: Internet
Author: User

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&LT;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", &AMP;QUERIES[I].L,&AMP;QUERIES[I].R), Queries[i].id=i;sort (queries+1,queries+n+1), for (i=1;i<=m;i++) {while (R&LT;QUERIES[I].R) Update (A[++r]); while (L&GT;QUERIES[I].L) Update (A[--l]), while (R&GT;QUERIES[I].R) downdate (a[r--]), while (L&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.