Submission #918229

#TimeUsernameProblemLanguageResultExecution timeMemory
918229borisAngelovDivide and conquer (IZhO14_divide)C++17
100 / 100
80 ms11864 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; const long long inf = 1e17; int n; struct Element { int pos; long long gold; long long energy; }; Element a[maxn]; long long prefEenrgy[maxn]; long long prefGold[maxn]; struct SegmentTree { long long tree[maxn * 4]; long long lazy[maxn * 4]; void build(int node, int l, int r) { lazy[node] = 0; if (l == r) { tree[node] = prefEenrgy[l] - a[l].pos; return; } int mid = (l + r) / 2; build(2 * node, l, mid); build(2 * node + 1, mid + 1, r); tree[node] = max(tree[2 * node], tree[2 * node + 1]); } void pushLazy(int node, int l, int r) { tree[node] += lazy[node]; if (l != r) { lazy[2 * node] += lazy[node]; lazy[2 * node + 1] += lazy[node]; } lazy[node] = 0; } void update(int node, int l, int r, int ql, int qr, long long delta) { pushLazy(node, l, r); if (l > qr || r < ql) { return; } if (ql <= l && r <= qr) { lazy[node] += delta; pushLazy(node, l, r); return; } int mid = (l + r) / 2; update(2 * node, l, mid, ql, qr, delta); update(2 * node + 1, mid + 1, r, ql, qr, delta); tree[node] = max(tree[2 * node], tree[2 * node + 1]); } int walk(int node, int l, int r) { pushLazy(node, l, r); if (l == r) { if (tree[node] >= 0) { return l; } else { return -1; } } int mid = (l + r) / 2; if (tree[2 * node + 1] + lazy[2 * node + 1] >= 0) { return walk(2 * node + 1, mid + 1, r); } else { return walk(2 * node, l, mid); } } void build() { build(1, 1, n); } void update(int l, int r, long long delta) { update(1, 1, n, l, r, delta); } }; SegmentTree tree; void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { fastIO(); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i].pos >> a[i].gold >> a[i].energy; prefEenrgy[i] = prefEenrgy[i - 1] + a[i].energy; prefGold[i] = prefGold[i - 1] + a[i].gold; } for (int i = 2; i <= n; ++i) { a[i].pos -= a[1].pos; } a[1].pos = 0; tree.build(); long long ans = -inf; for (int i = 1; i <= n; ++i) { ans = max(ans, a[i].gold); } for (int i = 1; i <= n; ++i) { int pos = tree.walk(1, 1, n); if (pos != -1) { ans = max(ans, prefGold[pos] - prefGold[i - 1]); } if (i + 1 <= n) { tree.update(i + 1, n, (a[i + 1].pos - a[i].pos) - a[i].energy); } tree.update(i, i, -inf); } cout << ans << endl; return 0; } /* 4 0 5 1 1 7 2 4 4 1 7 15 1 4 1 5 1 2 7 2 5 4 1 8 15 1 2 2 4 1 5 5 1 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...