[NOI2016] explanations interval (decision monotonic segment tree optimization +)

4653: [Noi2016] section

Time Limit: 60 Sec  Memory Limit: 256 MB
Submit: 1593  Solved: 869
[Submit][Status][Discuss]

Description

On the number line with n closed interval [l1, r1], [l2, r2], ..., [ln, rn]. Now selected from m intervals, so that these m interval comprises at least one common position. In other words, so that there is an x, such that for each selected interval [li, ri], there li≤x≤ri.
For the selection of a legitimate program, its cost minus the selected minimum interval length is selected longest interval length. Interval [li, ri] is defined as the length ri-li, i.e., a value equal to its right end point minus the value of the left point.
All programs seek legal minimum cost. If the absence of a legitimate program, outputs -1.

Input

The first line contains two positive integers n, m separated by a space, meaning as described above. Ensure 1≤m≤n
Subsequently n rows, each row represents a section, separated by a space containing the left and right end points of two integers ri and li for the interval.
N<=500000,M<=200000,0≤li≤ri≤10^9

Output

Only one line containing a positive integer that is the minimum cost.

Sample Input

6 3
3 5
1 2
3 4
2 2
1 5
1 4

Sample Output

2
 
 

The very idea of ​​God fucks title.

If the complexity of the matter, we can consider first of all discrete intervals, then sorted according to the length of the interval. Then sweep from left to right, starting at each interval interval, try interval after individually added. How to join it? Denotes a cover layers, it points to the left between all points +1 right end points, showing a multi-layer cover with an array. When there is a point when the cover layer to m, the current statistics about the length of the right-most section of the starting interval difference update answers. In fact, we specifically selected and no matter what the interval, just two maximum length and the minimum length of the answer can be updated, so just make sure to choose a legitimate, do not discuss specific options.

(Blogger wrote here, wrestling 40min, or did it come out of the correctness of the certificate, so the cushions)

(After the tree line optimization at intervals additions like)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ls(k) k<<1
#define rs(k) k<<1|1
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
const int N=1000005;
int n,m,c[N<<1],tot,top,ans=0x3f3f3f3f;
struct node
{
    int l,r,len; 
}q[N];
int cmp(node x,node y)
{
    return x.len<y.len;
}
int maxx[N<<2],lz[N<<2];
void down(int k,int l,int r)
{
    if(l==r)
    {
        lz[k]=0;
        return ;
    }
    lz[ls(k)]+=lz[k];lz[rs(k)]+=lz[k];
    maxx[ls(k)]+=lz[k];maxx[rs(k)]+=lz[k];
    lz[k]=0;
}
void add(int k,int l,int r,int L,int R,int val)
{
    if(L<=l&&R>=r)
    {
        maxx[k]+=val;
        lz[k]+=val;
        return ;
    }
    int mid=l+r>>1;
    if(lz[k])down(k,l,r);
    if(L<=mid)add(ls(k),l,mid,L,R,val);
    if(R>mid)add(rs(k),mid+1,r,L,R,val);
    maxx[k]=max(maxx[ls(k)],maxx[rs(k)]);
}
int main()
{
    n=read();m=read();
    for(int i=1;i<=n;i++)
        q[i].l=read(),q[i].r=read(),q[i].len=q[i].r-q[i].l,c[++tot]=q[i].l,c[++tot]=q[i].r;
    sort(c+1,c+tot+1);
    tot=unique(c+1,c+tot+1)-c-1;
    for(int i=1;i<=n;i++)
        q[i].l=lower_bound(c+1,c+tot+1,q[i].l)-c,q[i].r=lower_bound(c+1,c+tot+1,q[i].r)-c;
    sort(q+1,q+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        while(maxx[1]<m&&top<n)
            top++,add(1,1,tot,q[top].l,q[top].r,1);
        if(maxx[1]==m)ans=min(ans,q[top].len-q[i].len);
        add(1,1,tot,q[i].l,q[i].r,-1);
    }
    if(ans==0x3f3f3f3f)puts("-1");
    else cout<<ans<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Rorschach-XR/p/11298543.html