Bzoj 1293: [SCOI2009] Birthday gift "Monotone queue"

Source: Internet
Author: User

At first, the complexity of the line tree +hash that seems to be very constant is huge. Then I found the queue ...
Sort the beads by location, then use the queue to maintain a section, enumerate right endpoint update answer can

#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;const int N=1000005,inf    =2147483647;int n,m,s[65],ans=inf,tot,sum,l=1,r;struct qwe{int k,p;    qwe (int k=0,int p=0) {k=k,p=p; }}a[n],q[n];bool CMP (const qwe &a,const qwe &b) {return A.P&LT;B.P;}    int read () {int r=0,f=1;    Char P=getchar (); while (p> ' 9 ' | |        p< ' 0 ') {if (p== '-') f=-1;    P=getchar ();        } while (p>= ' 0 ' &&p<= ' 9 ') {r=r*10+p-48;    P=getchar (); } return r*f;}    int main () {N=read (), M=read ();    for (int i=1;i<=m;i++) for (int t=read (); t>=1;t--) A[++tot]=qwe (I,read ());    Sort (a+1,a+n+1,cmp);        for (int i=1;i<=n;i++) {q[++r]=a[i];        if (++s[q[r].k]==1) sum++;            while (sum==m) {ans=min (ANS,Q[R].P-Q[L].P);            s[q[l].k]--;            if (s[q[l].k]==0) sum--;        l++; }} printf("%d\n", ans); return 0;}

Very stupid line segment tree

luogu-judger-enable-o2#include<iostream> #include <cstdio> #include <vector> #include < Algorithm> #include <map>using namespace std;const int N=1000005;int n,m,g[n],tot,rl[n],has,ans=2e9,mn=0; vector<int>a[65],b[n];map<int,int>mp;struct xds{int l,r,mn;}    T[505];int read () {int r=0,f=1;    Char P=getchar (); while (p> ' 9 ' | |        p< ' 0 ') {if (p== '-') f=-1;    P=getchar ();        } while (p>= ' 0 ' &&p<= ' 9 ') {r=r*10+p-48;    P=getchar (); } return r*f;}    void build (int ro,int l,int r) {t[ro].l=l,t[ro].r=r;    if (l==r) return;    int mid= (L+R) >>1;    Build (Ro<<1,l,mid); Build (Ro<<1|1,mid+1,r);}        void update (int ro,int P,int v) {if (T[RO].L==T[RO].R) {t[ro].mn=v;    Return    } int mid= (T[RO].L+T[RO].R) >>1;    if (p<=mid) update (RO&LT;&LT;1,P,V);    else update (RO&LT;&LT;1|1,P,V); T[ro].mn=min (T[ro<<1].mn,t[ro<<1|1]. mn);}    int main () {N=read (), M=read ();            for (int. i=1;i<=m;i++) {for (int j=read (); j>=1;j--) {g[++tot]=read ();        A[i].push_back (G[tot]);    } mn=max (Mn,a[i][0]);    } sort (G+1,g+1+tot); for (int i=1;i<=tot;i++) if (i==1| |    G[I]!=G[I-1]) mp[g[i]]=++has,rl[has]=g[i];    for (int i=1;i<=m;i++) for (int j=0,len=a[i].size (); j<len;j++) B[mp[a[i][j]]].push_back (i);    Build (1,1,M);                for (int i=1;i<=has;i++) if (rl[i]>=mn) {for (int j=0,len=b[i].size (); j<len;j++)            Update (1,b[i][j],rl[i]);        Ans=min (ANS,RL[I]-T[1].MN);    } printf ("%d\n", ans); return 0;}

Bzoj 1293: [SCOI2009] Birthday present "monotone queue"

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.