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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| #include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <set> #include <unordered_set> #include <map>
using namespace std;
#define inf 999999999
class Node { public: Node *left, *right; int l, r; int tag; int min_val; unordered_set<int> S;
explicit Node(int l_, int r_) { left = right = nullptr; l = l_, r = r_; tag = 0; }
void Subtract() { tag++; min_val--; }
void PushDown() { if (tag == 0) return; if (left != nullptr) { left->tag += tag; left->min_val -= tag; } if (right != nullptr) { right->tag += tag; right->min_val -= tag; } tag = 0; }
void Update() { min_val = min(left->min_val, right->min_val); } };
Node* BuildTree(const vector<int>& a, int l, int r) { Node* v = new Node(l, r); if (l == r) { v->min_val = a[l]; return v; } int mid = (l + r) / 2; v->left = BuildTree(a, l, mid); v->right = BuildTree(a, mid + 1, r); v->Update(); return v; }
void Insert(Node* v, int ww, int ee, int x) { if (ww <= v->l && v->r <= ee) { v->S.insert(x); return; } int mid = (v->l + v->r) / 2; if (ww <= mid) Insert(v->left, ww, ee, x); if (ee > mid) Insert(v->right, ww, ee, x); }
void Remove(Node* v, int ww, int ee, int x) { if (ww <= v->l && v->r <= ee) { v->S.erase(x); return; } int mid = (v->l + v->r) / 2; if (ww <= mid) Remove(v->left, ww, ee, x); if (ee > mid) Remove(v->right, ww, ee, x); }
int FindId(Node* v, int pos) { if (v->l <= pos && pos <= v->r && v->S.size() == 1) { return *(v->S.begin()); } int mid = (v->l + v->r) / 2; if (pos <= mid) return FindId(v->left, pos); else return FindId(v->right, pos); }
void Subtract(Node* v, int ww, int ee) { if (ww <= v->l && v->r <= ee) { v->Subtract(); return; } v->PushDown(); int mid = (v->l + v->r) / 2; if (ww <= mid) Subtract(v->left, ww, ee); if (ee > mid) Subtract(v->right, ww, ee); v->Update(); }
int FindMinValuePosition(Node* v) { if (v->l == v->r) { return v->l; } v->PushDown(); if (v->left->min_val == v->min_val) return FindMinValuePosition(v->left); else return FindMinValuePosition(v->right); }
void SetValue(Node* v, int pos, int val) { if (v->l == v->r) { v->min_val = val; return; } v->PushDown(); int mid = (v->l + v->r) / 2; if (pos <= mid) SetValue(v->left, pos, val); else SetValue(v->right, pos, val); v->Update(); }
void ClearTrash(Node* v) { if (v->left) ClearTrash(v->left); if (v->right) ClearTrash(v->right); delete v; }
int Work(int N, int Q, const vector<pair<int, int>>& bookings) { set<int> a; a.insert(1); a.insert(N); for (int i = 0; i < Q; i++) { if (bookings[i].first > 1) a.insert(bookings[i].first - 1); a.insert(bookings[i].first); if (bookings[i].first < N) a.insert(bookings[i].first + 1); if (bookings[i].second > 1) a.insert(bookings[i].second - 1); a.insert(bookings[i].second); if (bookings[i].second < N) a.insert(bookings[i].second + 1); } int M = a.size(); int last = 0, t = 0; vector<int> cover(M + 1); map<int, int> go; for (int x : a) { t++; cover[t] = x - last; last = x; go[x] = t; } vector<int> num(M + 2); vector<pair<int, int>> new_bookings = bookings; for (pair<int, int>& booking : new_bookings) { booking.first = go[booking.first]; booking.second = go[booking.second]; num[booking.first]++; num[booking.second + 1]--; } for (int i = 1; i <= M; i++) { num[i] += num[i - 1]; } for (int i = 1; i <= M; i++) { if (num[i] == 0) { num[i] = inf; } } Node* root = BuildTree(num, 1, M); for (int i = 0; i < Q; i++) { Insert(root, new_bookings[i].first, new_bookings[i].second, i); } vector<int> cnt(Q); for (int i = 1; i <= M; i++) { if (num[i] == 1) { int id = FindId(root, i); cnt[id] += cover[i]; SetValue(root, i, inf); } } set<pair<int, int>> cnt_to_id; for (int i = 0; i < Q; i++) { cnt_to_id.insert(make_pair(cnt[i], i)); } int ans = inf; for (int i = 0; i < Q; i++) { int id = cnt_to_id.rbegin()->second; cnt_to_id.erase(make_pair(cnt[id], id)); ans = min(ans, cnt[id]); Remove(root, new_bookings[id].first, new_bookings[id].second, id); Subtract(root, new_bookings[id].first, new_bookings[id].second); while (root->min_val == 1) { int pos = FindMinValuePosition(root); SetValue(root, pos, inf); int now_id = FindId(root, pos); cnt_to_id.erase(make_pair(cnt[now_id], now_id)); cnt[now_id] += cover[pos]; cnt_to_id.insert(make_pair(cnt[now_id], now_id)); } } ClearTrash(root); return ans; }
int main() { int T; cin >> T; for (int test_id = 1; test_id <= T; test_id++) { int N, Q; cin >> N >> Q; vector<pair<int, int>> bookings(Q); for (int i = 0; i < Q; i++) { cin >> bookings[i].first >> bookings[i].second; } int k = Work(N, Q, bookings); cout << "Case #" << test_id << ": " << k << endl; }
return 0; }
|