"NOI2016" problem-solving interval report

"NOI2016" section

Recently a good stiff thinking ah ...

That one up on the first section is split into two endpoints difference, then scan position sequence, maintenance answer at each position, the current position of the maintenance interval sequence data structure, but not maintained.

So want to study nature, I think why bring interval length weights do it, do you have some property

So think of the nature of the interval length for a long time, I sb guess some conclusions, such as what a zone only when the contribution is added and deleted count on it like ...

All wrong then the autistic ...

Then think what the King James maximum, and then ask Interval, then I found each segment tree to hang a monotonous point queue (in fact the monotonous queue from the tree line to win is a positive solution, I did not think it was), then and a self-closing

Then think altogether Imperial maximum and minimum, anyway, only \ (n ^ 2 \) species, then we need to check the length of the interval between the minimum and maximum values, and then we look at the range of these intervals and there is no value of a position than \ (m \) larger range and can be dyed, then queries the maximum

This can take the violence out of the tree, the tree cover, seems to be on

Maximum Minimum then found two pointers if you are scanning, then move is \ (O (n) \) , and borrowing from the ruler is like asking, then you can only maintain a line of trees.


Code:

#include <cstdio>
#include <cctype>
#include <algorithm>
using std::max;
const int SIZE=1<<21;
char ibuf[SIZE],*iS,*iT;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,SIZE,stdin),iS==iT?EOF:*iS++):*iS++)
//#define gc() getchar()
template <class T>
void read(T &x)
{
    x=0;char c=gc();
    while(!isdigit(c)) c=gc();
    while(isdigit(c)) x=x*10+c-'0',c=gc();
}
void ckmin(int &x,int y){x=x<y?x:y;}
const int inf=0x3f3f3f3f;
const int N=5e5+10;
int n,m,k,saki[N<<1];
struct _toki
{
    int l,r,val;
    bool friend operator <(_toki a,_toki b){return a.val<b.val;}
}toki[N];
int mx[N<<3],tag[N<<3];
#define ls id<<1
#define rs id<<1|1
void pushdown(int id)
{
    if(tag[id])
    {
        mx[ls]+=tag[id],tag[ls]+=tag[id];
        mx[rs]+=tag[id],tag[rs]+=tag[id];
        tag[id]=0;
    }
}
void upt(int id,int L,int R,int l,int r,int d)
{
    if(r<L||l>R) return;
    if(l<=L&&R<=r)
    {
        mx[id]+=d;
        tag[id]+=d;
        return;
    }
    pushdown(id);
    int Mid=L+R>>1;
    upt(ls,L,Mid,l,r,d),upt(rs,Mid+1,R,l,r,d);
    mx[id]=max(mx[ls],mx[rs]);
}
int qry(int id,int L,int R,int l,int r)
{
    if(r<L||l>R) return 0;
    if(l<=L&&R<=r) return mx[id];
    pushdown(id);
    int Mid=L+R>>1;
    return max(qry(ls,L,Mid,l,r),qry(rs,Mid+1,R,l,r));
}
int main()
{
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    read(n),read(k);
    for(int i=1;i<=n;i++)
    {
        read(toki[i].l),read(toki[i].r);
        saki[++m]=toki[i].l,saki[++m]=toki[i].r;
        toki[i].val=toki[i].r-toki[i].l;
    }
    std::sort(saki+1,saki+1+m);
    m=std::unique(saki+1,saki+1+m)-saki-1;
    for(int i=1;i<=n;i++)
    {
        toki[i].l=std::lower_bound(saki+1,saki+1+m,toki[i].l)-saki;
        toki[i].r=std::lower_bound(saki+1,saki+1+m,toki[i].r)-saki;
    }
    std::sort(toki+1,toki+1+n);
    int l=1,r=1,ans=inf;
    while(r<=n)
    {
        upt(1,1,m,toki[r].l,toki[r].r,1);
        while(qry(1,1,m,toki[r].l,toki[r].r)>=k)
        {
            ckmin(ans,toki[r].val-toki[l].val);
            upt(1,1,m,toki[l].l,toki[l].r,-1);
            ++l;
        }
        ++r;
    }
    if(ans==inf) puts("-1");
    else printf("%d\n",ans);
    return 0;
}

2019.5.30

Guess you like

Origin www.cnblogs.com/butterflydew/p/10950152.html