Submission #1039803

#TimeUsernameProblemLanguageResultExecution timeMemory
1039803juicyTwo Dishes (JOI19_dishes)C++17
100 / 100
2267 ms166112 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif const int N = 1e6 + 5; const long long inf = 1e18; int n, m; int p[N], q[N]; long long s[N], t[N], a[N], b[N], dp[4 * N], lz[4 * N]; void bld(int id = 1, int l = 0, int r = m) { if (l == r) { dp[id] = 0; return; } int md = (l + r) / 2; bld(id * 2, l, md); bld(id * 2 + 1, md + 1, r); dp[id] = -inf; } void app(int id, long long x) { dp[id] += x; lz[id] += x; } void psh(int id) { if (lz[id]) { app(id * 2, lz[id]); app(id * 2 + 1, lz[id]); lz[id] = 0; } if (dp[id] != -inf) { dp[id * 2] = max(dp[id * 2], dp[id]); dp[id * 2 + 1] = max(dp[id * 2 + 1], dp[id]); dp[id] = -inf; } } void upd(int u, int v, long long x, int id = 1, int l = 0, int r = m) { if (u <= l && r <= v) { app(id, x); return; } psh(id); int md = (l + r) / 2; if (u <= md) { upd(u, v, x, id * 2, l, md); } if (md < v) { upd(u, v, x, id * 2 + 1, md + 1, r); } } void add(int u, int v, long long x, int id = 1, int l = 0, int r = m) { if (u <= l && r <= v) { dp[id] = max(dp[id], x); return; } psh(id); int md = (l + r) / 2; if (u <= md) { add(u, v, x, id * 2, l, md); } if (md < v) { add(u, v, x, id * 2 + 1, md + 1, r); } } long long qry(int i, int id = 1, int l = 0, int r = m) { if (l == r) { return dp[id]; } psh(id); int md = (l + r) / 2; if (i <= md) { return qry(i, id * 2, l, md); } return qry(i, id * 2 + 1, md + 1, r); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i] >> s[i] >> p[i]; a[i] += a[i - 1]; } for (int i = 1; i <= m; ++i) { cin >> b[i] >> t[i] >> q[i]; b[i] += b[i - 1]; } vector<array<int, 3>> cands; long long sum = 0; for (int i = 1; i <= n; ++i) { int j = upper_bound(b, b + m + 1, s[i] - a[i]) - b; if (j) { cands.push_back({i, 1 - j, p[i]}); } } for (int j = 1; j <= m; ++j) { int i = upper_bound(a, a + n + 1, t[j] - b[j]) - a; if (i) { cands.push_back({i, 1 - j, -q[j]}); sum += q[j]; } } sort(cands.begin(), cands.end()); bld(); for (auto [x, y, v] : cands) { if (x > n) { break; } y = -y; upd(0, y, v); add(y, m, qry(y)); } cout << sum + qry(m); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...