#include <cstdio> #include <algorithm> #include <cassert> #include <vector> #include <iostream> const int MAX_N = 200000; const int MAX_M = 200000; struct kolejka { long long begin_time = 0; long long end_time = 0; int total_orders = 0; long long waiting_time = 0; void join(kolejka& k2) { //std::cerr << "join (" << begin_time << "-" << end_time << "): " << total_orders << " + (" << k2.begin_time << "-" << k2.end_time << "): " << k2.total_orders << std::endl; k2.update_begin_time(end_time); total_orders += k2.total_orders; end_time = k2.end_time; assert(waiting_time >= 0); long long tmp = waiting_time; waiting_time += k2.waiting_time; if (waiting_time < 0) { std::cerr << "ERROR " << tmp << " " << k2.waiting_time << " " << waiting_time << std::endl; debug("error"); k2.debug("error"); } assert(waiting_time >= 0LL); } void update_begin_time(long long new_begin) { //std::cerr << "update_begin_time " << begin_time << " -> " << new_begin << std::endl; //debug("before"); assert(new_begin > begin_time); long long begin_diff = new_begin - begin_time; waiting_time += total_orders * begin_diff; end_time += begin_diff; begin_time = new_begin; //debug("after"); } void update_speed(int speed_diff) { //std::cerr << "update_speed " << std::endl; //debug("before"); begin_time -= speed_diff; waiting_time += arithmetic_progression(speed_diff, total_orders - 1); end_time += ((total_orders - 1) * speed_diff); //debug("before"); } long long arithmetic_progression(long long speed_diff, long long n) { if (n==0) return 0; long long an = speed_diff + (n-1)*speed_diff; return ((speed_diff + an) * n) / 2; } void debug(const std::string& msg) { // return; std::cerr << "\t " << msg << ": [" << begin_time << "-" << end_time << "): " << total_orders <<" " << waiting_time << std::endl; } }; struct oven { int id; long speed; oven() = default; oven(int i, long t) : id(i), speed(t) { }; }; void print_debug(std::vector<kolejka> kolejki) { return; long long total_waiting = 0; std::cerr << "#kolejki: " << kolejki.size() << std::endl; for (auto && k : kolejki) { std::cerr << "\t[" << k.begin_time << "-" << k.end_time << "): " << k.total_orders <<" " << k.waiting_time << std::endl; total_waiting += k.waiting_time; } std::cerr << "total_waiting " << total_waiting << std::endl; std::cerr << std::endl; } int main() { int n,m; scanf("%d %d", &n, &m); long long t[MAX_N]; for (int i = 0; i < n; ++i) { scanf("%lld", &t[i]); assert(t[i] >= 0); } long d; oven ovens[MAX_M]; for (int i = 0; i < m; ++i) { scanf("%ld", &d); ovens[i] = oven(i, d); assert(d > 0); } auto oven_cmp = [](const oven& a, const oven& b) { return a.speed < b.speed; }; std::sort(ovens, ovens + m, oven_cmp); long long rd[MAX_M] = { 0 }; std::vector<kolejka> kolejki; kolejka k0; k0.begin_time = t[0] - ovens[0].speed; k0.total_orders = 1; k0.end_time = t[0]; k0.waiting_time = 0; if (k0.begin_time < 0) k0.update_begin_time(0); kolejka last = k0; assert(last.waiting_time >= 0); long long sum_waiting = 0; for (int j = 1; j < n; ++j) { kolejka k; k.total_orders = 1; k.begin_time = t[j] - ovens[0].speed; k.end_time = t[j]; k.waiting_time = 0; if (k.begin_time < last.end_time) { last.join(k); } else { sum_waiting += last.waiting_time; kolejki.emplace_back(last); last = k; } } sum_waiting += last.waiting_time; kolejki.emplace_back(last); rd[ovens[0].id] = sum_waiting; //std::cerr << "#kolejek " << kolejki.size() << std::endl << std::endl; print_debug(kolejki); // return 0; for (int i = 1; i < m; ++i) { auto && prev_oven = ovens[i-1]; auto && oven = ovens[i]; long speed_diff = oven.speed - prev_oven.speed; // std::cerr << "oven " << oven.id << " speed_diff " << speed_diff << std::endl; if (speed_diff == 0) { rd[oven.id] = rd[prev_oven.id]; continue; } std::vector<kolejka> nowe_kolejki; nowe_kolejki.reserve(kolejki.size()); kolejka last = kolejki.front(); assert(last.waiting_time >= 0); last.update_speed(speed_diff); assert(last.waiting_time >= 0); if (last.begin_time < 0) last.update_begin_time(0); assert(last.waiting_time >= 0); //std::cerr << "init first OK" << std::endl; long long sum_t = 0; for (int j = 1; j < kolejki.size(); ++j) { kolejka &k = kolejki[j]; k.update_speed(speed_diff); if (k.begin_time < last.end_time) last.join(k); else { nowe_kolejki.emplace_back(last); sum_t += last.waiting_time; last = k; } assert(last.waiting_time >= 0); assert(k.waiting_time >= 0); } nowe_kolejki.emplace_back(last); sum_t += last.waiting_time; rd[oven.id] = sum_t; std::swap(kolejki, nowe_kolejki); // std::cerr << "#kolejek " << kolejki.size() << std::endl << std::endl; print_debug(kolejki); } for (int i = 0; i < m; ++i) printf("%lld\n", rd[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 | #include <cstdio> #include <algorithm> #include <cassert> #include <vector> #include <iostream> const int MAX_N = 200000; const int MAX_M = 200000; struct kolejka { long long begin_time = 0; long long end_time = 0; int total_orders = 0; long long waiting_time = 0; void join(kolejka& k2) { //std::cerr << "join (" << begin_time << "-" << end_time << "): " << total_orders << " + (" << k2.begin_time << "-" << k2.end_time << "): " << k2.total_orders << std::endl; k2.update_begin_time(end_time); total_orders += k2.total_orders; end_time = k2.end_time; assert(waiting_time >= 0); long long tmp = waiting_time; waiting_time += k2.waiting_time; if (waiting_time < 0) { std::cerr << "ERROR " << tmp << " " << k2.waiting_time << " " << waiting_time << std::endl; debug("error"); k2.debug("error"); } assert(waiting_time >= 0LL); } void update_begin_time(long long new_begin) { //std::cerr << "update_begin_time " << begin_time << " -> " << new_begin << std::endl; //debug("before"); assert(new_begin > begin_time); long long begin_diff = new_begin - begin_time; waiting_time += total_orders * begin_diff; end_time += begin_diff; begin_time = new_begin; //debug("after"); } void update_speed(int speed_diff) { //std::cerr << "update_speed " << std::endl; //debug("before"); begin_time -= speed_diff; waiting_time += arithmetic_progression(speed_diff, total_orders - 1); end_time += ((total_orders - 1) * speed_diff); //debug("before"); } long long arithmetic_progression(long long speed_diff, long long n) { if (n==0) return 0; long long an = speed_diff + (n-1)*speed_diff; return ((speed_diff + an) * n) / 2; } void debug(const std::string& msg) { // return; std::cerr << "\t " << msg << ": [" << begin_time << "-" << end_time << "): " << total_orders <<" " << waiting_time << std::endl; } }; struct oven { int id; long speed; oven() = default; oven(int i, long t) : id(i), speed(t) { }; }; void print_debug(std::vector<kolejka> kolejki) { return; long long total_waiting = 0; std::cerr << "#kolejki: " << kolejki.size() << std::endl; for (auto && k : kolejki) { std::cerr << "\t[" << k.begin_time << "-" << k.end_time << "): " << k.total_orders <<" " << k.waiting_time << std::endl; total_waiting += k.waiting_time; } std::cerr << "total_waiting " << total_waiting << std::endl; std::cerr << std::endl; } int main() { int n,m; scanf("%d %d", &n, &m); long long t[MAX_N]; for (int i = 0; i < n; ++i) { scanf("%lld", &t[i]); assert(t[i] >= 0); } long d; oven ovens[MAX_M]; for (int i = 0; i < m; ++i) { scanf("%ld", &d); ovens[i] = oven(i, d); assert(d > 0); } auto oven_cmp = [](const oven& a, const oven& b) { return a.speed < b.speed; }; std::sort(ovens, ovens + m, oven_cmp); long long rd[MAX_M] = { 0 }; std::vector<kolejka> kolejki; kolejka k0; k0.begin_time = t[0] - ovens[0].speed; k0.total_orders = 1; k0.end_time = t[0]; k0.waiting_time = 0; if (k0.begin_time < 0) k0.update_begin_time(0); kolejka last = k0; assert(last.waiting_time >= 0); long long sum_waiting = 0; for (int j = 1; j < n; ++j) { kolejka k; k.total_orders = 1; k.begin_time = t[j] - ovens[0].speed; k.end_time = t[j]; k.waiting_time = 0; if (k.begin_time < last.end_time) { last.join(k); } else { sum_waiting += last.waiting_time; kolejki.emplace_back(last); last = k; } } sum_waiting += last.waiting_time; kolejki.emplace_back(last); rd[ovens[0].id] = sum_waiting; //std::cerr << "#kolejek " << kolejki.size() << std::endl << std::endl; print_debug(kolejki); // return 0; for (int i = 1; i < m; ++i) { auto && prev_oven = ovens[i-1]; auto && oven = ovens[i]; long speed_diff = oven.speed - prev_oven.speed; // std::cerr << "oven " << oven.id << " speed_diff " << speed_diff << std::endl; if (speed_diff == 0) { rd[oven.id] = rd[prev_oven.id]; continue; } std::vector<kolejka> nowe_kolejki; nowe_kolejki.reserve(kolejki.size()); kolejka last = kolejki.front(); assert(last.waiting_time >= 0); last.update_speed(speed_diff); assert(last.waiting_time >= 0); if (last.begin_time < 0) last.update_begin_time(0); assert(last.waiting_time >= 0); //std::cerr << "init first OK" << std::endl; long long sum_t = 0; for (int j = 1; j < kolejki.size(); ++j) { kolejka &k = kolejki[j]; k.update_speed(speed_diff); if (k.begin_time < last.end_time) last.join(k); else { nowe_kolejki.emplace_back(last); sum_t += last.waiting_time; last = k; } assert(last.waiting_time >= 0); assert(k.waiting_time >= 0); } nowe_kolejki.emplace_back(last); sum_t += last.waiting_time; rd[oven.id] = sum_t; std::swap(kolejki, nowe_kolejki); // std::cerr << "#kolejek " << kolejki.size() << std::endl << std::endl; print_debug(kolejki); } for (int i = 0; i < m; ++i) printf("%lld\n", rd[i]); return 0; } |