답안 #938122

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
938122 2024-03-04T20:51:09 Z sysia Bigger segments (IZhO19_segments) C++17
100 / 100
154 ms 29584 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 50; 
const int mod = 998244353;

void ckmax(T &a, T b){
    if (a.first < b.first || (a.first == b.first && a.second > b.second)) a = b;
}

struct Tree{
    vector<T>tab;
    int size = 1;

    Tree(int n){
        while (size < n) size*=2;
        tab.assign(2*size, {-oo, oo});
    }

    T query(int x){
        T ans = {-oo, oo};
        x += size;
        while (x){
            ckmax(ans, tab[x]);
            x/=2;
        }
        return ans;
    }

    void update(int l, int r, T val){
        for (l += size-1, r += size+1; r-l>1; l/=2, r/=2){
            if (!(l&1)) ckmax(tab[l+1], val);
            if (r&1) ckmax(tab[r-1], val);
        }
    }
};

void solve(){
    int n; cin >> n;
    vector<int>a(n+1), pref(n+1);
    for (int i = 1; i<=n; i++){
        cin >> a[i];
        pref[i] = pref[i-1] + a[i];
    }
    Tree t(n+2);
    t.update(0, 0, {0, 0});
    // vector<T>dp(n+1, {-oo, oo});
    // dp[0] = {0, 0};
    for (int i = 0; i<n; i++){
        auto [mx, sum] = t.query(i);
        sum += pref[i];
        // auto [mx, sum] = dp[i];
        int p = max(i+1, (int)(lower_bound(pref.begin(), pref.end(), sum + pref[i])-pref.begin())); //[i+1, p]
        debug(i, mx, sum, p);
        if (p <= n) t.update(p, n, T{mx+1, -pref[i]});
        // for (int j = p; j <= n; j++){
            // ckmax(dp[j], {mx+1, pref[j]-pref[i]});
        // }
    }
    int ans = 0;
    for (int i = 1; i<=n; i++) ans = max(ans, t.query(i).first);
    cout << ans << "\n";
    // cout << dp[n].first << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 452 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 452 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 452 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 452 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 452 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 452 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 2 ms 464 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 540 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 1 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 452 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 452 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 2 ms 464 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 540 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 1 ms 856 KB Output is correct
46 Correct 19 ms 6492 KB Output is correct
47 Correct 23 ms 6436 KB Output is correct
48 Correct 36 ms 6748 KB Output is correct
49 Correct 26 ms 7004 KB Output is correct
50 Correct 26 ms 7000 KB Output is correct
51 Correct 30 ms 7256 KB Output is correct
52 Correct 14 ms 3676 KB Output is correct
53 Correct 9 ms 2140 KB Output is correct
54 Correct 24 ms 6236 KB Output is correct
55 Correct 29 ms 7100 KB Output is correct
56 Correct 26 ms 6604 KB Output is correct
57 Correct 19 ms 6532 KB Output is correct
58 Correct 21 ms 6492 KB Output is correct
59 Correct 24 ms 6492 KB Output is correct
60 Correct 25 ms 6640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 452 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 452 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 2 ms 464 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 540 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 1 ms 856 KB Output is correct
46 Correct 19 ms 6492 KB Output is correct
47 Correct 23 ms 6436 KB Output is correct
48 Correct 36 ms 6748 KB Output is correct
49 Correct 26 ms 7004 KB Output is correct
50 Correct 26 ms 7000 KB Output is correct
51 Correct 30 ms 7256 KB Output is correct
52 Correct 14 ms 3676 KB Output is correct
53 Correct 9 ms 2140 KB Output is correct
54 Correct 24 ms 6236 KB Output is correct
55 Correct 29 ms 7100 KB Output is correct
56 Correct 26 ms 6604 KB Output is correct
57 Correct 19 ms 6532 KB Output is correct
58 Correct 21 ms 6492 KB Output is correct
59 Correct 24 ms 6492 KB Output is correct
60 Correct 25 ms 6640 KB Output is correct
61 Correct 58 ms 26452 KB Output is correct
62 Correct 87 ms 26628 KB Output is correct
63 Correct 118 ms 27028 KB Output is correct
64 Correct 136 ms 29008 KB Output is correct
65 Correct 127 ms 29532 KB Output is correct
66 Correct 145 ms 29520 KB Output is correct
67 Correct 115 ms 26996 KB Output is correct
68 Correct 57 ms 13652 KB Output is correct
69 Correct 124 ms 25680 KB Output is correct
70 Correct 154 ms 29584 KB Output is correct
71 Correct 112 ms 26444 KB Output is correct
72 Correct 99 ms 27028 KB Output is correct
73 Correct 113 ms 27484 KB Output is correct
74 Correct 121 ms 28012 KB Output is correct
75 Correct 122 ms 28004 KB Output is correct