Submission #916770

#TimeUsernameProblemLanguageResultExecution timeMemory
916770atomDivide and conquer (IZhO14_divide)C++17
17 / 100
1 ms2516 KiB
#include "bits/stdc++.h" // @JASPER'S BOILERPLATE using namespace std; using ll = long long; #ifdef JASPER #include "debug.h" #else #define debug(...) 166 #endif const int N = 2e5 + 5; template <typename T> struct FenwickTree { vector <T> f; int n; FenwickTree(int _n){ init(_n); } void init(int _n) { n = _n; f.assign(n + 5, -1e18); } void upd(int x, ll val) { for (; x <= n; x += x & (-x)) f[x] = max(f[x], val); } ll qry(int x) { ll ans = -1e18; for (; x > 0; x -= x & (-x)) ans = max(ans, f[x]); return ans; } }; int n; int x[N], g[N], e[N]; ll prfE[N], prfG[N], dp[N]; signed main() { cin.tie(0) -> sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> x[i] >> g[i] >> e[i]; vector <ll> vals; for (int i = 1; i <= n; ++i) { prfG[i] = prfG[i - 1] + g[i]; prfE[i] = prfE[i - 1] + e[i]; vals.push_back(prfE[i] - x[i]); vals.push_back(prfE[i - 1] - x[i]); // xi - xj <= fe(i) - fe(j - 1) -> fe(i) - xi >= fe(j - 1) - xj // dp(i) = dp(j) + f(j + 1, i) = f(i) + max(dp(j) - f(j)); } sort(vals.begin(), vals.end()); vals.resize(unique(vals.begin(), vals.end()) - vals.begin()); auto get = [&] (ll tar) { return (int) (lower_bound(vals.begin(), vals.end(), tar) - vals.begin() + 1); }; FenwickTree <ll> fen((int) vals.size()); for (int i = 1; i <= n; ++i) { ll p = get(prfE[i] - x[i]); dp[i] = max(1LL * g[i], prfG[i] + fen.qry(p)); ll _p = get(prfE[i - 1] - x[i]); fen.upd(_p, dp[i] - prfG[i]); } cout << (*max_element(dp + 1, dp + 1 + n)) << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...