#include <cstdio>
#include <utility>
#include <vector>
#include <algorithm>
#include <cassert>
const int MAX_N = 500009;
int n, m;
long long a[MAX_N];
struct cut {
int from;
long long day;
long long height;
};
std::vector<cut> h;
long long ab[MAX_N];
long long compute(long long d, long long b) {
long long sum = 0;
int r = n;
int l = r;
long long bh, dd;
// pierwsze schodek nie caly sciety
auto it = h.rbegin();
for (; it != h.rend(); ++it) {
r = l;
l = it->from;
bh = it->height;
dd = d-it->day;
if ((bh+dd*a[l]) <= b) {
break;
}
// ile urosnie - (ile ma byc - ile bylo) * ile elementow
sum += dd*(ab[r]-ab[l])-(b-bh)*(r-l);
}
// wszystkie po tym, ktory nie caly do wywalenia
h.erase(it.base(), h.end());
if (h.empty()) {
assert(l == 0);
// scielismy wszystkie
h.push_back({l, d, b});
return sum;
}
// pierwszy ktory da sie sciac
auto it2 = std::lower_bound(a+l, a+r, b, [dd, bh](long long ai, long long b) {
return bh+dd*ai <= b;
});
l = std::distance(a, it2);
// ile urosnie - (ile ma byc - ile bylo) * ile elementow
sum += dd*(ab[r]-ab[l])-(b-bh)*(r-l);
// jesli cos w ogole scielismy
assert(sum != 0 || r == n);
if (sum != 0) {
h.push_back({l, d, b});
}
return sum;
}
void preproc() {
long long sum = 0;
for (int i = 0; i < n; ++i) {
ab[i] = sum;
sum += a[i];
}
ab[n] = sum;
h.push_back({0, 0ll, 0ll});
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%lld", &a[i]);
}
std::sort(a, a+n);
preproc();
for (int i = 0; i < m; ++i) {
long long d, b;
scanf("%lld%lld", &d, &b);
printf("%lld\n", compute(d, b));
}
}
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 | #include <cstdio> #include <utility> #include <vector> #include <algorithm> #include <cassert> const int MAX_N = 500009; int n, m; long long a[MAX_N]; struct cut { int from; long long day; long long height; }; std::vector<cut> h; long long ab[MAX_N]; long long compute(long long d, long long b) { long long sum = 0; int r = n; int l = r; long long bh, dd; // pierwsze schodek nie caly sciety auto it = h.rbegin(); for (; it != h.rend(); ++it) { r = l; l = it->from; bh = it->height; dd = d-it->day; if ((bh+dd*a[l]) <= b) { break; } // ile urosnie - (ile ma byc - ile bylo) * ile elementow sum += dd*(ab[r]-ab[l])-(b-bh)*(r-l); } // wszystkie po tym, ktory nie caly do wywalenia h.erase(it.base(), h.end()); if (h.empty()) { assert(l == 0); // scielismy wszystkie h.push_back({l, d, b}); return sum; } // pierwszy ktory da sie sciac auto it2 = std::lower_bound(a+l, a+r, b, [dd, bh](long long ai, long long b) { return bh+dd*ai <= b; }); l = std::distance(a, it2); // ile urosnie - (ile ma byc - ile bylo) * ile elementow sum += dd*(ab[r]-ab[l])-(b-bh)*(r-l); // jesli cos w ogole scielismy assert(sum != 0 || r == n); if (sum != 0) { h.push_back({l, d, b}); } return sum; } void preproc() { long long sum = 0; for (int i = 0; i < n; ++i) { ab[i] = sum; sum += a[i]; } ab[n] = sum; h.push_back({0, 0ll, 0ll}); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%lld", &a[i]); } std::sort(a, a+n); preproc(); for (int i = 0; i < m; ++i) { long long d, b; scanf("%lld%lld", &d, &b); printf("%lld\n", compute(d, b)); } } |
English