#include "bits/stdc++.h"
using namespace std;
using Int = unsigned long long;
using Event = tuple<int, int, int>;
template<class T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;
struct Ratio {
Ratio(Int num_=0ll, int den_=1):num(num_), den(den_){}
Int num;
int den;
double ToDouble() const {return (double) num / den;}
};
bool operator<(const Ratio& a, const Ratio& b) {
return a.num * b.den < b.num * a.den;
}
int main() {
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<Int> ts(n + 1);
ts[0] = 0;
for (int i = 1; i <= n; ++i) {
cin >> ts[i];
}
vector<int> ds(m);
for (int j = 0; j < m; ++j) {
cin >> ds[j];
}
set<int> S;
for (int i = 0; i <= n; ++i) {
if (i == 0 || ts[i-1] < ts[i]) {
S.insert(i);
}
}
MinPriorityQueue<pair<Ratio, Event>> E;
for (auto it = S.begin(); it != S.end() && next(it) != S.end(); ++it) {
int i1 = *it;
int i2 = *next(it);
// ts[i1] + (i2 - i1) * d >= ts[i2]
// d >= (ts[i2] - ts[i1]) / (i2 - i1)
E.emplace(Ratio(ts[i2] - ts[i1], i2-i1), Event(-1,i1,i2));
}
for (int j = 0; j < m; ++j) {
Event e;
get<0>(e) = j;
E.emplace(ds[j], e);
}
Int W0 = 0;
Int W1 = 0;
for (auto it = ts.begin(); it != ts.end(); ++it) {
W1 += it - lower_bound(ts.begin(), ts.end(), *it);
}
vector<Int> ws(m);
while (!E.empty()) {
Event e;
Ratio r;
tie(r, e) = E.top(); E.pop();
int j, i1, i2;
tie(j, i1, i2) = e;
if (j != -1) {
ws[j] = W0 + W1 * ds[j];
} else {
if (S.count(i1) && S.count(i2)) {
auto it = next(S.find(i2));
int i3 = (it == S.end() ? n + 1 : *it);
W0 += (i3 - i2) * (ts[i1] - ts[i2]);
W1 += (Int) (i3 - i2) * (i2 - i1);
S.erase(i2);
if (i3 != n + 1) {
E.emplace(Ratio(ts[i3] - ts[i1], i3-i1), Event(-1,i1,i3));
}
}
}
}
for (Int w : ws) {
cout << w << '\n';
}
}
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 | #include "bits/stdc++.h" using namespace std; using Int = unsigned long long; using Event = tuple<int, int, int>; template<class T> using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>; struct Ratio { Ratio(Int num_=0ll, int den_=1):num(num_), den(den_){} Int num; int den; double ToDouble() const {return (double) num / den;} }; bool operator<(const Ratio& a, const Ratio& b) { return a.num * b.den < b.num * a.den; } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<Int> ts(n + 1); ts[0] = 0; for (int i = 1; i <= n; ++i) { cin >> ts[i]; } vector<int> ds(m); for (int j = 0; j < m; ++j) { cin >> ds[j]; } set<int> S; for (int i = 0; i <= n; ++i) { if (i == 0 || ts[i-1] < ts[i]) { S.insert(i); } } MinPriorityQueue<pair<Ratio, Event>> E; for (auto it = S.begin(); it != S.end() && next(it) != S.end(); ++it) { int i1 = *it; int i2 = *next(it); // ts[i1] + (i2 - i1) * d >= ts[i2] // d >= (ts[i2] - ts[i1]) / (i2 - i1) E.emplace(Ratio(ts[i2] - ts[i1], i2-i1), Event(-1,i1,i2)); } for (int j = 0; j < m; ++j) { Event e; get<0>(e) = j; E.emplace(ds[j], e); } Int W0 = 0; Int W1 = 0; for (auto it = ts.begin(); it != ts.end(); ++it) { W1 += it - lower_bound(ts.begin(), ts.end(), *it); } vector<Int> ws(m); while (!E.empty()) { Event e; Ratio r; tie(r, e) = E.top(); E.pop(); int j, i1, i2; tie(j, i1, i2) = e; if (j != -1) { ws[j] = W0 + W1 * ds[j]; } else { if (S.count(i1) && S.count(i2)) { auto it = next(S.find(i2)); int i3 = (it == S.end() ? n + 1 : *it); W0 += (i3 - i2) * (ts[i1] - ts[i2]); W1 += (Int) (i3 - i2) * (i2 - i1); S.erase(i2); if (i3 != n + 1) { E.emplace(Ratio(ts[i3] - ts[i1], i3-i1), Event(-1,i1,i3)); } } } } for (Int w : ws) { cout << w << '\n'; } } |
English