BZOJ4241: A historical study

Source: Internet
Author: User

The MO team algorithm, consider how to quickly maintain the maximum importance degree.

Considering that the answer is only $o (n) $ of different values, the size of these values can be processed first by $o (N\log N) $, and the values are discretized, and two pointers to the pre and NXT are maintained for each occurrence of each event. Indicates the number of occurrences decreased or increased one after the first few small.

These values are then chunked, and each block maintains what values appear in the block.

Obviously, the modification is $o (1) $.

When querying from backward to forward scan, encountered the first number of blocks in the block from backward scanning, time complexity $o (\sqrt{n}) $.

So the total complexity is $o ((n+q) \sqrt{n}) $.

#include <cstdio> #include <cmath> #include <algorithm>using namespace std;typedef long Long ll;const int n=210000,k=9,buf=6000000,out=2000000;struct query{int L,r,id;} Ask[n];int n,q,m,lim,i,pos[n],a[n],b[n],c[n],ap[n];int pre[n],nxt[n],loc[n],cnt,sum[n],en[n];ll ans[N];struct P{ll X;int y; P () {}p (ll _x,int _y) {x=_x,y=_y;}} v[n];inline bool CMPV (const P&a,const p&b) {return a.x<b.x;} inline BOOL CMP (const Query&a,const query&b) {return pos[a.l]<pos[b.l]| | POS[A.L]==POS[B.L]&AMP;&AMP;A.R&LT;B.R;}  inline int lower (int x) {int l=1,r=n,mid,t;  while (l<=r) if (b[mid= (l+r) >>1]<=x) l= (t=mid) +1;else r=mid-1; return t;}  inline void Add (int x) {int&y=loc[x]; y=nxt[y],ap[y]=1,sum[y>>k]++;}  inline void del (int x) {int&y=loc[x]; AP[Y]=0,SUM[Y&GT;&GT;K]--, y=pre[y];} inline ll Askmax () {for (Int. i=cnt;~i;i--) if (Sum[i]) for (int j=en[i];j;j--) if (Ap[j]) return v[j].x;} Char buf[buf],*buf=buf,out[out],*ou=out;int outn[30],outcnt;inline void Read (int&A) {for (a=0;*buf<48;buf++), while (*buf>47) a=a*10+*buf++-48;}  inline void Write (ll x) {if (!x) *ou++=48;    else{for (outcnt=0;x;x/=10) outn[++outcnt]=x%10+48;  while (OUTCNT) *ou++=outn[outcnt--];  }}int Main () {fread (Buf,1,buf,stdin); Read (n); read (q);  lim= (int) sqrt (n+0.5);  for (i=1;i<=n;i++) read (A[i]), B[i]=a[i],pos[i]=i/lim;  Sort (b+1,b+n+1);  for (i=1;i<=n;i++) C[i]=lower (A[i]);  for (i=1;i<=n;i++) v[++m]=p (0,i);  for (i=1;i<=n;i++) ap[c[i]]++,v[++m]=p ((LL) a[i]*ap[c[i]],c[i]);  Sort (V+1,V+M+1,CMPV);  for (i=1;i<=n;i++) ap[i]=0;    for (i=1;i<=m;i++) {if (Ap[v[i].y]) Pre[i]=ap[v[i].y],nxt[ap[v[i].y]]=i;else loc[v[i].y]=i;  ap[v[i].y]=en[i>>k]=i;  } cnt=m>>k;  for (i=1;i<=q;i++) read (ASK[I].L), read (ASK[I].R), ask[i].id=i;  Sort (ask+1,ask+q+1,cmp);  for (i=1;i<=n;i++) ap[i]=1,sum[i>>k]++;  Int*l=c+1,*r=c;    for (i=1;i<=q;i++) {INT*L=C+ASK[I].L,*R=C+ASK[I].R;    if (R<r) {for (r++;r<=r;r++) Add (*r); r--;}    if (r>r) for (; r>r;r--) del (*R); if (l&LT;L) for (; l<l;l++) del (*L);    else if (l>l) {for (l--;l>=l;l--) Add (*l); l++;}  Ans[ask[i].id]=askmax ();  } for (i=1;i<=q;i++) write (Ans[i]), *ou++=10;  Fwrite (out,1,ou-out,stdout); return 0;}

  

BZOJ4241: A historical study

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.