#include <cassert> #include <cstdio> #include <cstdlib> #include <set> typedef long long int lli; enum event_type_e { MERGE = 0, WRITE = 1 }; struct event_t { event_type_e type; int argument; lli moment; }; struct expand_block_t { lli anchor, increment, clients; int prev, next; std::set<event_t>::iterator it; }; static const int MAX_EBLOCKS = 200 * 1000 + 1; static const int MAX_OVENS = 200 * 1000 + 1; expand_block_t eblocks[MAX_EBLOCKS]; int oven_times[MAX_OVENS]; lli oven_results[MAX_OVENS]; int n, m; int num_blocks; inline bool operator<(const event_t & a, const event_t & b) { if (a.moment != b.moment) { return a.moment < b.moment; } if (a.type != b.type) { return a.type < b.type; } return a.argument > b.argument; } lli read_data() { scanf("%d %d", &n, &m); eblocks[0] = expand_block_t{ 0, 0, 0, -1, 1 }; // Read clients int cli = 1; lli prev = 0; lli lagspeed = 0; for (int i = 0; i < n; i++) { lli d; scanf("%lld", &d); if (d == prev) { eblocks[cli - 1].increment++; eblocks[cli - 1].clients++; lagspeed += eblocks[cli - 1].increment; } else { eblocks[cli] = expand_block_t{ d, 0, 1, cli - 1, cli + 1, {} }; cli++; prev = d; } } eblocks[cli - 1].next = -1; num_blocks = cli; // Read ovens for (int i = 0; i < m; i++) { scanf("%d", oven_times + i); } return lagspeed; } lli compute_collision_time(const expand_block_t & a, const expand_block_t & b) { assert(a.anchor < b.anchor); const lli speed = a.increment + 1; const lli distance = b.anchor - a.anchor; const lli toi = (distance + speed - 1) / speed; return toi; } std::pair<lli, lli> merge_blocks(expand_block_t & a, const expand_block_t & b, lli toi, std::set<event_t> & events) { assert(a.anchor < b.anchor); const lli speed = a.increment + 1; const lli right_anchor = a.anchor + toi * speed; const lli additional_lag = (right_anchor - b.anchor) * b.clients; const lli additional_lagspeed = b.clients * speed; const int aid = b.prev; const int bid = a.next; a = expand_block_t{ a.anchor, a.increment + b.increment + 1, a.clients + b.clients, a.prev, b.next, {} }; // Add two new events if (a.prev != -1) { eblocks[a.prev].next = aid; assert(eblocks[a.prev].it != events.end()); // printf(" Erasing event between %d and %d\n", a.prev, aid); events.erase(eblocks[a.prev].it); // printf(" Adding event between %d and %d\n", a.prev, aid); eblocks[a.prev].it = events.insert(event_t{ event_type_e::MERGE, a.prev, compute_collision_time(eblocks[a.prev], a), }).first; } if (a.next != -1) { eblocks[a.next].prev = aid; assert(b.it != events.end()); // printf(" Erasing event between %d and %d\n", bid, a.next); events.erase(b.it); // printf(" Adding event between %d and %d\n", aid, a.next); a.it = events.insert(event_t{ event_type_e::MERGE, aid, compute_collision_time(a, eblocks[a.next]), }).first; } // printf(" This will result in additional %lld lag and %lld lagspeed\n", additional_lag, additional_lagspeed); return { additional_lag, additional_lagspeed }; } void work(lli initial_lagspeed) { std::set<event_t> events; // Generate merge events for (int i = 0; i < num_blocks - 1; i++) { event_t evt{ event_type_e::MERGE, i, compute_collision_time(eblocks[i], eblocks[i + 1]), }; eblocks[i].it = events.insert(evt).first; } // Generate write events for (int i = 0; i < m; i++) { event_t evt{ event_type_e::WRITE, i, (lli)oven_times[i], }; events.insert(evt); } lli t = 0; lli lag = 0, lagspeed = initial_lagspeed; int writes = m; // Sweep the broom while (!events.empty()) { // printf("EVENTS:"); // for (const auto & evt : events) { // if (evt.type == event_type_e::MERGE) { // printf(" MERGE(%d and %d)", evt.argument, eblocks[evt.argument].next); // } // else { // printf(" WRITE(%d)", evt.argument); // } // } // puts(""); // Pop the event const auto evt = *events.begin(); events.erase(events.begin()); lag += lagspeed * (evt.moment - t); t = evt.moment; // printf("TIME: %lld\n", t); // printf("BLOCKS:"); // { // int b = 0; // while (b != -1) { // printf(" [%lld %lld]", eblocks[b].anchor - t, eblocks[b].anchor + t * eblocks[b].increment); // b = eblocks[b].next; // } // } // puts(""); if (evt.type == event_type_e::MERGE) { // printf("Merge blocks %d and %d\n", evt.argument, eblocks[evt.argument].next); // printf(" time: %lld\n", evt.moment); const auto p = merge_blocks(eblocks[evt.argument], eblocks[eblocks[evt.argument].next], evt.moment, events); lag += p.first; lagspeed += p.second; } else if (evt.type == event_type_e::WRITE) { // printf("Write value %d\n", evt.argument); oven_results[evt.argument] = lag; writes--; if (writes == 0) { return; // All writes done, no need to merge more } } } } void write_results() { for (int i = 0; i < m; i++) { printf("%lld\n", oven_results[i]); } } int main() { const lli initial_lagspeed = read_data(); work(initial_lagspeed); write_results(); 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 | #include <cassert> #include <cstdio> #include <cstdlib> #include <set> typedef long long int lli; enum event_type_e { MERGE = 0, WRITE = 1 }; struct event_t { event_type_e type; int argument; lli moment; }; struct expand_block_t { lli anchor, increment, clients; int prev, next; std::set<event_t>::iterator it; }; static const int MAX_EBLOCKS = 200 * 1000 + 1; static const int MAX_OVENS = 200 * 1000 + 1; expand_block_t eblocks[MAX_EBLOCKS]; int oven_times[MAX_OVENS]; lli oven_results[MAX_OVENS]; int n, m; int num_blocks; inline bool operator<(const event_t & a, const event_t & b) { if (a.moment != b.moment) { return a.moment < b.moment; } if (a.type != b.type) { return a.type < b.type; } return a.argument > b.argument; } lli read_data() { scanf("%d %d", &n, &m); eblocks[0] = expand_block_t{ 0, 0, 0, -1, 1 }; // Read clients int cli = 1; lli prev = 0; lli lagspeed = 0; for (int i = 0; i < n; i++) { lli d; scanf("%lld", &d); if (d == prev) { eblocks[cli - 1].increment++; eblocks[cli - 1].clients++; lagspeed += eblocks[cli - 1].increment; } else { eblocks[cli] = expand_block_t{ d, 0, 1, cli - 1, cli + 1, {} }; cli++; prev = d; } } eblocks[cli - 1].next = -1; num_blocks = cli; // Read ovens for (int i = 0; i < m; i++) { scanf("%d", oven_times + i); } return lagspeed; } lli compute_collision_time(const expand_block_t & a, const expand_block_t & b) { assert(a.anchor < b.anchor); const lli speed = a.increment + 1; const lli distance = b.anchor - a.anchor; const lli toi = (distance + speed - 1) / speed; return toi; } std::pair<lli, lli> merge_blocks(expand_block_t & a, const expand_block_t & b, lli toi, std::set<event_t> & events) { assert(a.anchor < b.anchor); const lli speed = a.increment + 1; const lli right_anchor = a.anchor + toi * speed; const lli additional_lag = (right_anchor - b.anchor) * b.clients; const lli additional_lagspeed = b.clients * speed; const int aid = b.prev; const int bid = a.next; a = expand_block_t{ a.anchor, a.increment + b.increment + 1, a.clients + b.clients, a.prev, b.next, {} }; // Add two new events if (a.prev != -1) { eblocks[a.prev].next = aid; assert(eblocks[a.prev].it != events.end()); // printf(" Erasing event between %d and %d\n", a.prev, aid); events.erase(eblocks[a.prev].it); // printf(" Adding event between %d and %d\n", a.prev, aid); eblocks[a.prev].it = events.insert(event_t{ event_type_e::MERGE, a.prev, compute_collision_time(eblocks[a.prev], a), }).first; } if (a.next != -1) { eblocks[a.next].prev = aid; assert(b.it != events.end()); // printf(" Erasing event between %d and %d\n", bid, a.next); events.erase(b.it); // printf(" Adding event between %d and %d\n", aid, a.next); a.it = events.insert(event_t{ event_type_e::MERGE, aid, compute_collision_time(a, eblocks[a.next]), }).first; } // printf(" This will result in additional %lld lag and %lld lagspeed\n", additional_lag, additional_lagspeed); return { additional_lag, additional_lagspeed }; } void work(lli initial_lagspeed) { std::set<event_t> events; // Generate merge events for (int i = 0; i < num_blocks - 1; i++) { event_t evt{ event_type_e::MERGE, i, compute_collision_time(eblocks[i], eblocks[i + 1]), }; eblocks[i].it = events.insert(evt).first; } // Generate write events for (int i = 0; i < m; i++) { event_t evt{ event_type_e::WRITE, i, (lli)oven_times[i], }; events.insert(evt); } lli t = 0; lli lag = 0, lagspeed = initial_lagspeed; int writes = m; // Sweep the broom while (!events.empty()) { // printf("EVENTS:"); // for (const auto & evt : events) { // if (evt.type == event_type_e::MERGE) { // printf(" MERGE(%d and %d)", evt.argument, eblocks[evt.argument].next); // } // else { // printf(" WRITE(%d)", evt.argument); // } // } // puts(""); // Pop the event const auto evt = *events.begin(); events.erase(events.begin()); lag += lagspeed * (evt.moment - t); t = evt.moment; // printf("TIME: %lld\n", t); // printf("BLOCKS:"); // { // int b = 0; // while (b != -1) { // printf(" [%lld %lld]", eblocks[b].anchor - t, eblocks[b].anchor + t * eblocks[b].increment); // b = eblocks[b].next; // } // } // puts(""); if (evt.type == event_type_e::MERGE) { // printf("Merge blocks %d and %d\n", evt.argument, eblocks[evt.argument].next); // printf(" time: %lld\n", evt.moment); const auto p = merge_blocks(eblocks[evt.argument], eblocks[eblocks[evt.argument].next], evt.moment, events); lag += p.first; lagspeed += p.second; } else if (evt.type == event_type_e::WRITE) { // printf("Write value %d\n", evt.argument); oven_results[evt.argument] = lag; writes--; if (writes == 0) { return; // All writes done, no need to merge more } } } } void write_results() { for (int i = 0; i < m; i++) { printf("%lld\n", oven_results[i]); } } int main() { const lli initial_lagspeed = read_data(); work(initial_lagspeed); write_results(); return 0; } |