"NOI2016" section

Portal
Luogu

Problem-solving ideas

For the selected range, we can directly use the tree line is covered by a single point within the range of the maximum number of maintenance.
So solving the focus will fall on the selected mode.
In order for a maximum minimum, consider taking ruler, not half, reducing efficiency and good writing.
First section by the length from small to large, until a selected into a single meet point is covered by \ (m \) times the requirements, can not meet directly break, and then taking a one foot section removed from the left point in question is intended to meet the under the premise that the maximum value of the minimum, taking answers \ (\ min \) alone.

Details Notes

  • Gugu Gu

Reference Code

#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;
}

End Sahua \ (qwq \)

Guess you like

Origin www.cnblogs.com/zsbzsb/p/11745947.html