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 96 97 98 99 100 101 102 103
| #include <iostream> #include <algorithm> using namespace std; const int N = 505; struct clover { int x, y; } a[N]; struct event { int x, yl, yr, val; bool operator<(const event &rhs) const { return x < rhs.x; } } e[N * 2]; int y[N * 2]; struct node { int max, lazy; } tree[1 << 11];
void build(int id, int l, int r) { tree[id].max = tree[id].lazy = 0; if (l < r) { int mid = (l + r) / 2; build(id * 2, l, mid); build(id * 2 + 1, mid + 1, r); } } inline void pushdown(int id, int l, int r) { if (tree[id].lazy && l < r) { tree[id * 2].max += tree[id].lazy; tree[id * 2].lazy += tree[id].lazy; tree[id * 2 + 1].max += tree[id].lazy; tree[id * 2 + 1].lazy += tree[id].lazy; tree[id].lazy = 0; } } void update(int id, int l, int r, int L, int R, int val) { if (L <= l && R >= r) { tree[id].lazy += val; tree[id].max += val; } else { pushdown(id, l, r); int mid = (l + r) / 2; if (L <= mid) update(id * 2, l, mid, L, R, val); if (R > mid) update(id * 2 + 1, mid + 1, r, L, R, val); tree[id].max = max(tree[id * 2].max, tree[id * 2 + 1].max); } } int main() { ios::sync_with_stdio(false); int c, n; cin >> c >> n; for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y; int l = 1, r = 1e4; while (l < r) { int mid = (l + r) / 2, en = 0, yn = 0; for (int i = 1; i <= n; i++) { e[++en].x = a[i].x - mid + 1; y[++yn] = e[en].yl = a[i].y - mid + 1; y[++yn] = e[en].yr = a[i].y; e[en].val = 1; e[++en].x = a[i].x + 1; e[en].yl = a[i].y - mid + 1; e[en].yr = a[i].y; e[en].val = -1; } sort(e + 1, e + en + 1); sort(y + 1, y + yn + 1); build(1, 1, yn); int now = 0; for (int i = 1; i <= en; i++) { if (e[i].x != e[i - 1].x) now = max(now, tree[1].max); update(1, 1, yn, lower_bound(y + 1, y + yn + 1, e[i].yl) - y, lower_bound(y + 1, y + yn + 1, e[i].yr) - y, e[i].val); } if (now < c) l = mid + 1; else r = mid; } cout << r << endl; return 0; }
|