Submission #344692

#TimeUsernameProblemLanguageResultExecution timeMemory
344692NurlykhanBigger segments (IZhO19_segments)C++17
100 / 100
195 ms19540 KiB
#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int N = (int)5e5 + 10;
const int mod = (int)1e9 + 7;
const ll inf = (ll) 1e16 + 10;

int n;
int a[N];
ll pref[N];

ll get_sum(int l, int r) {
    return pref[r] - pref[l - 1];
}

pair<int, int> dp[N];
pair<int, int> t[N];

int sz = 1;
vector<ll> ids;

int get_id(ll x) {
    //cout << x << " -> id=" << lower_bound(ids.begin(), ids.end(), x) - ids.begin() - 1 << endl;;
    return lower_bound(ids.begin(), ids.end(), x) - ids.begin() - 1;
}

pair<int, int> get(ll x) {
    assert(x >= 0);
    pair<int, int> ans = make_pair(0, 0);
    while (x >= 0) {
        ans = max(ans, t[x]);
        x &= x + 1;
        x--;
    }
    return ans;
}

void upd(ll x, pair<int, int> y) {
    assert(x >= 0);
    while (x <= ids.size()) {
        t[x] = max(t[x], y);
        x |= x + 1;
    }
}


int main() {
   // freopen("in.txt", "r", stdin);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        pref[i] = pref[i - 1] + a[i];
        ids.push_back(pref[i]);
    }

    ids.push_back(-inf);
    ids.push_back(inf);
    sort(ids.begin(), ids.end());
    //for (auto it : ids) cout << it << ' '; cout << endl;
    for (int i = 1; i <= n; i++) {
        dp[i] = make_pair(1, 1);
        dp[i] = max(dp[i], get(get_id(pref[i])));
        upd(get_id(2 * pref[i] - pref[dp[i].second - 1]), make_pair(dp[i].first + 1, i + 1));
    }
    //cout << "\n";
    cout << dp[n].first;
    return 0;
}

Compilation message (stderr)

segments.cpp: In function 'void upd(long long int, std::pair<int, int>)':
segments.cpp:43:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     while (x <= ids.size()) {
      |            ~~^~~~~~~~~~~~~
segments.cpp: In function 'int main()':
segments.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   52 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
segments.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   54 |         scanf("%d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~
#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...