#include <cstdio> #include <cstdlib> #include <cassert> #include <cstdint> using ull = unsigned long long int; static const size_t MAX_N = 300 * 1000; static const ull MOD = 1000 * 1000 * 1000 + 7; static const ull DOUBLE_MOD = 2 * MOD; enum class color : uint8_t { red, black }; struct tree_node { ull key = 0; ull value = 0; ull sum = 0; tree_node* left = nullptr; tree_node* right = nullptr; color col = color::red; }; tree_node pool[MAX_N + 100]; size_t nodes_allocated = 0; tree_node* tree_new_empty() { return nullptr; } tree_node* tree_node_allocate() { return &pool[nodes_allocated++]; } void tree_node_update_sum(tree_node* t) { t->sum = t->value; if (t->left) { t->sum += t->left->sum; } if (t->right) { t->sum += t->right->sum; } t->sum %= MOD; } tree_node* tree_rotate_left(tree_node* t) { tree_node* x = t->right; t->right = x->left; x->col = t->col; t->col = color::red; x->left = t; tree_node_update_sum(x->left); tree_node_update_sum(x); return x; } tree_node* tree_rotate_right(tree_node* t) { tree_node* x = t->left; t->left = x->right; x->col = t->col; t->col = color::red; x->right = t; tree_node_update_sum(x->right); tree_node_update_sum(x); return x; } void tree_flip_colors(tree_node* t) { t->col = (color)(1 - (int)t->col); t->left->col = (color)(1 - (int)t->left->col); t->right->col = (color)(1 - (int)t->right->col); } bool tree_node_is_red(const tree_node* t) { return t ? t->col == color::red : false; } tree_node* tree_fix_up(tree_node* t) { tree_node_update_sum(t); if (!tree_node_is_red(t->left) && tree_node_is_red(t->right)) { t = tree_rotate_left(t); } if (tree_node_is_red(t->left) && tree_node_is_red(t->left->left)) { t = tree_rotate_right(t); } if (tree_node_is_red(t->left) && tree_node_is_red(t->right)) { tree_flip_colors(t); } return t; } tree_node* tree_add_inner(tree_node* t, ull key, ull value) { if (!t) { tree_node* ret = tree_node_allocate(); ret->key = key; ret->value = value; ret->sum = value; return ret; } if (t->key > key) { t->left = tree_add_inner(t->left, key, value); } else if (t->key < key) { t->right = tree_add_inner(t->right, key, value); } else { t->value = (t->value + value) % MOD; tree_node_update_sum(t); return t; } return tree_fix_up(t); } tree_node* tree_add(tree_node* t, ull key, ull value) { tree_node* ret = tree_add_inner(t, key, value); ret->col = color::black; return ret; } ull tree_get_sum(tree_node* t) { return t ? t->sum : 0; } ull tree_get_prefix(tree_node* t, ull before) { if (!t) { return 0; } if (t->key >= before) { return tree_get_prefix(t->left, before); } else { return tree_get_sum(t->left) + t->value + tree_get_prefix(t->right, before); } } void tree_print(tree_node* t, int depth = 0) { if (!t) { return; } tree_print(t->left, depth + 1); for (int i = 0; i < depth; i++) { printf(" "); } printf("key: %llu, value: %llu, sum: %llu\n", t->key, t->value, t->sum); tree_print(t->right, depth + 1); } int tree_consistency_check_inner(tree_node* t) { if (!t) { return 1; } if (t->col == color::red) { assert(!tree_node_is_red(t->left)); } // This is an implementation of 2-3 trees, // so the right node MUST be black assert(!tree_node_is_red(t->right)); const int left_blacks = tree_consistency_check_inner(t->left); const int right_blacks = tree_consistency_check_inner(t->right); assert(left_blacks == right_blacks); return left_blacks + (t->col == color::black); } void tree_consistency_check(tree_node* t) { assert(!tree_node_is_red(t)); tree_consistency_check_inner(t); } void print_nice(tree_node* t, ull shift) { if (!t) { return; } print_nice(t->left, shift); printf(" %llu: %llu\n", (2 * t->key + shift) % MOD, t->value); print_nice(t->right, shift); } int main() { int n; assert(1 == scanf("%d", &n)); tree_node* evens = tree_new_empty(); tree_node* odds = tree_new_empty(); ull shift = 0; auto query_wrapping_range = [] (tree_node* t, ull begin, ull end) -> ull { // printf("Query range %llu %llu\n", begin, end); if (begin < end) { return tree_get_prefix(t, end) - tree_get_prefix(t, begin); } else { // ull a = tree_get_sum(t); // ull b = tree_get_prefix(t, begin); // ull c = tree_get_prefix(t, end); // printf("a: %llu, b: %llu, c: %llu\n", a, b, c); // return a - b + c; return tree_get_sum(t) - tree_get_prefix(t, begin) + tree_get_prefix(t, end); } }; auto count_mopadulos = [&] () { // printf("shift: %llu\n", shift); ull mopadulos = 0; if (shift % 2 == 0) { { const ull begin = (shift / 2) % MOD; const ull end = (begin + (MOD / 2 + 1)) % MOD; mopadulos += query_wrapping_range(evens, begin, end); } { const ull begin = (shift / 2 + MOD / 2) % MOD; const ull end = (begin + (MOD / 2 + 1)) % MOD; mopadulos += query_wrapping_range(odds, begin, end); } } else { { const ull begin = (shift / 2 + 1) % MOD; const ull end = (begin + (MOD / 2 + 1)) % MOD; mopadulos += query_wrapping_range(odds, begin, end); } { const ull begin = (shift / 2 + MOD / 2) % MOD; const ull end = (begin + (MOD / 2 + 1)) % MOD; mopadulos += query_wrapping_range(evens, begin, end); } } // { // const ull begin = ((shift + 1) / 2) % MOD; // const ull end = (begin + (MOD / 2 + 1)) % MOD; // mopadulos += query_wrapping_range((shift%2==0) ? evens : odds, begin, end); // // printf("POOF %llu\n", mopadulos); // } // { // const ull begin = ((shift / 2) + (MOD / 2)) % MOD; // const ull end = (begin + (MOD / 2 + 1)) % MOD; // mopadulos += query_wrapping_range((shift%2==0) ? odds : evens, begin, end); // // printf("POOF %llu\n", mopadulos); // } return mopadulos % MOD; }; // ull sum = 0; for (int i = 0; i < n; i++) { ull x; assert(1 == scanf("%llu", &x)); // puts(""); ull mopadulos = 1; if (i > 0) { mopadulos = count_mopadulos(); shift = (shift + DOUBLE_MOD - x) % DOUBLE_MOD; } // printf("i: %d, x: %llu, mopadulos: %llu\n", i, x, mopadulos); // printf("Evens:\n"); // tree_print(evens, 1); // printf("Odds:\n"); // tree_print(odds, 1); if (mopadulos > 0) { const ull adjusted_x = (shift + x) % DOUBLE_MOD; if (adjusted_x % 2 == 0) { evens = tree_add(evens, adjusted_x/2, mopadulos); // tree_consistency_check(evens); } else { odds = tree_add(odds, adjusted_x/2, mopadulos); // tree_consistency_check(odds); } // sum = (sum + mopadulos) % MOD; // ull calc_sum = (tree_get_sum(evens) + tree_get_sum(odds)) % MOD; // if (calc_sum != sum) { // printf("OH NO! Expected %llu, got %llu, step %d\n", sum, calc_sum, i); // return 1; // } } // printf("Shift2: %llu\n", shift); // printf("Dump:\n"); // print_nice(evens, DOUBLE_MOD - shift); // print_nice(odds, 2 * DOUBLE_MOD - shift + 1); } // puts(""); ull result = count_mopadulos(); // printf("Evens:\n"); // tree_print(evens, 1); // printf("Odds:\n"); // tree_print(odds, 1); printf("%llu\n", result); 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 | #include <cstdio> #include <cstdlib> #include <cassert> #include <cstdint> using ull = unsigned long long int; static const size_t MAX_N = 300 * 1000; static const ull MOD = 1000 * 1000 * 1000 + 7; static const ull DOUBLE_MOD = 2 * MOD; enum class color : uint8_t { red, black }; struct tree_node { ull key = 0; ull value = 0; ull sum = 0; tree_node* left = nullptr; tree_node* right = nullptr; color col = color::red; }; tree_node pool[MAX_N + 100]; size_t nodes_allocated = 0; tree_node* tree_new_empty() { return nullptr; } tree_node* tree_node_allocate() { return &pool[nodes_allocated++]; } void tree_node_update_sum(tree_node* t) { t->sum = t->value; if (t->left) { t->sum += t->left->sum; } if (t->right) { t->sum += t->right->sum; } t->sum %= MOD; } tree_node* tree_rotate_left(tree_node* t) { tree_node* x = t->right; t->right = x->left; x->col = t->col; t->col = color::red; x->left = t; tree_node_update_sum(x->left); tree_node_update_sum(x); return x; } tree_node* tree_rotate_right(tree_node* t) { tree_node* x = t->left; t->left = x->right; x->col = t->col; t->col = color::red; x->right = t; tree_node_update_sum(x->right); tree_node_update_sum(x); return x; } void tree_flip_colors(tree_node* t) { t->col = (color)(1 - (int)t->col); t->left->col = (color)(1 - (int)t->left->col); t->right->col = (color)(1 - (int)t->right->col); } bool tree_node_is_red(const tree_node* t) { return t ? t->col == color::red : false; } tree_node* tree_fix_up(tree_node* t) { tree_node_update_sum(t); if (!tree_node_is_red(t->left) && tree_node_is_red(t->right)) { t = tree_rotate_left(t); } if (tree_node_is_red(t->left) && tree_node_is_red(t->left->left)) { t = tree_rotate_right(t); } if (tree_node_is_red(t->left) && tree_node_is_red(t->right)) { tree_flip_colors(t); } return t; } tree_node* tree_add_inner(tree_node* t, ull key, ull value) { if (!t) { tree_node* ret = tree_node_allocate(); ret->key = key; ret->value = value; ret->sum = value; return ret; } if (t->key > key) { t->left = tree_add_inner(t->left, key, value); } else if (t->key < key) { t->right = tree_add_inner(t->right, key, value); } else { t->value = (t->value + value) % MOD; tree_node_update_sum(t); return t; } return tree_fix_up(t); } tree_node* tree_add(tree_node* t, ull key, ull value) { tree_node* ret = tree_add_inner(t, key, value); ret->col = color::black; return ret; } ull tree_get_sum(tree_node* t) { return t ? t->sum : 0; } ull tree_get_prefix(tree_node* t, ull before) { if (!t) { return 0; } if (t->key >= before) { return tree_get_prefix(t->left, before); } else { return tree_get_sum(t->left) + t->value + tree_get_prefix(t->right, before); } } void tree_print(tree_node* t, int depth = 0) { if (!t) { return; } tree_print(t->left, depth + 1); for (int i = 0; i < depth; i++) { printf(" "); } printf("key: %llu, value: %llu, sum: %llu\n", t->key, t->value, t->sum); tree_print(t->right, depth + 1); } int tree_consistency_check_inner(tree_node* t) { if (!t) { return 1; } if (t->col == color::red) { assert(!tree_node_is_red(t->left)); } // This is an implementation of 2-3 trees, // so the right node MUST be black assert(!tree_node_is_red(t->right)); const int left_blacks = tree_consistency_check_inner(t->left); const int right_blacks = tree_consistency_check_inner(t->right); assert(left_blacks == right_blacks); return left_blacks + (t->col == color::black); } void tree_consistency_check(tree_node* t) { assert(!tree_node_is_red(t)); tree_consistency_check_inner(t); } void print_nice(tree_node* t, ull shift) { if (!t) { return; } print_nice(t->left, shift); printf(" %llu: %llu\n", (2 * t->key + shift) % MOD, t->value); print_nice(t->right, shift); } int main() { int n; assert(1 == scanf("%d", &n)); tree_node* evens = tree_new_empty(); tree_node* odds = tree_new_empty(); ull shift = 0; auto query_wrapping_range = [] (tree_node* t, ull begin, ull end) -> ull { // printf("Query range %llu %llu\n", begin, end); if (begin < end) { return tree_get_prefix(t, end) - tree_get_prefix(t, begin); } else { // ull a = tree_get_sum(t); // ull b = tree_get_prefix(t, begin); // ull c = tree_get_prefix(t, end); // printf("a: %llu, b: %llu, c: %llu\n", a, b, c); // return a - b + c; return tree_get_sum(t) - tree_get_prefix(t, begin) + tree_get_prefix(t, end); } }; auto count_mopadulos = [&] () { // printf("shift: %llu\n", shift); ull mopadulos = 0; if (shift % 2 == 0) { { const ull begin = (shift / 2) % MOD; const ull end = (begin + (MOD / 2 + 1)) % MOD; mopadulos += query_wrapping_range(evens, begin, end); } { const ull begin = (shift / 2 + MOD / 2) % MOD; const ull end = (begin + (MOD / 2 + 1)) % MOD; mopadulos += query_wrapping_range(odds, begin, end); } } else { { const ull begin = (shift / 2 + 1) % MOD; const ull end = (begin + (MOD / 2 + 1)) % MOD; mopadulos += query_wrapping_range(odds, begin, end); } { const ull begin = (shift / 2 + MOD / 2) % MOD; const ull end = (begin + (MOD / 2 + 1)) % MOD; mopadulos += query_wrapping_range(evens, begin, end); } } // { // const ull begin = ((shift + 1) / 2) % MOD; // const ull end = (begin + (MOD / 2 + 1)) % MOD; // mopadulos += query_wrapping_range((shift%2==0) ? evens : odds, begin, end); // // printf("POOF %llu\n", mopadulos); // } // { // const ull begin = ((shift / 2) + (MOD / 2)) % MOD; // const ull end = (begin + (MOD / 2 + 1)) % MOD; // mopadulos += query_wrapping_range((shift%2==0) ? odds : evens, begin, end); // // printf("POOF %llu\n", mopadulos); // } return mopadulos % MOD; }; // ull sum = 0; for (int i = 0; i < n; i++) { ull x; assert(1 == scanf("%llu", &x)); // puts(""); ull mopadulos = 1; if (i > 0) { mopadulos = count_mopadulos(); shift = (shift + DOUBLE_MOD - x) % DOUBLE_MOD; } // printf("i: %d, x: %llu, mopadulos: %llu\n", i, x, mopadulos); // printf("Evens:\n"); // tree_print(evens, 1); // printf("Odds:\n"); // tree_print(odds, 1); if (mopadulos > 0) { const ull adjusted_x = (shift + x) % DOUBLE_MOD; if (adjusted_x % 2 == 0) { evens = tree_add(evens, adjusted_x/2, mopadulos); // tree_consistency_check(evens); } else { odds = tree_add(odds, adjusted_x/2, mopadulos); // tree_consistency_check(odds); } // sum = (sum + mopadulos) % MOD; // ull calc_sum = (tree_get_sum(evens) + tree_get_sum(odds)) % MOD; // if (calc_sum != sum) { // printf("OH NO! Expected %llu, got %llu, step %d\n", sum, calc_sum, i); // return 1; // } } // printf("Shift2: %llu\n", shift); // printf("Dump:\n"); // print_nice(evens, DOUBLE_MOD - shift); // print_nice(odds, 2 * DOUBLE_MOD - shift + 1); } // puts(""); ull result = count_mopadulos(); // printf("Evens:\n"); // tree_print(evens, 1); // printf("Odds:\n"); // tree_print(odds, 1); printf("%llu\n", result); return 0; } |