#include <bits/stdc++.h> using namespace std; struct Oven { int id; int d; int64_t ans; Oven(): ans(0) { } static bool lower_d(Oven const &lhs, Oven const &rhs) { return lhs.d < rhs.d; } static bool lower_id(Oven const &lhs, Oven const &rhs) { return lhs.id < rhs.id; } }; struct Event { int64_t start; int thres; int impact; Event(int64_t start, int thres, int impact): start(start), thres(thres), impact(impact) { } struct Prioritizer { bool operator()(Event const &lhs, Event const &rhs) const { if(lhs.thres != rhs.thres) return lhs.thres > rhs.thres; return lhs.start < rhs.start; } }; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; map<int64_t, int> counts; priority_queue<Event, vector<Event>, Event::Prioritizer> triggers; int64_t ans = 0; int64_t inc = 0; vector<Oven> ovens(m); counts[0] = 1; for(int i = 0; i < n; i++) { int64_t t; cin >> t; inc += counts[t]; counts[t]++; } for(int j = 0; j < m; j++) { ovens[j].id = j; cin >> ovens[j].d; } sort(ovens.begin(), ovens.end(), Oven::lower_d); map<int64_t, int>::iterator it = counts.begin(), jt; jt = it; jt++; while(jt != counts.end()) { int count = it->second; int thres = (jt->first - it->first + count - 1) / count; int impact = (it->first + count * int64_t(thres) - jt->first) % count; triggers.push(Event(it->first, thres, impact)); it = jt; jt++; } int j = 0; int prev = 0; while(not triggers.empty()) { Event event = triggers.top(); triggers.pop(); while(j < m and ovens[j].d < event.thres) { ans += (ovens[j].d - prev) * inc; prev = ovens[j].d; ovens[j].ans = ans; j++; } ans += (event.thres - prev) * inc; prev = event.thres; map<int64_t, int>::iterator it = counts.find(event.start), jt; if(it == counts.end()) { continue; } jt = it; jt++; int icount = it->second; int jcount = jt->second; ans += event.impact * jcount; inc += icount * int64_t(jcount); it->second += jcount; icount = it->second; counts.erase(jt); jt = it; jt++; if(jt != counts.end()) { int thres = (jt->first - it->first + icount - 1) / icount; int impact = (it->first + icount * int64_t(thres) - jt->first) % icount; triggers.push(Event(it->first, thres, impact)); } } while(j < m) { ans += (ovens[j].d - prev) * inc; prev = ovens[j].d; ovens[j].ans = ans; j++; } sort(ovens.begin(), ovens.end(), Oven::lower_id); for(int j = 0; j < m; j++) { cout << ovens[j].ans << '\n'; } 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 | #include <bits/stdc++.h> using namespace std; struct Oven { int id; int d; int64_t ans; Oven(): ans(0) { } static bool lower_d(Oven const &lhs, Oven const &rhs) { return lhs.d < rhs.d; } static bool lower_id(Oven const &lhs, Oven const &rhs) { return lhs.id < rhs.id; } }; struct Event { int64_t start; int thres; int impact; Event(int64_t start, int thres, int impact): start(start), thres(thres), impact(impact) { } struct Prioritizer { bool operator()(Event const &lhs, Event const &rhs) const { if(lhs.thres != rhs.thres) return lhs.thres > rhs.thres; return lhs.start < rhs.start; } }; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; map<int64_t, int> counts; priority_queue<Event, vector<Event>, Event::Prioritizer> triggers; int64_t ans = 0; int64_t inc = 0; vector<Oven> ovens(m); counts[0] = 1; for(int i = 0; i < n; i++) { int64_t t; cin >> t; inc += counts[t]; counts[t]++; } for(int j = 0; j < m; j++) { ovens[j].id = j; cin >> ovens[j].d; } sort(ovens.begin(), ovens.end(), Oven::lower_d); map<int64_t, int>::iterator it = counts.begin(), jt; jt = it; jt++; while(jt != counts.end()) { int count = it->second; int thres = (jt->first - it->first + count - 1) / count; int impact = (it->first + count * int64_t(thres) - jt->first) % count; triggers.push(Event(it->first, thres, impact)); it = jt; jt++; } int j = 0; int prev = 0; while(not triggers.empty()) { Event event = triggers.top(); triggers.pop(); while(j < m and ovens[j].d < event.thres) { ans += (ovens[j].d - prev) * inc; prev = ovens[j].d; ovens[j].ans = ans; j++; } ans += (event.thres - prev) * inc; prev = event.thres; map<int64_t, int>::iterator it = counts.find(event.start), jt; if(it == counts.end()) { continue; } jt = it; jt++; int icount = it->second; int jcount = jt->second; ans += event.impact * jcount; inc += icount * int64_t(jcount); it->second += jcount; icount = it->second; counts.erase(jt); jt = it; jt++; if(jt != counts.end()) { int thres = (jt->first - it->first + icount - 1) / icount; int impact = (it->first + icount * int64_t(thres) - jt->first) % icount; triggers.push(Event(it->first, thres, impact)); } } while(j < m) { ans += (ovens[j].d - prev) * inc; prev = ovens[j].d; ovens[j].ans = ans; j++; } sort(ovens.begin(), ovens.end(), Oven::lower_id); for(int j = 0; j < m; j++) { cout << ovens[j].ans << '\n'; } return 0; } |