// https://www.geeksforgeeks.org/avl-tree-set-2-deletion/ #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long long TKey; class Node { public: Node *left; Node *right; Node *parent; int height; TKey key; // extra data: int val; int cnt; ll sum; }; class AVLTree { public: Node *root; AVLTree() { root = NULL; } void insert(Node *newNode) { root = insertNode(root, newNode, NULL); } void remove(TKey key) { root = removeNode(root, key, NULL); } Node *find(TKey key) { Node *node = findLE(key); if (!node) return NULL; if (node->key != key) return NULL; return node; } Node *findLE(TKey key) { Node *node = root, *smaller = NULL; while (node) { if (key < node->key) { node = node->left; } else if (key > node->key) { smaller = node; node = node->right; } else { break; } } if (!node) return smaller; return node; } Node *pred(Node *node) { if (node->left != NULL) return getMax(node->left); if (node == root) return NULL; // find path to node Node *parent = root, *path[256]; int len = 0; while (parent != node) { path[len++] = parent; if (node->key < parent->key) parent = parent->left; else parent = parent->right; } // here parent == node while (len > 0) { parent = path[--len]; if (node == parent->right) return parent; node = parent; } return NULL; } Node *succ(Node *node) { if (node->right != NULL) return getMin(node->right); if (node == root) return NULL; // find path to node Node *parent = root, *path[256]; int len = 0; while (parent != node) { path[len++] = parent; if (node->key < parent->key) parent = parent->left; else parent = parent->right; } // here parent == node while (len > 0) { parent = path[--len]; if (node == parent->left) return parent; node = parent; } return NULL; } /* Given a non-empty binary search tree, return the node with minimum key value found in that tree. Note that the entire tree does not need to be searched. */ Node* getMin() { return getMin(root); } Node* getMin(Node* node) { Node* current = node; /* loop down to find the leftmost leaf */ while (current->left != NULL) current = current->left; return current; } Node* getMax() { return getMax(root); } Node* getMax(Node* node) { Node* current = node; /* loop down to find the rightmost leaf */ while (current->right != NULL) current = current->right; return current; } Node* insertNode(Node* node, Node *newNode, Node *parent) { /* 1. Perform the normal BST rotation */ if (node == NULL) { newNode->parent = parent; return newNode; } if (newNode->key < node->key) { node->left = insertNode(node->left, newNode, node); } else if (newNode->key > node->key) { node->right = insertNode(node->right, newNode, node); } else { // Equal keys not allowed return node; } /* 2. Update height of this ancestor node */ node->height = 1 + maxHeight(node->left, node->right); /* 3. Get the balance factor of this ancestor node to check whether this node became unbalanced */ int balance = getBalance(node); // If this node becomes unbalanced, then there are 4 cases: // Left Left Case if (balance > 1 && newNode->key < node->left->key) { childrenChanged(node); return rightRotate(node, parent); } // Right Right Case if (balance < -1 && newNode->key > node->right->key) { childrenChanged(node); return leftRotate(node, parent); } // Left Right Case if (balance > 1 && newNode->key > node->left->key) { node->left = leftRotate(node->left, node); childrenChanged(node); return rightRotate(node, parent); } // Right Left Case if (balance < -1 && newNode->key < node->right->key) { node->right = rightRotate(node->right, node); childrenChanged(node); return leftRotate(node, parent); } childrenChanged(node); /* return the (unchanged) node pointer */ return node; } // Recursive function to delete a node with given key from subtree with given root. It returns root of the modified subtree. Node* removeNode(Node* node, TKey key, Node *parent) { // STEP 1: PERFORM STANDARD BST DELETE if (node == NULL) return NULL; node->parent = parent; if (key < node->key) { // If the key to be deleted is smaller than the node's key, then it lies in left subtree node->left = removeNode(node->left, key, node); } else if (key > node->key) { // If the key to be deleted is greater than the node's key, then it lies in right subtree node->right = removeNode(node->right, key, node); } else { // if key is same as node's key, then this is the node to be deleted // node with only one child or no child if (node->left == NULL || node->right == NULL) { Node *temp = node->left ? node->left : node->right; if (temp == NULL) { // No child case temp = node; node = NULL; } else { // One child case *node = *temp; // Copy the contents of the non-empty child node->parent = parent; } free(temp); } else { // node with two children: Get the inorder successor (smallest in the right subtree) Node* temp = getMin(node->right); // Copy the inorder successor's data to this node copyData(temp, node); // Delete the inorder successor node->right = removeNode(node->right, temp->key, node); } } // If the tree had only one node then return if (node == NULL) return node; // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE node->height = 1 + maxHeight(node->left, node->right); // STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to check whether this node became unbalanced) int balance = getBalance(node); // If this node becomes unbalanced, then there are 4 cases: // Left Left Case if (balance > 1 && getBalance(node->left) >= 0) { childrenChanged(node); return rightRotate(node, parent); } // Left Right Case if (balance > 1 && getBalance(node->left) < 0) { node->left = leftRotate(node->left, node); childrenChanged(node); return rightRotate(node, parent); } // Right Right Case if (balance < -1 && getBalance(node->right) <= 0) { childrenChanged(node); return leftRotate(node, parent); } // Right Left Case if (balance < -1 && getBalance(node->right) > 0) { node->right = rightRotate(node->right, node); childrenChanged(node); return leftRotate(node, parent); } childrenChanged(node); return node; } private: Node *rightRotate(Node *y, Node *parent) { Node *x = y->left; Node *T2 = x->right; // Perform rotation x->right = y; y->parent = x; y->left = T2; if (T2) T2->parent = y; // Update heights y->height = maxHeight(y->left, y->right) + 1; x->height = maxHeight(x->left, x->right) + 1; x->parent = parent; childrenChanged(y); childrenChanged(x); // Return new root return x; } Node *leftRotate(Node *x, Node *parent) { Node *y = x->right; Node *T2 = y->left; // Perform rotation y->left = x; x->parent = y; x->right = T2; if (T2) T2->parent = x; // Update heights x->height = maxHeight(x->left, x->right) + 1; y->height = maxHeight(y->left, y->right) + 1; y->parent = parent; childrenChanged(x); childrenChanged(y); // Return new root return y; } // Get Balance factor of node N int getBalance(Node *N) { if (N == NULL) return 0; return height(N->left) - height(N->right); } int maxHeight(Node *a, Node *b) { int ah = height(a), bh = height(b); if (ah > bh) return ah; return bh; } int height(Node *N) { if (N == NULL) return 0; return N->height; } Node* createNode(TKey key) { Node* node = new Node(); node->key = key; node->left = NULL; node->right = NULL; node->parent = NULL; node->height = 1; // new node is initially added at leaf return node; } public: void copyData(Node *src, Node *dst) { dst->key = src->key; dst->cnt = src->cnt; dst->sum = src->sum; dst->val = src->val; } void childrenChanged(Node *node) { int lcnt = 0; if (node->left) lcnt = node->left->cnt; int rcnt = 0; if (node->right) rcnt = node->right->cnt; node->cnt = lcnt + rcnt + node->val; ll lsum = 0; if (node->left) lsum = node->left->sum; ll rsum = 0; if (node->right) rsum = node->right->sum; node->sum = lsum + rsum + node->key*node->val; } void insert(TKey key, int val) { Node *node = createNode(key); node->val = val; childrenChanged(node); insert(node); } void valueChanged(Node *node) { while (node) { childrenChanged(node); node = node->parent; } } void addVal(TKey key, int val) { Node *node; if (node = find(key)) { node->val += val; valueChanged(node); if (node->val == 0) { remove(key); } } else { insert(key, val); } } int ileRyb(Node *node, ll s) { if (s == node->sum) { return node->cnt; } int w = 0; if (node->right) { if (s >= node->right->sum) { w += node->right->cnt; s -= node->right->sum; } else { return ileRyb(node->right, s); } } if (s <= node->val * node->key) { w += (s + node->key - 1) / node->key; return w; } else { w += node->val; s -= node->val * node->key; } if (node->left) { w += ileRyb(node->left, s); } return w; } }; AVLTree tree; /* int main() { for (int i=0; i<=100; i++) { tree.addVal(i, 1); } for (int i=0; i<=100; i++) { tree.addVal(i, 1); } Node *n = tree.getMin(); while (n) { printf("%d %d\n", n->key, n->val); n = tree.succ(n); } printf("rootcnt=%d rootsum=%d\n\n", tree.root->cnt, tree.root->sum); for (int i=0; i<100; i++) { tree.remove(i); printf("remove %d rootcnt=%d rootsum=%d\n", i, tree.root->cnt, tree.root->sum); } return 0; } */ int atak(ll s, ll k) { int wynik = 0; AVLTree zjedzone; Node *it; while (s < k) { ll mniejsza; //, wieksza; if (!tree.root || tree.root->sum < k-s) { wynik = -1; break; } it = tree.findLE(s); if (it && it->key == s) it = tree.pred(it); int ile = 1; if (!it) { // wszystkie wieksze lub rowne wynik = -1; break; } else if (it == tree.getMax()) { // wszystkie mniejsze mniejsza = it->key; wynik += tree.ileRyb(tree.root, k-s); break; } else { mniejsza = it->key; // wieksza = tree.succ(it)->key; // ile = (wieksza-s+mniejsza)/mniejsza; // if (ile > it->val) ile = it->val; } tree.addVal(mniejsza, -ile); zjedzone.addVal(mniejsza, ile); s += ile*mniejsza; wynik += ile; } if (zjedzone.root) { for (it=zjedzone.getMin(); it != NULL; it = zjedzone.succ(it)) { tree.addVal(it->key, it->val); } } return wynik; } int main() { int n, q; /* ll i, sum; n=100000; for (i=1; i<=n; i++) { tree.addVal(i, 1); sum = ((i+1)*i)/2; if (tree.root->sum != sum) { break; } } printf("i=%d\n", i); //printf("%d %d %d %d %d\n", ryby.begin(), ryby.end(), ryby.lower_bound(-4), ryby.lower_bound(-4)->first, (--ryby.end())->first); return 0; */ scanf("%d", &n); for (int i=0; i<n; i++) { ll w; scanf("%lld", &w); tree.addVal(w, 1); } //printf("sum=%lld cnt=%d key=%lld val=%d\n", tree.root->sum, tree.root->cnt, tree.root->key, tree.root->val); //return 0; scanf("%d", &q); for (int i=0; i<q; i++) { int t; ll s, k, w; scanf("%d", &t); //printf("i=%d\n", i); if (t == 1) { scanf("%lld %lld", &s, &k); printf("%d\n", atak(s, k)); } else if (t == 2) { scanf("%lld", &w); tree.addVal(w, 1); } else { scanf("%lld", &w); tree.addVal(w, -1); } } return 0; }
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | // https://www.geeksforgeeks.org/avl-tree-set-2-deletion/ #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long long TKey; class Node { public: Node *left; Node *right; Node *parent; int height; TKey key; // extra data: int val; int cnt; ll sum; }; class AVLTree { public: Node *root; AVLTree() { root = NULL; } void insert(Node *newNode) { root = insertNode(root, newNode, NULL); } void remove(TKey key) { root = removeNode(root, key, NULL); } Node *find(TKey key) { Node *node = findLE(key); if (!node) return NULL; if (node->key != key) return NULL; return node; } Node *findLE(TKey key) { Node *node = root, *smaller = NULL; while (node) { if (key < node->key) { node = node->left; } else if (key > node->key) { smaller = node; node = node->right; } else { break; } } if (!node) return smaller; return node; } Node *pred(Node *node) { if (node->left != NULL) return getMax(node->left); if (node == root) return NULL; // find path to node Node *parent = root, *path[256]; int len = 0; while (parent != node) { path[len++] = parent; if (node->key < parent->key) parent = parent->left; else parent = parent->right; } // here parent == node while (len > 0) { parent = path[--len]; if (node == parent->right) return parent; node = parent; } return NULL; } Node *succ(Node *node) { if (node->right != NULL) return getMin(node->right); if (node == root) return NULL; // find path to node Node *parent = root, *path[256]; int len = 0; while (parent != node) { path[len++] = parent; if (node->key < parent->key) parent = parent->left; else parent = parent->right; } // here parent == node while (len > 0) { parent = path[--len]; if (node == parent->left) return parent; node = parent; } return NULL; } /* Given a non-empty binary search tree, return the node with minimum key value found in that tree. Note that the entire tree does not need to be searched. */ Node* getMin() { return getMin(root); } Node* getMin(Node* node) { Node* current = node; /* loop down to find the leftmost leaf */ while (current->left != NULL) current = current->left; return current; } Node* getMax() { return getMax(root); } Node* getMax(Node* node) { Node* current = node; /* loop down to find the rightmost leaf */ while (current->right != NULL) current = current->right; return current; } Node* insertNode(Node* node, Node *newNode, Node *parent) { /* 1. Perform the normal BST rotation */ if (node == NULL) { newNode->parent = parent; return newNode; } if (newNode->key < node->key) { node->left = insertNode(node->left, newNode, node); } else if (newNode->key > node->key) { node->right = insertNode(node->right, newNode, node); } else { // Equal keys not allowed return node; } /* 2. Update height of this ancestor node */ node->height = 1 + maxHeight(node->left, node->right); /* 3. Get the balance factor of this ancestor node to check whether this node became unbalanced */ int balance = getBalance(node); // If this node becomes unbalanced, then there are 4 cases: // Left Left Case if (balance > 1 && newNode->key < node->left->key) { childrenChanged(node); return rightRotate(node, parent); } // Right Right Case if (balance < -1 && newNode->key > node->right->key) { childrenChanged(node); return leftRotate(node, parent); } // Left Right Case if (balance > 1 && newNode->key > node->left->key) { node->left = leftRotate(node->left, node); childrenChanged(node); return rightRotate(node, parent); } // Right Left Case if (balance < -1 && newNode->key < node->right->key) { node->right = rightRotate(node->right, node); childrenChanged(node); return leftRotate(node, parent); } childrenChanged(node); /* return the (unchanged) node pointer */ return node; } // Recursive function to delete a node with given key from subtree with given root. It returns root of the modified subtree. Node* removeNode(Node* node, TKey key, Node *parent) { // STEP 1: PERFORM STANDARD BST DELETE if (node == NULL) return NULL; node->parent = parent; if (key < node->key) { // If the key to be deleted is smaller than the node's key, then it lies in left subtree node->left = removeNode(node->left, key, node); } else if (key > node->key) { // If the key to be deleted is greater than the node's key, then it lies in right subtree node->right = removeNode(node->right, key, node); } else { // if key is same as node's key, then this is the node to be deleted // node with only one child or no child if (node->left == NULL || node->right == NULL) { Node *temp = node->left ? node->left : node->right; if (temp == NULL) { // No child case temp = node; node = NULL; } else { // One child case *node = *temp; // Copy the contents of the non-empty child node->parent = parent; } free(temp); } else { // node with two children: Get the inorder successor (smallest in the right subtree) Node* temp = getMin(node->right); // Copy the inorder successor's data to this node copyData(temp, node); // Delete the inorder successor node->right = removeNode(node->right, temp->key, node); } } // If the tree had only one node then return if (node == NULL) return node; // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE node->height = 1 + maxHeight(node->left, node->right); // STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to check whether this node became unbalanced) int balance = getBalance(node); // If this node becomes unbalanced, then there are 4 cases: // Left Left Case if (balance > 1 && getBalance(node->left) >= 0) { childrenChanged(node); return rightRotate(node, parent); } // Left Right Case if (balance > 1 && getBalance(node->left) < 0) { node->left = leftRotate(node->left, node); childrenChanged(node); return rightRotate(node, parent); } // Right Right Case if (balance < -1 && getBalance(node->right) <= 0) { childrenChanged(node); return leftRotate(node, parent); } // Right Left Case if (balance < -1 && getBalance(node->right) > 0) { node->right = rightRotate(node->right, node); childrenChanged(node); return leftRotate(node, parent); } childrenChanged(node); return node; } private: Node *rightRotate(Node *y, Node *parent) { Node *x = y->left; Node *T2 = x->right; // Perform rotation x->right = y; y->parent = x; y->left = T2; if (T2) T2->parent = y; // Update heights y->height = maxHeight(y->left, y->right) + 1; x->height = maxHeight(x->left, x->right) + 1; x->parent = parent; childrenChanged(y); childrenChanged(x); // Return new root return x; } Node *leftRotate(Node *x, Node *parent) { Node *y = x->right; Node *T2 = y->left; // Perform rotation y->left = x; x->parent = y; x->right = T2; if (T2) T2->parent = x; // Update heights x->height = maxHeight(x->left, x->right) + 1; y->height = maxHeight(y->left, y->right) + 1; y->parent = parent; childrenChanged(x); childrenChanged(y); // Return new root return y; } // Get Balance factor of node N int getBalance(Node *N) { if (N == NULL) return 0; return height(N->left) - height(N->right); } int maxHeight(Node *a, Node *b) { int ah = height(a), bh = height(b); if (ah > bh) return ah; return bh; } int height(Node *N) { if (N == NULL) return 0; return N->height; } Node* createNode(TKey key) { Node* node = new Node(); node->key = key; node->left = NULL; node->right = NULL; node->parent = NULL; node->height = 1; // new node is initially added at leaf return node; } public: void copyData(Node *src, Node *dst) { dst->key = src->key; dst->cnt = src->cnt; dst->sum = src->sum; dst->val = src->val; } void childrenChanged(Node *node) { int lcnt = 0; if (node->left) lcnt = node->left->cnt; int rcnt = 0; if (node->right) rcnt = node->right->cnt; node->cnt = lcnt + rcnt + node->val; ll lsum = 0; if (node->left) lsum = node->left->sum; ll rsum = 0; if (node->right) rsum = node->right->sum; node->sum = lsum + rsum + node->key*node->val; } void insert(TKey key, int val) { Node *node = createNode(key); node->val = val; childrenChanged(node); insert(node); } void valueChanged(Node *node) { while (node) { childrenChanged(node); node = node->parent; } } void addVal(TKey key, int val) { Node *node; if (node = find(key)) { node->val += val; valueChanged(node); if (node->val == 0) { remove(key); } } else { insert(key, val); } } int ileRyb(Node *node, ll s) { if (s == node->sum) { return node->cnt; } int w = 0; if (node->right) { if (s >= node->right->sum) { w += node->right->cnt; s -= node->right->sum; } else { return ileRyb(node->right, s); } } if (s <= node->val * node->key) { w += (s + node->key - 1) / node->key; return w; } else { w += node->val; s -= node->val * node->key; } if (node->left) { w += ileRyb(node->left, s); } return w; } }; AVLTree tree; /* int main() { for (int i=0; i<=100; i++) { tree.addVal(i, 1); } for (int i=0; i<=100; i++) { tree.addVal(i, 1); } Node *n = tree.getMin(); while (n) { printf("%d %d\n", n->key, n->val); n = tree.succ(n); } printf("rootcnt=%d rootsum=%d\n\n", tree.root->cnt, tree.root->sum); for (int i=0; i<100; i++) { tree.remove(i); printf("remove %d rootcnt=%d rootsum=%d\n", i, tree.root->cnt, tree.root->sum); } return 0; } */ int atak(ll s, ll k) { int wynik = 0; AVLTree zjedzone; Node *it; while (s < k) { ll mniejsza; //, wieksza; if (!tree.root || tree.root->sum < k-s) { wynik = -1; break; } it = tree.findLE(s); if (it && it->key == s) it = tree.pred(it); int ile = 1; if (!it) { // wszystkie wieksze lub rowne wynik = -1; break; } else if (it == tree.getMax()) { // wszystkie mniejsze mniejsza = it->key; wynik += tree.ileRyb(tree.root, k-s); break; } else { mniejsza = it->key; // wieksza = tree.succ(it)->key; // ile = (wieksza-s+mniejsza)/mniejsza; // if (ile > it->val) ile = it->val; } tree.addVal(mniejsza, -ile); zjedzone.addVal(mniejsza, ile); s += ile*mniejsza; wynik += ile; } if (zjedzone.root) { for (it=zjedzone.getMin(); it != NULL; it = zjedzone.succ(it)) { tree.addVal(it->key, it->val); } } return wynik; } int main() { int n, q; /* ll i, sum; n=100000; for (i=1; i<=n; i++) { tree.addVal(i, 1); sum = ((i+1)*i)/2; if (tree.root->sum != sum) { break; } } printf("i=%d\n", i); //printf("%d %d %d %d %d\n", ryby.begin(), ryby.end(), ryby.lower_bound(-4), ryby.lower_bound(-4)->first, (--ryby.end())->first); return 0; */ scanf("%d", &n); for (int i=0; i<n; i++) { ll w; scanf("%lld", &w); tree.addVal(w, 1); } //printf("sum=%lld cnt=%d key=%lld val=%d\n", tree.root->sum, tree.root->cnt, tree.root->key, tree.root->val); //return 0; scanf("%d", &q); for (int i=0; i<q; i++) { int t; ll s, k, w; scanf("%d", &t); //printf("i=%d\n", i); if (t == 1) { scanf("%lld %lld", &s, &k); printf("%d\n", atak(s, k)); } else if (t == 2) { scanf("%lld", &w); tree.addVal(w, 1); } else { scanf("%lld", &w); tree.addVal(w, -1); } } return 0; } |