Submission #161952

#TimeUsernameProblemLanguageResultExecution timeMemory
161952Alexa2001Bigger segments (IZhO19_segments)C++17
100 / 100
147 ms20984 KiB
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 5e5 + 5;
typedef long long ll;

ll s[Nmax];
int n, a[Nmax];

pair<int, ll> dp[Nmax];

class AIB 
{
    int a[Nmax];
    int ub(int x) { return (x&(-x)); }

public:
    int query(int x)
    {
        int ans = 0;
        for(; x; x-=ub(x)) ans = max(ans, a[x]);
        return ans;
    }

    void update(int x, int y)
    {
        for(; x<=n; x+=ub(x)) a[x] = max(a[x], y);
    }
} aib;

void add(int pos)
{
    int leftmost = lower_bound(s+1, s+n+1, s[pos] + dp[pos].second) - s;
    aib.update(leftmost, pos);
}

int main()
{
  //  freopen("input", "r", stdin);
    cin.sync_with_stdio(false); cin.tie(0);

    cin >> n;
    int i;
    for(i=1; i<=n; ++i) cin >> a[i];
    for(i=1; i<=n; ++i) s[i] = s[i-1] + a[i];

    for(i=1; i<=n; ++i)
    {
        int j = aib.query(i);
        
        dp[i] = { dp[j].first + 1, s[i] - s[j] };
        add(i);
    }

    cout << dp[n].first << '\n';
    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...