Submission #344569

#TimeUsernameProblemLanguageResultExecution timeMemory
344569kekWwBigger segments (IZhO19_segments)C++17
100 / 100
218 ms40544 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <map>
#include <unordered_set>
#include <tuple>
#include <queue>
#include <set>
#include <cstring>
#include <functional>
#include <random>
#include <chrono>
#include <cassert>
#include <iomanip>
// #include <ext/pb_ds/assoc_container.hpp>

#define ar array
#define all(arr) (arr).begin(), (arr).end()
#define range(i, n) for (int i=0; i < (n); ++i)
#define rall(arr) (arr).rbegin(), (arr).rend()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

// using namespace __gnu_pbds;
/*
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
*/

const ll INF = 1e18;
const int INFi = 2e9;
const int maxN = 5e5 + 5;
const int md2 = 998244353;
const int md3 = 1e9 + 7;

double getTime() { return clock() / (double) CLOCKS_PER_SEC; }

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    range(i, n) cin >> a[i];
    vector<ll> pref(n);
    pref[0] = a[0];
    range(i, n - 1) pref[i + 1] = pref[i] + a[i + 1];
    vector<vector<pair<int, ll>>> add(n + 1);
    set<ll> s;
    s.insert(0);
    int cur = 0;
    range(i, n) {
        for(auto &[x, y] : add[i]) {
            if (x > cur) {
                cur = x;
                s.clear();
            }
            if (cur == x) s.insert(y);
        }
        ll d = pref[i] - *s.rbegin();
        int j = lower_bound(all(pref), pref[i] + d) - pref.begin();
        add[j].emplace_back(cur + 1, pref[i]);
    }
    cout << cur + 1 << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // cout << setprecision(15) << fixed;
    int tests = 1;
    // cin >> tests;
    range(_, tests) {
        solve();
    }
    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...