只要一堆线段有重叠次数大于等于 $m$ 次的位置,那么一定有解

因为重叠 $m$ 次只需 $m$ 个线断,将那些多余的线断排除掉即可

先将区间按照长度从小到大排序,再用 $two-pointer$ 从左到右扫描

不难发现左右两个指针都是不递减的,所以时间复杂度是 $O(\texttt{nlogn})$ 的

#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
namespace IO {
inline void setIO(string s) {
string in=s+".in";
string out=s+".out";
freopen(in.c_str(),"r",stdin);
}
};
const int maxn=500005;
const int inf=1000030000;
int n,m;
namespace tree {
int tag[maxn*5],maxv[maxn*5];
inline void pushup(int now) {
maxv[now]=tag[now];
maxv[now]+=max(maxv[now<<1],maxv[now<<1|1]);
}
void update(int l,int r,int now,int L,int R,int v) {
if(l>=L&&r<=R) {
tag[now]+=v, maxv[now]+=v;
return;
}
int mid=(l+r)>>1;
if(L<=mid) update(l,mid,now<<1,L,R,v);
if(R>mid) update(mid+1,r,now<<1|1,L,R,v);
pushup(now);
}
int query(int l,int r,int now,int L,int R) {
if(l>=L&&r<=R) return maxv[now];
int mid=(l+r)>>1,re=0;
if(L<=mid) re=max(re,query(l,mid,now<<1,L,R));
if(R>mid) re=max(re,query(mid+1,r,now<<1|1,L,R));
return re+tag[now];
}
};
int Arr[maxn*2];
struct Seg {
int l,r,len,L,R;
}seg[maxn];
bool cmp(Seg a,Seg b) {
return a.len<b.len;
}
int main() {
// IO::setIO("input");
scanf("%d%d",&n,&m);
int cc=0,i,j;
for(i=1;i<=n;++i) {
scanf("%d%d",&seg[i].l,&seg[i].r);
seg[i].len=seg[i].r-seg[i].l;
Arr[++cc]=seg[i].l,Arr[++cc]=seg[i].r;
}
sort(seg+1,seg+1+n,cmp);
sort(Arr+1,Arr+1+cc);
for(i=1;i<=n;++i) {
seg[i].L=lower_bound(Arr+1,Arr+1+cc,seg[i].l)-Arr;
seg[i].R=lower_bound(Arr+1,Arr+1+cc,seg[i].r)-Arr;
}
int ans=inf;
int l=1,r=0;
while(l<=n&&r<=n) {
if(tree::query(1,cc,1,1,cc)>=m) {
ans=min(ans, seg[r].len-seg[l].len);
tree::update(1,cc,1,seg[l].L,seg[l].R,-1);
++l;
}
else {
++r;
if(r<=n) tree::update(1,cc,1,seg[r].L,seg[r].R,1);
}
}
printf("%d\n",ans==inf?-1:ans);
return 0;
}

  

最新文章

  1. ok
  2. hibernate在使用sql查询query自动转化成model类型数据,query.addEntity
  3. 如何延长windows评估版的方法
  4. [USACO2005][POJ3044]City Skyline(贪心+单调栈)
  5. eclipse常用窗口和功能总结
  6. rpm包的管理
  7. HDU 5620 KK&#39;s Steel (斐波那契序列)
  8. POJ - 3903 Stock Exchange(LIS最长上升子序列问题)
  9. 深度探索QT窗口系统(五篇)
  10. jquery.load问题
  11. 对LCS算法及其变种的初步研究
  12. 数据结构(java版)学习笔记(三)——线性表之单链表
  13. UI规范案例-宝龙广场
  14. hive中的子查询改join操作(转)
  15. 校内模拟赛 虫洞(by NiroBC)
  16. java动态加载
  17. 用postman做接口测试实例
  18. Spring IOC容器启动流程源码解析(四)——初始化单实例bean阶段
  19. Linux CentOS环境下.Net Core SDK安装
  20. Redis实战(五)

热门文章

  1. PHP 静态变量的介绍
  2. mysql——单表查询——其它整理示例00
  3. eclipse中svn的使用
  4. mysql(中)
  5. WebGPU学习(八):学习“texturedCube”示例
  6. Android 一共有多少种动画?准确告诉你!
  7. 从零开始学MySQL(一)
  8. 005-监控项item详解,手动创建item实例
  9. SQL的基本操作(三)
  10. Instr()函数用法