Submission #1299666

#TimeUsernameProblemLanguageResultExecution timeMemory
1299666daotuankhoiDivide and conquer (IZhO14_divide)C++20
100 / 100
33 ms5648 KiB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define ll long long

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

template <class T> bool ckmax(T &a, T b) { return a < b ? (a = b, true) : false; }
template <class T> bool ckmin(T &a, T b) { return a > b ? (a = b, true) : false; }

const int MAXN = 1e5 + 5;
ll x[MAXN], g[MAXN], d[MAXN], pre[MAXN], cost[MAXN];

ll bit[MAXN];

void add(int i, ll v) {
    for (; i > 0; i -= i & -i) ckmin(bit[i], v);
}

ll get(int i) {
    ll ans = 1e18;
    for (; i < MAXN; i += i & -i) ckmin(ans, bit[i]);
    return ans;
}

int main() {
    #define NAME "test"
    if (fopen(NAME".inp", "r")) {
        freopen(NAME".inp", "r", stdin);
        freopen(NAME".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> x[i] >> g[i] >> d[i];
    vector<int> comp;
    for (int i = 1; i <= n; i++) {
        pre[i] = pre[i - 1] + d[i];
        cost[i] = cost[i - 1] + g[i];
        comp.emplace_back(x[i] - pre[i - 1]);
    }
    ll ans = 0;
    sort(comp.begin(), comp.end());
    comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
    memset(bit, 0x3f, sizeof bit);
    for (int i = 1; i <= n; i++) {
        int it = lower_bound(comp.begin(), comp.end(), x[i] - pre[i - 1]) - comp.begin() + 1;
        add(it, cost[i - 1]);
        it = lower_bound(comp.begin(), comp.end(), x[i] - pre[i]) - comp.begin() + 1;
        ckmax(ans, cost[i] - get(it));
    }
    cout << ans << '\n';
    return 0;
}

Compilation message (stderr)

divide.cpp: In function 'int main()':
divide.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(NAME".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
divide.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen(NAME".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...