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
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args<< " -> "; err(args); } while (0) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int maxn = 1E5 + 100; struct Q { LL D, P, R, G; }; struct P { LL x, y; }; P operator - (P& a, P& b) { return {a.x - b.x, a.y - b.y}; } Q a[maxn]; LL n, m, C; LL f[maxn];
__int128 det(P a, P b) { return (__int128)a.x * b.y - (__int128)a.y * b.x; }
void go(LL l, LL r) { if (l + 1 == r) return; LL m = l + r >> 1; go(l, m); static deque<P> p, q; p.clear(); q.clear(); FOR (i, l, m) if (f[i] >= a[i].P) p.push_back({a[i].G, f[i] - a[i].P + a[i].R - (a[i].D + 1) * a[i].G}); if (p.empty()) { go(m, r); return; } sort(p.begin(), p.end(), [](const P& a, const P& b){ if (a.x == b.x) return a.y > b.y; return a.x < b.x; }); for (P& x: p) { LL t; while ((t = q.size()) >= 2 && det(q[t - 1] - q[t - 2], x - q[t - 1]) >= 0) q.pop_back(); q.push_back(x); } FOR (i, m, r) { auto ans = [&](LL k) { return q[k].y + q[k].x * a[i].D; }; while (q.size() >= 2 && ans(0) <= ans(1)) q.pop_front(); f[i] = max(f[i], ans(0)); } go(m, r); }
int main() { int Ca = 0; while (cin >> n >> C >> m) { if (!(n || C || m)) break; FOR (i, 0, n) scanf("%lld%lld%lld%lld", &a[i].D, &a[i].P, &a[i].R, &a[i].G); a[n++] = {m + 1, 0, 0, 0}; sort(a, a + n, [](const Q &a, const Q &b) { return a.D < b.D; }); FOR (i, 0, n) f[i] = C; go(0, n); printf("Case %d: %lld\n", ++Ca, f[n - 1]); } }
|