1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include<bits/stdc++.h>
#define ll long long #define mid (l+r)/2 #define ls id<<1 #define rs id<<1|1 const ll N = 1000005; using namespace std; ll n, m, tmp, uni[N]; struct node { ll l, r; } q[N], bas[N];
bool cmp(node a, node b) { return (a.r - a.l) < (b.r - b.l); }
ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = (s << 1) + (s << 3) + ch - '0'; ch = getchar(); } return s * w; }
ll maxn[N << 2], laz[N << 2];
inline void pushup(ll id) { maxn[id] = max(maxn[ls], maxn[rs]); }
void pushdown(ll id) { if (!laz[id]) return; maxn[ls] += laz[id]; maxn[rs] += laz[id]; laz[ls] += laz[id]; laz[rs] += laz[id]; laz[id] = 0; }
void update(ll id, ll l, ll r, ll x, ll y, ll v) { if(y<l || x>r) return; if (l >= x && r <= y) { maxn[id] += v; laz[id] += v; return; } pushdown(id); if (x <= mid) update(ls, l, mid, x, y, v); if (y > mid) update(rs, mid + 1, r, x, y, v); pushup(id); }
int main() { n = read(), m = read(); for (int i = 1; i <= n; i++) { q[i].l = bas[i].l = read(); q[i].r = bas[i].r = read(); uni[++tmp] = q[i].l; uni[++tmp] = q[i].r; } sort(uni + 1, uni + tmp + 1); tmp = unique(uni + 1, uni + tmp + 1) - uni - 1; sort(q + 1, q + n + 1, cmp); sort(bas + 1, bas + n + 1, cmp); for (int i = 1; i <= n; i++) { q[i].l = lower_bound(uni + 1, uni + tmp + 1, q[i].l) - uni; q[i].r = lower_bound(uni + 1, uni + tmp + 1, q[i].r) - uni; }
ll L = 0, R = 0, ans = 1e18; while (true) { while (maxn[1] < m && R <= n) { R++; update(1, 1, tmp, q[R].l, q[R].r, 1); } if (maxn[1] < m) break; while (maxn[1] >= m && L <= n) { L++; update(1, 1, tmp, q[L].l, q[L].r, -1); } ans = min(ans, (bas[R].r - bas[R].l) - (bas[L].r - bas[L].l)); } if (ans == 1e18) puts("-1"); else printf("%lld\n", ans); return 0; }
|