|
| 1 | +#include <iostream> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +#define fastio \ |
| 5 | + ios_base::sync_with_stdio(0); \ |
| 6 | + cin.tie(0) |
| 7 | +#define LL long long |
| 8 | +#define FOR(i, j, k) for (auto i = j; i < k; i++) |
| 9 | + |
| 10 | +struct pt |
| 11 | +{ |
| 12 | + LL l, r; |
| 13 | + pt(LL x, LL y) |
| 14 | + { |
| 15 | + l = x; |
| 16 | + r = y; |
| 17 | + } |
| 18 | + pt() {} |
| 19 | +}; |
| 20 | + |
| 21 | +struct Node |
| 22 | +{ |
| 23 | + pt key; |
| 24 | + Node *left; |
| 25 | + Node *right; |
| 26 | + int height; |
| 27 | +}; |
| 28 | + |
| 29 | +int max(int a, int b); |
| 30 | + |
| 31 | +int height(Node *N) |
| 32 | +{ |
| 33 | + if (N == NULL) |
| 34 | + return 0; |
| 35 | + return N->height; |
| 36 | +} |
| 37 | + |
| 38 | +int max(int a, int b) |
| 39 | +{ |
| 40 | + return (a > b) ? a : b; |
| 41 | +} |
| 42 | + |
| 43 | +Node *newNode(pt key) |
| 44 | +{ |
| 45 | + Node *node = new Node(); |
| 46 | + node->key = key; |
| 47 | + node->left = NULL; |
| 48 | + node->right = NULL; |
| 49 | + node->height = 1; |
| 50 | + |
| 51 | + return (node); |
| 52 | +} |
| 53 | + |
| 54 | +Node *rightRotate(Node *y) |
| 55 | +{ |
| 56 | + Node *x = y->left; |
| 57 | + Node *T2 = x->right; |
| 58 | + |
| 59 | + x->right = y; |
| 60 | + y->left = T2; |
| 61 | + |
| 62 | + y->height = max(height(y->left), |
| 63 | + height(y->right)) + |
| 64 | + 1; |
| 65 | + x->height = max(height(x->left), |
| 66 | + height(x->right)) + |
| 67 | + 1; |
| 68 | + |
| 69 | + return x; |
| 70 | +} |
| 71 | + |
| 72 | +Node *leftRotate(Node *x) |
| 73 | +{ |
| 74 | + Node *y = x->right; |
| 75 | + Node *T2 = y->left; |
| 76 | + |
| 77 | + y->left = x; |
| 78 | + x->right = T2; |
| 79 | + |
| 80 | + x->height = max(height(x->left), |
| 81 | + height(x->right)) + |
| 82 | + 1; |
| 83 | + y->height = max(height(y->left), |
| 84 | + height(y->right)) + |
| 85 | + 1; |
| 86 | + |
| 87 | + return y; |
| 88 | +} |
| 89 | + |
| 90 | +int getBalance(Node *N) |
| 91 | +{ |
| 92 | + if (N == NULL) |
| 93 | + return 0; |
| 94 | + return height(N->left) - |
| 95 | + height(N->right); |
| 96 | +} |
| 97 | + |
| 98 | +Node *insert(Node *node, pt key) |
| 99 | +{ |
| 100 | + if (node == NULL) |
| 101 | + return (newNode(key)); |
| 102 | + |
| 103 | + if (key.r < node->key.l) |
| 104 | + node->left = insert(node->left, key); |
| 105 | + else if (key.l > node->key.r) |
| 106 | + node->right = insert(node->right, key); |
| 107 | + else |
| 108 | + return node; |
| 109 | + |
| 110 | + node->height = 1 + max(height(node->left), |
| 111 | + height(node->right)); |
| 112 | + |
| 113 | + int balance = getBalance(node); |
| 114 | + |
| 115 | + if (balance > 1 && key.r < node->left->key.l) |
| 116 | + return rightRotate(node); |
| 117 | + if (balance < -1 && key.l > node->right->key.r) |
| 118 | + return leftRotate(node); |
| 119 | + if (balance > 1 && key.l > node->left->key.r) |
| 120 | + { |
| 121 | + node->left = leftRotate(node->left); |
| 122 | + return rightRotate(node); |
| 123 | + } |
| 124 | + if (balance < -1 && key.r < node->right->key.l) |
| 125 | + { |
| 126 | + node->right = rightRotate(node->right); |
| 127 | + return leftRotate(node); |
| 128 | + } |
| 129 | + |
| 130 | + return node; |
| 131 | +} |
| 132 | + |
| 133 | +Node *minValueNode(Node *node) |
| 134 | +{ |
| 135 | + Node *current = node; |
| 136 | + while (current->left != NULL) |
| 137 | + current = current->left; |
| 138 | + |
| 139 | + return current; |
| 140 | +} |
| 141 | + |
| 142 | +Node *deleteNode(Node *root, pt key) |
| 143 | +{ |
| 144 | + if (root == NULL) |
| 145 | + return root; |
| 146 | + |
| 147 | + if (key.r < root->key.l) |
| 148 | + root->left = deleteNode(root->left, key); |
| 149 | + else if (key.l > root->key.r) |
| 150 | + root->right = deleteNode(root->right, key); |
| 151 | + else |
| 152 | + { |
| 153 | + if ((root->left == NULL) || |
| 154 | + (root->right == NULL)) |
| 155 | + { |
| 156 | + Node *temp = root->left ? root->left : root->right; |
| 157 | + if (temp == NULL) |
| 158 | + { |
| 159 | + temp = root; |
| 160 | + root = NULL; |
| 161 | + } |
| 162 | + else |
| 163 | + *root = *temp; |
| 164 | + free(temp); |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + Node *temp = minValueNode(root->right); |
| 169 | + root->key = temp->key; |
| 170 | + root->right = deleteNode(root->right, |
| 171 | + temp->key); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if (root == NULL) |
| 176 | + return root; |
| 177 | + |
| 178 | + root->height = 1 + max(height(root->left), |
| 179 | + height(root->right)); |
| 180 | + |
| 181 | + int balance = getBalance(root); |
| 182 | + |
| 183 | + if (balance > 1 && |
| 184 | + getBalance(root->left) >= 0) |
| 185 | + return rightRotate(root); |
| 186 | + |
| 187 | + if (balance > 1 && |
| 188 | + getBalance(root->left) < 0) |
| 189 | + { |
| 190 | + root->left = leftRotate(root->left); |
| 191 | + return rightRotate(root); |
| 192 | + } |
| 193 | + |
| 194 | + if (balance < -1 && |
| 195 | + getBalance(root->right) <= 0) |
| 196 | + return leftRotate(root); |
| 197 | + |
| 198 | + if (balance < -1 && |
| 199 | + getBalance(root->right) > 0) |
| 200 | + { |
| 201 | + root->right = rightRotate(root->right); |
| 202 | + return leftRotate(root); |
| 203 | + } |
| 204 | + |
| 205 | + return root; |
| 206 | +} |
| 207 | + |
| 208 | +void search(Node *root, pt &key, LL &d, LL x) |
| 209 | +{ |
| 210 | + if (root == NULL) |
| 211 | + return; |
| 212 | + if (root->key.l <= x && root->key.r >= x) |
| 213 | + { |
| 214 | + d = 0; |
| 215 | + key.l = root->key.l; |
| 216 | + key.r = root->key.r; |
| 217 | + } |
| 218 | + else if (root->key.l > x) |
| 219 | + { |
| 220 | + if (root->key.l - x < d || (root->key.l - x == d && key.l > root->key.r)) |
| 221 | + { |
| 222 | + d = root->key.l - x; |
| 223 | + key.l = root->key.l; |
| 224 | + key.r = root->key.r; |
| 225 | + } |
| 226 | + search(root->left, key, d, x); |
| 227 | + } |
| 228 | + else |
| 229 | + { |
| 230 | + if (x - root->key.r < d || (x - root->key.r == d && key.l > root->key.r)) |
| 231 | + { |
| 232 | + d = x - root->key.r; |
| 233 | + key.l = root->key.l; |
| 234 | + key.r = root->key.r; |
| 235 | + } |
| 236 | + search(root->right, key, d, x); |
| 237 | + } |
| 238 | +} |
| 239 | +void merge(LL arr[], int p, int q, int r) |
| 240 | +{ |
| 241 | + |
| 242 | + int n1 = q - p + 1; |
| 243 | + int n2 = r - q; |
| 244 | + |
| 245 | + LL L[n1], M[n2]; |
| 246 | + |
| 247 | + for (int i = 0; i < n1; i++) |
| 248 | + L[i] = arr[p + i]; |
| 249 | + for (int j = 0; j < n2; j++) |
| 250 | + M[j] = arr[q + 1 + j]; |
| 251 | + |
| 252 | + int i, j, k; |
| 253 | + i = 0; |
| 254 | + j = 0; |
| 255 | + k = p; |
| 256 | + |
| 257 | + while (i < n1 && j < n2) |
| 258 | + { |
| 259 | + if (L[i] >= M[j]) |
| 260 | + { |
| 261 | + arr[k] = L[i]; |
| 262 | + i++; |
| 263 | + } |
| 264 | + else |
| 265 | + { |
| 266 | + arr[k] = M[j]; |
| 267 | + j++; |
| 268 | + } |
| 269 | + k++; |
| 270 | + } |
| 271 | + |
| 272 | + while (i < n1) |
| 273 | + { |
| 274 | + arr[k] = L[i]; |
| 275 | + i++; |
| 276 | + k++; |
| 277 | + } |
| 278 | + |
| 279 | + while (j < n2) |
| 280 | + { |
| 281 | + arr[k] = M[j]; |
| 282 | + j++; |
| 283 | + k++; |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +void mergeSort(LL arr[], int l, int r) |
| 288 | +{ |
| 289 | + if (l < r) |
| 290 | + { |
| 291 | + int m = l + (r - l) / 2; |
| 292 | + mergeSort(arr, l, m); |
| 293 | + mergeSort(arr, m + 1, r); |
| 294 | + merge(arr, l, m, r); |
| 295 | + } |
| 296 | +} |
| 297 | +int main() |
| 298 | +{ |
| 299 | + fastio; |
| 300 | + int t = 1; |
| 301 | + cin >> t; |
| 302 | + int T = 1; |
| 303 | + // auto start = high_resolution_clock::now(); |
| 304 | + while (t--) |
| 305 | + { |
| 306 | + Node *root = NULL; |
| 307 | + int n, m; |
| 308 | + cin >> n >> m; |
| 309 | + pt v[n]; |
| 310 | + LL vv[m]; |
| 311 | + LL ans[m]; |
| 312 | + FOR(i, 0, n) |
| 313 | + { |
| 314 | + cin >> v[i].l >> v[i].r; |
| 315 | + } |
| 316 | + FOR(i, 0, m) |
| 317 | + cin >> vv[i]; |
| 318 | + FOR(i, 0, n) |
| 319 | + { |
| 320 | + root = insert(root, pt(v[i].l, v[i].r)); |
| 321 | + } |
| 322 | + mergeSort(vv, 0, m - 1); |
| 323 | + |
| 324 | + FOR(i, 0, m) |
| 325 | + { |
| 326 | + LL x = vv[i]; |
| 327 | + LL d = 1e18; |
| 328 | + pt p = pt(INT_MAX, INT_MAX); |
| 329 | + search(root, p, d, x); |
| 330 | + root = deleteNode(root, pt(p.l, p.r)); |
| 331 | + LL k = x - d >= p.l && x - d <= p.r ? x - d : x + d; |
| 332 | + |
| 333 | + ans[i] = k; |
| 334 | + if (p.l == p.r) |
| 335 | + continue; |
| 336 | + if (p.l == k) |
| 337 | + root = insert(root, pt(k + 1, p.r)); |
| 338 | + else if (p.r == k) |
| 339 | + root = insert(root, pt(p.l, k - 1)); |
| 340 | + else |
| 341 | + { |
| 342 | + root = insert(root, pt(p.l, k - 1)); |
| 343 | + root = insert(root, pt(k + 1, p.r)); |
| 344 | + } |
| 345 | + } |
| 346 | + printf("Case #%d: ", T++); |
| 347 | + for (int i = 0; i < m; i++) |
| 348 | + printf("%lld ", ans[i]); |
| 349 | + printf("\n"); |
| 350 | + } |
| 351 | +} |
0 commit comments