#include <cstdlib> #include <cstdio> #include <vector> #include <utility> #include <algorithm> #include <functional> typedef unsigned long long bignum; typedef unsigned long int smallnum; typedef std::pair<bignum, bignum> bigpair; #define DEBUG 0 struct tree_node; tree_node* fix_inconsistency(tree_node* root); tree_node* fix_own_speed_token(tree_node* root); struct tree_node { bignum subtract_token; bignum own_speed_token; bignum speed; bignum loss_value; tree_node* left; tree_node* right; tree_node() : subtract_token(0), own_speed_token(0), left(NULL), right(NULL) {} void add_token(bignum token_value) { loss_value -= token_value; subtract_token += token_value; } void add_own_speed_token(bignum token_quantity) { loss_value -= speed * token_quantity; own_speed_token += token_quantity; } void push_token_down() { if (subtract_token > 0 || own_speed_token > 0) { if (this->left) { this->left->add_token(subtract_token); this->left->add_own_speed_token(own_speed_token); } if (this->right) { this->right->add_token(subtract_token); this->right->add_own_speed_token(own_speed_token); } subtract_token = 0; own_speed_token = 0; } } }; tree_node* insert_node(tree_node* root, tree_node* new_element); tree_node* insert_node_simple(tree_node* root, tree_node* new_element) { if (root == NULL) { return new_element; } if (new_element->speed <= root->speed) { // New element goes below to the left root->left = insert_node(root->left, new_element); } else { // New element goes below to the right root->right = insert_node(root->right, new_element); } return root; } tree_node* rotate_right(tree_node* root) { tree_node* new_root = root->left; tree_node* new_root_right = new_root->right; new_root->right = root; root->left = new_root_right; return new_root; } tree_node* rotate_left(tree_node* root) { tree_node* new_root = root->right; tree_node* new_root_left = new_root->left; new_root->left = root; root->right = new_root_left; return new_root; } tree_node* fix_inconsistency(tree_node* root) { if (root == NULL) {return root;} // Rotate tree to fix single inconsistency if (root->left) { if (root->left->loss_value < root->loss_value) { return rotate_right(root); } } if (root->right) { if (root->right->loss_value <= root->loss_value) { return rotate_left(root); } } return root; } tree_node* fix_inconsistency_left(tree_node* root) { if (root == NULL) {return root;} // Rotate tree to fix single inconsistency if (root->right) { if (root->right->loss_value <= root->loss_value) { return rotate_left(root); } } return root; } tree_node* insert_node(tree_node* root, tree_node* new_element) { tree_node* maybe_inconsistent_root = insert_node_simple(root, new_element); return fix_inconsistency(maybe_inconsistent_root); } bignum fastest_tree_speed(tree_node* root) { if (root->right) { return fastest_tree_speed(root->right); } else { return root->speed; } } tree_node* fix_own_speed_token(tree_node* root) { if (root == NULL) {return root;} root->push_token_down(); root->right = fix_own_speed_token(root->right); return fix_inconsistency_left(root); } tree_node* tree_merge(tree_node* left, tree_node* right) { if (!left) { return right; } if (!right) { return left; } left->push_token_down(); right->push_token_down(); // left = fix_own_speed_token(left); // right = fix_own_speed_token(right); if (left->loss_value < right->loss_value) { // left->right = fix_own_speed_token(left->right); // right = fix_own_speed_token(right); left->right = tree_merge(left->right, right); return left; } else { // right->left = fix_own_speed_token(right->left); // left = fix_own_speed_token(left); right->left = tree_merge(left, right->left); return right; } } tree_node* pop_top_element(tree_node* root) { // POP the element if (root->left) { root->left->add_token(root->speed); // root->right->add_token(root->speed); root->left = fix_own_speed_token(root->left); } if (root->right) { // bignum fastest_speed = fastest_tree_speed(root); root->right->add_own_speed_token(1); root->right = fix_own_speed_token(root->right); } root->push_token_down(); // Both nodes are there, we need to perform tree merge // root->right = fix_own_speed_token(root->right); return tree_merge(root->left, root->right); } void tree_debug_print(tree_node* tree) { int token = rand(); printf("Tree node: <%llu, %llu>(%llu), token: %d\n", tree->speed, tree->loss_value, tree->subtract_token, token); if (tree->left) { printf("Left: %d>>>\n", token); tree_debug_print(tree->left); printf("Left finish: %d<<<\n", token); } else { printf("No left in tree %d\n", token); } if (tree->right) { printf("Right: %d>>>\n", token); tree_debug_print(tree->right); printf("Right finish: %d<<<\n", token); } else { printf("No right in tree %d\n", token); } } int main() { bignum n; scanf("%llu", &n); // FIRST - speed // SECOND - starting position std::vector<bigpair> data(n); for (smallnum i = 0; i < n; i++) { scanf("%llu %llu", &data[i].first, &data[i].second); } std::sort(data.begin(), data.end(), std::greater<bigpair>()); if (DEBUG) { printf("SORTED DATA:\n"); for (smallnum i = 0; i < n; i++) { printf("<%llu, %llu>\n", data[i].first, data[i].second); } } bignum full_sum = 0; for (smallnum i = 0; i < n; i++) { full_sum += data[i].first * (n - i - 1) + data[i].second; } std::vector<bignum> cumulative_sums(n); if (DEBUG) { printf("TOTAL SUM = %lld\n", full_sum); } cumulative_sums[0] = data[0].first; for (smallnum i = 1; i < n; i++) { cumulative_sums[i] = cumulative_sums[i-1] + data[i].first; } std::vector<bignum> loss_values(n); for (smallnum i = 0; i < n; i++) { if (i == 0) { loss_values[i] = data[i].second + data[i].first * (n - i - 1); if (DEBUG) { printf("loss values(%lu) = %llu = %lld + %lld * %lld\n", i, loss_values[i], data[i].second, data[i].first, (n-i-1)); } } else { loss_values[i] = data[i].second + data[i].first * (n - i - 1) + cumulative_sums[i-1]; if (DEBUG) { printf("loss values(%lu) = %llu = %lld + %lld * %lld + %lld\n", i, loss_values[i], data[i].second, data[i].first, (n-i-1), cumulative_sums[i-1]); } } } if (DEBUG) { printf("LOSS FUNCTION DATA:\n"); for (smallnum i = 0; i < n; i++) { printf("<%llu>\n", loss_values[i]); } } std::vector<tree_node> tree_nodes(n); tree_node* tree_root = NULL; for (smallnum i = 0; i < n; i++) { tree_nodes[i].speed = data[i].first; tree_nodes[i].loss_value = loss_values[i]; tree_root = insert_node(tree_root, &(tree_nodes[i])); // if (DEBUG) { // printf("==================================================\n"); // printf("TREE PRINT - insert iteration %lu:\n", i); // tree_debug_print(tree_root); // } } if (DEBUG) { printf("==================================================\n"); printf("TREE PRINT:\n"); tree_debug_print(tree_root); } std::vector<bignum> solution(n); solution[n-1] = full_sum; for (smallnum i = 1; i < n; i++) { bignum selected_loss_value = tree_root->loss_value; full_sum -= selected_loss_value; solution[n-i-1] = full_sum; tree_root = pop_top_element(tree_root); if (DEBUG) { printf("==================================================\n"); printf("TREE PRINT after pop %lu:\n", i); tree_debug_print(tree_root); } } if (DEBUG) { printf("======= SOLUTION =====\n"); } for (smallnum i = 0; i < n; i++) { printf("%llu\n", solution[i]); } 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 | #include <cstdlib> #include <cstdio> #include <vector> #include <utility> #include <algorithm> #include <functional> typedef unsigned long long bignum; typedef unsigned long int smallnum; typedef std::pair<bignum, bignum> bigpair; #define DEBUG 0 struct tree_node; tree_node* fix_inconsistency(tree_node* root); tree_node* fix_own_speed_token(tree_node* root); struct tree_node { bignum subtract_token; bignum own_speed_token; bignum speed; bignum loss_value; tree_node* left; tree_node* right; tree_node() : subtract_token(0), own_speed_token(0), left(NULL), right(NULL) {} void add_token(bignum token_value) { loss_value -= token_value; subtract_token += token_value; } void add_own_speed_token(bignum token_quantity) { loss_value -= speed * token_quantity; own_speed_token += token_quantity; } void push_token_down() { if (subtract_token > 0 || own_speed_token > 0) { if (this->left) { this->left->add_token(subtract_token); this->left->add_own_speed_token(own_speed_token); } if (this->right) { this->right->add_token(subtract_token); this->right->add_own_speed_token(own_speed_token); } subtract_token = 0; own_speed_token = 0; } } }; tree_node* insert_node(tree_node* root, tree_node* new_element); tree_node* insert_node_simple(tree_node* root, tree_node* new_element) { if (root == NULL) { return new_element; } if (new_element->speed <= root->speed) { // New element goes below to the left root->left = insert_node(root->left, new_element); } else { // New element goes below to the right root->right = insert_node(root->right, new_element); } return root; } tree_node* rotate_right(tree_node* root) { tree_node* new_root = root->left; tree_node* new_root_right = new_root->right; new_root->right = root; root->left = new_root_right; return new_root; } tree_node* rotate_left(tree_node* root) { tree_node* new_root = root->right; tree_node* new_root_left = new_root->left; new_root->left = root; root->right = new_root_left; return new_root; } tree_node* fix_inconsistency(tree_node* root) { if (root == NULL) {return root;} // Rotate tree to fix single inconsistency if (root->left) { if (root->left->loss_value < root->loss_value) { return rotate_right(root); } } if (root->right) { if (root->right->loss_value <= root->loss_value) { return rotate_left(root); } } return root; } tree_node* fix_inconsistency_left(tree_node* root) { if (root == NULL) {return root;} // Rotate tree to fix single inconsistency if (root->right) { if (root->right->loss_value <= root->loss_value) { return rotate_left(root); } } return root; } tree_node* insert_node(tree_node* root, tree_node* new_element) { tree_node* maybe_inconsistent_root = insert_node_simple(root, new_element); return fix_inconsistency(maybe_inconsistent_root); } bignum fastest_tree_speed(tree_node* root) { if (root->right) { return fastest_tree_speed(root->right); } else { return root->speed; } } tree_node* fix_own_speed_token(tree_node* root) { if (root == NULL) {return root;} root->push_token_down(); root->right = fix_own_speed_token(root->right); return fix_inconsistency_left(root); } tree_node* tree_merge(tree_node* left, tree_node* right) { if (!left) { return right; } if (!right) { return left; } left->push_token_down(); right->push_token_down(); // left = fix_own_speed_token(left); // right = fix_own_speed_token(right); if (left->loss_value < right->loss_value) { // left->right = fix_own_speed_token(left->right); // right = fix_own_speed_token(right); left->right = tree_merge(left->right, right); return left; } else { // right->left = fix_own_speed_token(right->left); // left = fix_own_speed_token(left); right->left = tree_merge(left, right->left); return right; } } tree_node* pop_top_element(tree_node* root) { // POP the element if (root->left) { root->left->add_token(root->speed); // root->right->add_token(root->speed); root->left = fix_own_speed_token(root->left); } if (root->right) { // bignum fastest_speed = fastest_tree_speed(root); root->right->add_own_speed_token(1); root->right = fix_own_speed_token(root->right); } root->push_token_down(); // Both nodes are there, we need to perform tree merge // root->right = fix_own_speed_token(root->right); return tree_merge(root->left, root->right); } void tree_debug_print(tree_node* tree) { int token = rand(); printf("Tree node: <%llu, %llu>(%llu), token: %d\n", tree->speed, tree->loss_value, tree->subtract_token, token); if (tree->left) { printf("Left: %d>>>\n", token); tree_debug_print(tree->left); printf("Left finish: %d<<<\n", token); } else { printf("No left in tree %d\n", token); } if (tree->right) { printf("Right: %d>>>\n", token); tree_debug_print(tree->right); printf("Right finish: %d<<<\n", token); } else { printf("No right in tree %d\n", token); } } int main() { bignum n; scanf("%llu", &n); // FIRST - speed // SECOND - starting position std::vector<bigpair> data(n); for (smallnum i = 0; i < n; i++) { scanf("%llu %llu", &data[i].first, &data[i].second); } std::sort(data.begin(), data.end(), std::greater<bigpair>()); if (DEBUG) { printf("SORTED DATA:\n"); for (smallnum i = 0; i < n; i++) { printf("<%llu, %llu>\n", data[i].first, data[i].second); } } bignum full_sum = 0; for (smallnum i = 0; i < n; i++) { full_sum += data[i].first * (n - i - 1) + data[i].second; } std::vector<bignum> cumulative_sums(n); if (DEBUG) { printf("TOTAL SUM = %lld\n", full_sum); } cumulative_sums[0] = data[0].first; for (smallnum i = 1; i < n; i++) { cumulative_sums[i] = cumulative_sums[i-1] + data[i].first; } std::vector<bignum> loss_values(n); for (smallnum i = 0; i < n; i++) { if (i == 0) { loss_values[i] = data[i].second + data[i].first * (n - i - 1); if (DEBUG) { printf("loss values(%lu) = %llu = %lld + %lld * %lld\n", i, loss_values[i], data[i].second, data[i].first, (n-i-1)); } } else { loss_values[i] = data[i].second + data[i].first * (n - i - 1) + cumulative_sums[i-1]; if (DEBUG) { printf("loss values(%lu) = %llu = %lld + %lld * %lld + %lld\n", i, loss_values[i], data[i].second, data[i].first, (n-i-1), cumulative_sums[i-1]); } } } if (DEBUG) { printf("LOSS FUNCTION DATA:\n"); for (smallnum i = 0; i < n; i++) { printf("<%llu>\n", loss_values[i]); } } std::vector<tree_node> tree_nodes(n); tree_node* tree_root = NULL; for (smallnum i = 0; i < n; i++) { tree_nodes[i].speed = data[i].first; tree_nodes[i].loss_value = loss_values[i]; tree_root = insert_node(tree_root, &(tree_nodes[i])); // if (DEBUG) { // printf("==================================================\n"); // printf("TREE PRINT - insert iteration %lu:\n", i); // tree_debug_print(tree_root); // } } if (DEBUG) { printf("==================================================\n"); printf("TREE PRINT:\n"); tree_debug_print(tree_root); } std::vector<bignum> solution(n); solution[n-1] = full_sum; for (smallnum i = 1; i < n; i++) { bignum selected_loss_value = tree_root->loss_value; full_sum -= selected_loss_value; solution[n-i-1] = full_sum; tree_root = pop_top_element(tree_root); if (DEBUG) { printf("==================================================\n"); printf("TREE PRINT after pop %lu:\n", i); tree_debug_print(tree_root); } } if (DEBUG) { printf("======= SOLUTION =====\n"); } for (smallnum i = 0; i < n; i++) { printf("%llu\n", solution[i]); } return 0; } |