传送门

Luogu

解题思路

对于选出的区间,我们可以直接用线段树维护区间内单点被覆盖次数最大值。

那么解题重心便落在了选取方式上。

为了让最大值最小,考虑尺取,不能二分,降低效率而且不好写。

先将区间按长度从小到大排序,一个一个选入直到满足单点被覆盖 \(m\) 次的要求,满足不了就直接 break,然后再从尺取区间左端点一个一个删除,在满足题意的前提下使得最大值最小,对答案取 \(\min\)就好了。

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} const int _ = 510000; int n, m, num, XX, X[_ << 1];
struct node{ int l, r, len; }p[_];
inline bool cmp(const node& x, const node& y) { return x.len < y.len; } int mx[_ << 3], tag[_ << 3]; inline int lc(int rt) { return rt << 1; } inline int rc(int rt) { return rt << 1 | 1; } inline void f(int rt, int v) { mx[rt] += v, tag[rt] += v; } inline void pushdown(int rt)
{ if (tag[rt]) f(lc(rt), tag[rt]), f(rc(rt), tag[rt]), tag[rt] = 0; } inline void update(int ql, int qr, int v, int rt = 1, int l = 1, int r = XX) {
if (ql <= l && r <= qr) return f(rt, v);
int mid = (l + r) >> 1;
pushdown(rt);
if (ql <= mid) update(ql, qr, v, lc(rt), l, mid);
if (qr > mid) update(ql, qr, v, rc(rt), mid + 1, r);
mx[rt] = max(mx[lc(rt)], mx[rc(rt)]);
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m);
for (rg int i = 1; i <= n; ++i) {
read(p[i].l), read(p[i].r);
p[i].len = p[i].r - p[i].l;
X[++num] = p[i].l, X[++num] = p[i].r;
}
sort(X + 1, X + num + 1);
XX = unique(X + 1, X + num + 1) - X - 1;
for (rg int i = 1; i <= n; ++i) {
p[i].l = lower_bound(X + 1, X + XX + 1, p[i].l) - X;
p[i].r = lower_bound(X + 1, X + XX + 1, p[i].r) - X;
}
sort(p + 1, p + n + 1, cmp);
int hd = 0, tl = 0, ans = 2147483647;
while (tl < n) {
while (mx[1] < m && tl < n) ++tl, update(p[tl].l, p[tl].r, 1);
if (mx[1] < m) break;
while (mx[1] >= m && hd <= tl) ++hd, update(p[hd].l, p[hd].r, -1);
ans = min(ans, p[tl].len - p[hd].len);
}
if (ans == 2147483647) puts("-1");
else printf("%d\n", ans);
return 0;
}

完结撒花 \(qwq\)

「NOI2016」区间的更多相关文章

  1. 「NOI2016」区间 解题报告

    「NOI2016」区间 最近思维好僵硬啊... 一上来就觉得先把区间拆成两个端点进行差分,然后扫描位置序列,在每个位置维护答案,用数据结构维护当前位置的区间序列,但是不会维护. 于是想研究性质,想到为 ...

  2. LOJ#2086. 「NOI2016」区间

    $n \leq 500000$个区间,从中挑出一些,使得至少有一个点被$m$个选中区间包含,且选中区间长度的极差最小. 区间题死脑筋晚期:把区间按左端点排序,然后右端点用个优先队列来弹,然后需要维护下 ...

  3. 「NOI2016」优秀的拆分 解题报告

    「NOI2016」优秀的拆分 这不是个SAM题,只是个LCP题目 95分的Hash很简单,枚举每个点为开头和末尾的AA串个数,然后乘一下之类的. 考虑怎么快速求"每个点为开头和末尾的AA串个 ...

  4. LibreOJ2085 - 「NOI2016」循环之美

    Portal Description 给出\(n,m(n,m\leq10^9)\)和\(k(k\leq2000)\),求在\(k\)进制下,有多少个数值不同的纯循环小数可以表示成\(\dfrac{x} ...

  5. 「NOI2016」网格 解题报告

    「NOI2016」网格 容易注意到,答案最多为2,也就是说答案为-\(1,0,1,2\)四种,考虑逐个判断. 无解的情况比较简单 如果\(nm\le c+1\),显然无解 如果\(nm=c+2\),判 ...

  6. 「NOI2016」循环之美 解题报告

    「NOI2016」循环之美 对于小数\(\frac{a}{b}\),如果它在\(k\)进制下被统计,需要满足要求并且不重复. 不重复我们确保这个分数是最简分数即\((a,b)=1\) 满足要求需要满足 ...

  7. 「SDOI2005」区间

    「SDOI2005」区间 传送门 记录每一个位置作为左端点和右端点的出现次数,然后直接考虑差分即可. 参考代码: #include <cstdio> #define rg register ...

  8. 「Splay」区间翻转

    传送门:>Here< 解法分析 用splay来维护这个序列. 一直没有搞明白的是,这里的splay的节点究竟维护的是什么?是权值吗?肯定不是,因为区间是会翻转的,如果维护权值的话很快平衡树 ...

  9. LOJ#2083. 「NOI2016」优秀的拆分

    $n \leq 30000$的字符串,问其所有子串的所有AABB形式的拆分有多少种.$t \leq 10$组询问. $n^3$过80,$n^2$过95,鬼去写正解.. $n^2$:先枚举一次算每个位置 ...

随机推荐

  1. 升级openssh编译报错“configure: error: *** working libcrypto not found, check config.log”的解决办法

    问题描述 在linux上,欲将OpenSSH_6.4p1编译升级到OpenSSH_8.0p1时,执行了./configure --prefix=/usr --sysconfdir=/etc/ssh - ...

  2. 零Web知识个性化Blog

    需要使用到的工具 Chrome Pycharm 自定主题的CSS 申请博客的Js权限 设置博客选项 打开Chrome修改查看CSS样式,Windows(F12),MacOS(Command+Optio ...

  3. 使用C#使用Windows的HID通信

    本文转载于:https://blog.csdn.net/u010875635/article/details/73321066 Windows使用HID通信相对比较简单,HID都是通过PID.VID信 ...

  4. js splice()

    //arrayObject.splice(index,howmany,item1,.....,itemX) 语法 //测试代码let array;array = ['George','John','T ...

  5. SpringBean的生命周期以及循环依赖过程

    上面就是springBean的大致生命周期. Bean的创建过程 创建Bean之前会调用Bean工厂的后置处理器,可以获取到BeanDefinition Bean的初始化过程 初始化之前会调用前置处理 ...

  6. koa2第一天 安装koa2

    安装全局koa2:npm install -g koa2 -generator 创建一个koa2文件夹:koa2 -e koa2 进入koa2文件夹:cd koa2 安装npm模块:npm insta ...

  7. 每天进步一点点------Allegro中SYMBOL种类

    Allegro 中SYMBOL 种类在Allegro 中, Symbol 有五种, 它们分别是Package Symbol .Mechanical Symbol.Format Symbol.Shape ...

  8. springmvc、springboot 参数校验

    参数校验在项目中是必不可少的,不仅前端需要校验,为了程序的可靠性,后端也需要对参数进行有效性的校验.下面将介绍在springmvc或springboot项目中参数校验的方法 准备工作: 引入校验需要用 ...

  9. mysql存储表情报错

    数据库版本:mysql  Ver 8.0.16 数据库字符集:utf8 原因:mysql的utf8编码的一个字符最多3个字节,但是一个emoji表情为4个字节,所以utf8不支持存储emoji表情.但 ...

  10. Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version)

    This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In a ...