diff --git a/BZOJ/4653/4653.cpp b/BZOJ/4653/4653.cpp new file mode 100644 index 00000000..3f445a22 --- /dev/null +++ b/BZOJ/4653/4653.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include + +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::max(); +std::pair seg[N]; +std::vector 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 &a, const std::pair &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::max() ? -1 : ans) << endl; + + return 0; +} diff --git a/BZOJ/4653/data/1.in b/BZOJ/4653/data/1.in new file mode 100644 index 00000000..28e290b8 --- /dev/null +++ b/BZOJ/4653/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3907909fed3521ee265fb8efee48a2bc12d8e7bf8da1dd8861d848bea566eb31 +size 353344 diff --git a/BZOJ/4653/data/1.out b/BZOJ/4653/data/1.out new file mode 100644 index 00000000..5b25d675 --- /dev/null +++ b/BZOJ/4653/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:febbdaafb6d29651d77823972817af448745b5e81e95891e5baff97e5f90b5be +size 5 diff --git a/BZOJ/4653/data/10.in b/BZOJ/4653/data/10.in new file mode 100644 index 00000000..0bcaf5f1 --- /dev/null +++ b/BZOJ/4653/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1efe2f8d5965fd7ee8540f5bca7b4d24e85f7450ab0df19ae57c07219999eacb +size 1904 diff --git a/BZOJ/4653/data/10.out b/BZOJ/4653/data/10.out new file mode 100644 index 00000000..c7a5de9f --- /dev/null +++ b/BZOJ/4653/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2d3815df335ba0b06ed82740397587d3a77840ebf2993067c0698bca55dc999c +size 5 diff --git a/BZOJ/4653/data/11.in b/BZOJ/4653/data/11.in new file mode 100644 index 00000000..66c394e0 --- /dev/null +++ b/BZOJ/4653/data/11.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7a6431e48218f21286a91846cdc530bfb7abaacf8879854aaff006800303b53f +size 11784 diff --git a/BZOJ/4653/data/11.out b/BZOJ/4653/data/11.out new file mode 100644 index 00000000..804fcc90 --- /dev/null +++ b/BZOJ/4653/data/11.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa +size 2 diff --git a/BZOJ/4653/data/12.in b/BZOJ/4653/data/12.in new file mode 100644 index 00000000..d4364ce4 --- /dev/null +++ b/BZOJ/4653/data/12.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:de6846f8d178d2276ab1cb43ae482b6f560be0d5317f56ef323eba4de73007fc +size 5933906 diff --git a/BZOJ/4653/data/12.out b/BZOJ/4653/data/12.out new file mode 100644 index 00000000..a438c2ca --- /dev/null +++ b/BZOJ/4653/data/12.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4ba0a139b0c3bd569a4ae82dbe7cb7467789001ed9e99dfba4283fd0ca9774b5 +size 10 diff --git a/BZOJ/4653/data/13.in b/BZOJ/4653/data/13.in new file mode 100644 index 00000000..d4b92ac2 --- /dev/null +++ b/BZOJ/4653/data/13.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ec7da5083aa2d4d95b7d7ced0ad3639a1ae4174af3ba058738e3a8765f5fbdf8 +size 18903 diff --git a/BZOJ/4653/data/13.out b/BZOJ/4653/data/13.out new file mode 100644 index 00000000..311c4506 --- /dev/null +++ b/BZOJ/4653/data/13.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ee3aa64bb94a50845d5024cd4bd20202a4567aed5cd5328c0d97e9920775fc28 +size 3 diff --git a/BZOJ/4653/data/14.in b/BZOJ/4653/data/14.in new file mode 100644 index 00000000..0ab71c53 --- /dev/null +++ b/BZOJ/4653/data/14.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:53d1bcbe3b1271f112babd1aca1634ea8a927e0391dbeb3ef99e1ead791cb8e3 +size 19134 diff --git a/BZOJ/4653/data/14.out b/BZOJ/4653/data/14.out new file mode 100644 index 00000000..8194d5ec --- /dev/null +++ b/BZOJ/4653/data/14.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:64ea044adf4474fe225ef5c7c147f27175d20a6ba35ba9e66c9dae4d554b0ed0 +size 5 diff --git a/BZOJ/4653/data/15.in b/BZOJ/4653/data/15.in new file mode 100644 index 00000000..9a2fbedf --- /dev/null +++ b/BZOJ/4653/data/15.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f87fd8af6fb5bb8a34fbfa6ab12b32ad831d78d1fd3a1f3f60e6901a5d624e8c +size 9888503 diff --git a/BZOJ/4653/data/15.out b/BZOJ/4653/data/15.out new file mode 100644 index 00000000..43f289d1 --- /dev/null +++ b/BZOJ/4653/data/15.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:279b4b4983e01e07a7bc7c170e6610c3549cc240042f304b92e4506db2e4012e +size 10 diff --git a/BZOJ/4653/data/16.in b/BZOJ/4653/data/16.in new file mode 100644 index 00000000..3597240c --- /dev/null +++ b/BZOJ/4653/data/16.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:40dc7bef29554790698a1542684c78b287d666f6e4ac0b5de0b24d87e3fde882 +size 589084 diff --git a/BZOJ/4653/data/16.out b/BZOJ/4653/data/16.out new file mode 100644 index 00000000..de6bdaff --- /dev/null +++ b/BZOJ/4653/data/16.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c1c658d533be288bb69fbd00aff96cce3cb963a81c394a160e714ffc12c78ea1 +size 6 diff --git a/BZOJ/4653/data/17.in b/BZOJ/4653/data/17.in new file mode 100644 index 00000000..88b9262f --- /dev/null +++ b/BZOJ/4653/data/17.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2f8aa124ddd0af95eccc217cc6bf88a04589ff6e0dafe4c2cb0d2f7de5bd5149 +size 3958 diff --git a/BZOJ/4653/data/17.out b/BZOJ/4653/data/17.out new file mode 100644 index 00000000..9948b1bb --- /dev/null +++ b/BZOJ/4653/data/17.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9baafc0f6b7536204c183b899bdcb189018efe193125be610e4da1fed152321f +size 10 diff --git a/BZOJ/4653/data/18.in b/BZOJ/4653/data/18.in new file mode 100644 index 00000000..97b8ba3c --- /dev/null +++ b/BZOJ/4653/data/18.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2bda3440b9e76ff6898a2dd3a9354bdf588e0c932a634aa0bf5a9c187970e075 +size 7911447 diff --git a/BZOJ/4653/data/18.out b/BZOJ/4653/data/18.out new file mode 100644 index 00000000..5bdef46d --- /dev/null +++ b/BZOJ/4653/data/18.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7947b0b659fbd177fa6e8a76d544154e458f31602e6cbbb723b849ed6b8fe81f +size 10 diff --git a/BZOJ/4653/data/19.in b/BZOJ/4653/data/19.in new file mode 100644 index 00000000..4f4d5ad8 --- /dev/null +++ b/BZOJ/4653/data/19.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:878790c059541f16f152817b7951d27a29cc34a6bc63143f3cc364d0f46ccdde +size 19106 diff --git a/BZOJ/4653/data/19.out b/BZOJ/4653/data/19.out new file mode 100644 index 00000000..c7605a39 --- /dev/null +++ b/BZOJ/4653/data/19.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ab6557812843af78e1517a997d9fd98e61cd0d61f1dfd9059789cef3f2cd9eda +size 5 diff --git a/BZOJ/4653/data/2.in b/BZOJ/4653/data/2.in new file mode 100644 index 00000000..31f694be --- /dev/null +++ b/BZOJ/4653/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:581157ee6b28c61ca970f9b19dd3e3aed588b93c821d6925324d897ea40435dc +size 1928 diff --git a/BZOJ/4653/data/2.out b/BZOJ/4653/data/2.out new file mode 100644 index 00000000..dc2ec621 --- /dev/null +++ b/BZOJ/4653/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5510127ed7c60d7d78b4a8094ef56f841b7ef9e1c0b08e1516f4754c25fcaa16 +size 5 diff --git a/BZOJ/4653/data/20.in b/BZOJ/4653/data/20.in new file mode 100644 index 00000000..8f006b91 --- /dev/null +++ b/BZOJ/4653/data/20.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:238e34814adc368c0c59e5062ac3c4898dcb394870a3fe766cfcd3304532b2c4 +size 123 diff --git a/BZOJ/4653/data/20.out b/BZOJ/4653/data/20.out new file mode 100644 index 00000000..88c40b81 --- /dev/null +++ b/BZOJ/4653/data/20.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e3667f7d8c030260bf49046f955ec9bebdb9a4cb8a66b812fd498ded5431a821 +size 3 diff --git a/BZOJ/4653/data/3.in b/BZOJ/4653/data/3.in new file mode 100644 index 00000000..f8e26cfe --- /dev/null +++ b/BZOJ/4653/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7b327bc46c37729a9f3b0bd8440e9df116fafd2c275aecafff287e609e890ba7 +size 39576 diff --git a/BZOJ/4653/data/3.out b/BZOJ/4653/data/3.out new file mode 100644 index 00000000..953ba56e --- /dev/null +++ b/BZOJ/4653/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e2d6ba65625f9131cdc2c03c9ed62be06b8100004486608405079aff04d393d +size 10 diff --git a/BZOJ/4653/data/4.in b/BZOJ/4653/data/4.in new file mode 100644 index 00000000..419de35f --- /dev/null +++ b/BZOJ/4653/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:425bafc740836f317d0cb101ffafb53a72c575f166efe7be911eece70c28d822 +size 2355 diff --git a/BZOJ/4653/data/4.out b/BZOJ/4653/data/4.out new file mode 100644 index 00000000..c13e25ee --- /dev/null +++ b/BZOJ/4653/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:816c83d0ac08a94e91547a30df27dbbe88c7643cfa3c99d33120fd361173759a +size 3 diff --git a/BZOJ/4653/data/5.in b/BZOJ/4653/data/5.in new file mode 100644 index 00000000..e995eceb --- /dev/null +++ b/BZOJ/4653/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b2496c86055a98e8b2ad2df06826e35137a371911d7efb7d5afa2e7d1568ba3e +size 2371 diff --git a/BZOJ/4653/data/5.out b/BZOJ/4653/data/5.out new file mode 100644 index 00000000..81d80beb --- /dev/null +++ b/BZOJ/4653/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cbd91cdba3348178fa87e56abd4e1d96506db346eb2288be4284b9014a52fd94 +size 4 diff --git a/BZOJ/4653/data/6.in b/BZOJ/4653/data/6.in new file mode 100644 index 00000000..f6f4e77e --- /dev/null +++ b/BZOJ/4653/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3814f898c61079433536de6fa474e4dcf1a2c2fdd6b527a904ddb2f47c8e3a41 +size 471134 diff --git a/BZOJ/4653/data/6.out b/BZOJ/4653/data/6.out new file mode 100644 index 00000000..253c951e --- /dev/null +++ b/BZOJ/4653/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2a27177d589962e5fcdcb9b9e648a99ea6525d403cc1feca74a4dd2f1f22e2a2 +size 5 diff --git a/BZOJ/4653/data/7.in b/BZOJ/4653/data/7.in new file mode 100644 index 00000000..805e9297 --- /dev/null +++ b/BZOJ/4653/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:97105375746e32148e1ed5444b3023e1917fa2cfde7e2261521c599052fc2d02 +size 1178133 diff --git a/BZOJ/4653/data/7.out b/BZOJ/4653/data/7.out new file mode 100644 index 00000000..1134cfee --- /dev/null +++ b/BZOJ/4653/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3723c256e87505fc1174cca1f71bc8145d8703d46ae88c88537df9af72e2ab0b +size 6 diff --git a/BZOJ/4653/data/8.in b/BZOJ/4653/data/8.in new file mode 100644 index 00000000..d5f0643e --- /dev/null +++ b/BZOJ/4653/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c6423a20374e7f9be2df019f7d00b514053baa7245eb0d346566dc68d279d28f +size 121 diff --git a/BZOJ/4653/data/8.out b/BZOJ/4653/data/8.out new file mode 100644 index 00000000..f553c3a4 --- /dev/null +++ b/BZOJ/4653/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4c82a221b575ce7fe118b2e8cdf0764bf4ef570a3017e80b6d3438af9095f376 +size 3 diff --git a/BZOJ/4653/data/9.in b/BZOJ/4653/data/9.in new file mode 100644 index 00000000..beb10be4 --- /dev/null +++ b/BZOJ/4653/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4a68bab7b7e5bfc839c103cb3fe0d74ce73338819a7ed8884cf2d7213b27ce4d +size 3955190 diff --git a/BZOJ/4653/data/9.out b/BZOJ/4653/data/9.out new file mode 100644 index 00000000..86bbf5e3 --- /dev/null +++ b/BZOJ/4653/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:41b49f5ae1a877524e539ba17f94ff03be10697c4929cb8975207fee71385562 +size 10