0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 12:58:48 +00:00

#2086. 「NOI2016」区间

https://loj.ac/s/1584099
This commit is contained in:
Baoshuo Ren 2022-09-22 11:43:50 +08:00
parent b7526c13af
commit 382f3dc4d1
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
41 changed files with 233 additions and 0 deletions

113
LibreOJ/2086/2086.cpp Normal file
View File

@ -0,0 +1,113 @@
#include <iostream>
#include <algorithm>
#include <limits>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 5e5 + 5;
struct node {
int l, r, m, d;
node(int _l = 0, int _r = 0)
: l(_l), r(_r), m(0), d(0) {}
} tr[N << 3];
void pushup(int u) {
tr[u].m = std::max(tr[u << 1].m, tr[u << 1 | 1].m);
}
void pushdown(int u) {
if (!tr[u].d) return;
tr[u << 1].d += tr[u].d;
tr[u << 1 | 1].d += tr[u].d;
tr[u << 1].m += tr[u].d;
tr[u << 1 | 1].m += tr[u].d;
tr[u].d = 0;
}
void build(int u, int l, int r) {
tr[u] = node(l, r);
if (l == r) return;
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
}
void modify(int u, int l, int r, int v) {
if (l <= tr[u].l && tr[u].r <= r) {
tr[u].d += v;
tr[u].m += v;
return;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(u << 1, l, r, v);
if (r > mid) modify(u << 1 | 1, l, r, v);
pushup(u);
}
int n, m, ans = std::numeric_limits<int>::max();
std::pair<int, int> seg[N];
std::vector<int> nums;
int len(int p) {
return nums[seg[p].second - 1] - nums[seg[p].first - 1];
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> seg[i].first >> seg[i].second;
nums.emplace_back(seg[i].first);
nums.emplace_back(seg[i].second);
}
std::sort(seg + 1, seg + 1 + n, [&](const std::pair<int, int> &a, const std::pair<int, int> &b) {
return a.second - a.first < b.second - b.first;
});
std::sort(nums.begin(), nums.end());
nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
for (int i = 1; i <= n; i++) {
seg[i].first = std::lower_bound(nums.begin(), nums.end(), seg[i].first) - nums.begin() + 1;
seg[i].second = std::lower_bound(nums.begin(), nums.end(), seg[i].second) - nums.begin() + 1;
}
build(1, 1, nums.size());
for (int i = 1, l = 1; i <= n; i++) {
modify(1, seg[i].first, seg[i].second, 1);
while (tr[1].m >= m) {
ans = std::min(ans, len(i) - len(l));
modify(1, seg[l].first, seg[l].second, -1);
l++;
}
}
cout << (ans == std::numeric_limits<int>::max() ? -1 : ans) << endl;
return 0;
}

BIN
LibreOJ/2086/data/interval1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval11.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval11.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval12.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval12.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval13.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval13.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval14.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval14.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval15.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval15.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval16.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval16.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval17.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval17.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval18.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval18.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval19.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval19.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval20.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval20.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2086/data/interval9.out (Stored with Git LFS) Normal file

Binary file not shown.