4653: [Noi2016] section
Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1593 Solved: 869
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
3 5
1 2
3 4
2 2
1 5
1 4
Sample Output
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; }