답안 #1067059

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067059 2024-08-20T10:29:32 Z manhlinh1501 Bigger segments (IZhO19_segments) C++17
73 / 100
497 ms 262144 KB
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 5e5 + 5;
using pli = pair<i64, int>;
const i64 oo64 = 6e14 + 5;

#define left ___left
#define right ___right

struct node {
    pli x = {0, 0};
    node *left = nullptr, *right = nullptr;

    node(pli x = {0, 0}, node *left = nullptr, node *right = nullptr) : x(x), left(left), right(right) {}
};

pli combine(const pli a, const pli b) {
    if(a.second > b.second)
        return a;
    if(a.second < b.second)
        return b;
    return {max(a.first, b.first), a.second};
}

struct segment_tree {
    node *root = new node();

    void update(node *cur, i64 l, i64 r, i64 p, pli x) {
        if(r < p or l > p) return;
        if(l == r) {
            cur -> x = combine(cur -> x, x);
            return;
        }
        i64 m = (l + r) / 2;


        if(p <= m) {
            if(cur -> left == nullptr) cur -> left = new node();
            update(cur -> left, l, m, p, x);
        } else {
            if(cur -> right == nullptr) cur -> right = new node();
            update(cur -> right, m + 1, r, p, x);
        }
        cur -> x = {0, 0};
        if(cur -> left != nullptr)
            cur -> x = combine(cur -> x, cur -> left -> x);
        if(cur -> right != nullptr)
            cur -> x = combine(cur -> x, cur -> right -> x);
    }

    pli get(node *cur, i64 l, i64 r, i64 u, i64 v) {
        if(r < u or l > v) return {0, 0};
        if(u <= l and r <= v) return cur -> x;
        i64 m = (l + r) / 2;
        pli res = {0, 0};
        if(cur -> left != nullptr)
            res = combine(res, get(cur -> left, l, m, u, v));
        if(cur -> right != nullptr)
            res = combine(res, get(cur -> right, m + 1, r, u, v));
        return res;
    }

    void update(i64 p, pli x) {
        return update(root, 1, oo64, p, x);
    }

    pli get(i64 l, i64 r) {
        return get(root, 1, oo64, l, r);
    }
} ST;

int N;
int a[MAXN];
i64 b[MAXN];
pli dp[MAXN];

signed main() {
#define TASK "code"

    if (fopen(TASK ".inp", "r")) {
        freopen(TASK ".inp", "r", stdin);
        freopen(TASK ".out", "w", stdout);
    }

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> N;
    for(int i = 1; i <= N; i++) cin >> a[i];
    for(int i = 1; i <= N; i++) b[i] = b[i - 1] + a[i];
    ST.update(0, {0, 0});
    for(int i = 1; i <= N; i++) {
        pli res = ST.get(0, b[i]);
        dp[i] = {b[i] - res.first, res.second + 1};
        ST.update(dp[i].first + b[i], {b[i], dp[i].second});
    }
    cout << dp[N].second;
    return (0 ^ 0);
}

/**
4
2 3 1 7
**/

/**
5
6 2 3 9 13
**/

/**
3
3 1 2
**/

Compilation message

segments.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("O3")
      | 
segments.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      | 
segments.cpp: In function 'int main()':
segments.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         freopen(TASK ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen(TASK ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 712 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 464 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 712 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 464 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 1032 KB Output is correct
18 Correct 1 ms 1112 KB Output is correct
19 Correct 1 ms 1116 KB Output is correct
20 Correct 1 ms 976 KB Output is correct
21 Correct 1 ms 980 KB Output is correct
22 Correct 1 ms 1116 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 1116 KB Output is correct
26 Correct 1 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 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 712 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 464 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 1032 KB Output is correct
18 Correct 1 ms 1112 KB Output is correct
19 Correct 1 ms 1116 KB Output is correct
20 Correct 1 ms 976 KB Output is correct
21 Correct 1 ms 980 KB Output is correct
22 Correct 1 ms 1116 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 1116 KB Output is correct
26 Correct 1 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 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 4 ms 2652 KB Output is correct
32 Correct 4 ms 3420 KB Output is correct
33 Correct 5 ms 3932 KB Output is correct
34 Correct 6 ms 3932 KB Output is correct
35 Correct 6 ms 4700 KB Output is correct
36 Correct 5 ms 4700 KB Output is correct
37 Correct 4 ms 3932 KB Output is correct
38 Correct 4 ms 3248 KB Output is correct
39 Correct 3 ms 860 KB Output is correct
40 Correct 5 ms 4444 KB Output is correct
41 Correct 3 ms 1112 KB Output is correct
42 Correct 6 ms 1880 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 4 ms 2244 KB Output is correct
45 Correct 4 ms 2140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 712 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 464 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 1032 KB Output is correct
18 Correct 1 ms 1112 KB Output is correct
19 Correct 1 ms 1116 KB Output is correct
20 Correct 1 ms 976 KB Output is correct
21 Correct 1 ms 980 KB Output is correct
22 Correct 1 ms 1116 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 1116 KB Output is correct
26 Correct 1 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 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 4 ms 2652 KB Output is correct
32 Correct 4 ms 3420 KB Output is correct
33 Correct 5 ms 3932 KB Output is correct
34 Correct 6 ms 3932 KB Output is correct
35 Correct 6 ms 4700 KB Output is correct
36 Correct 5 ms 4700 KB Output is correct
37 Correct 4 ms 3932 KB Output is correct
38 Correct 4 ms 3248 KB Output is correct
39 Correct 3 ms 860 KB Output is correct
40 Correct 5 ms 4444 KB Output is correct
41 Correct 3 ms 1112 KB Output is correct
42 Correct 6 ms 1880 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 4 ms 2244 KB Output is correct
45 Correct 4 ms 2140 KB Output is correct
46 Correct 90 ms 54796 KB Output is correct
47 Correct 115 ms 62292 KB Output is correct
48 Correct 152 ms 124500 KB Output is correct
49 Correct 172 ms 133892 KB Output is correct
50 Correct 196 ms 148052 KB Output is correct
51 Correct 179 ms 145788 KB Output is correct
52 Correct 88 ms 73052 KB Output is correct
53 Correct 53 ms 43860 KB Output is correct
54 Correct 90 ms 20564 KB Output is correct
55 Correct 189 ms 140884 KB Output is correct
56 Correct 130 ms 65188 KB Output is correct
57 Correct 108 ms 53988 KB Output is correct
58 Correct 117 ms 59472 KB Output is correct
59 Correct 140 ms 84044 KB Output is correct
60 Correct 143 ms 83872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 712 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 464 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 1032 KB Output is correct
18 Correct 1 ms 1112 KB Output is correct
19 Correct 1 ms 1116 KB Output is correct
20 Correct 1 ms 976 KB Output is correct
21 Correct 1 ms 980 KB Output is correct
22 Correct 1 ms 1116 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 1116 KB Output is correct
26 Correct 1 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 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 4 ms 2652 KB Output is correct
32 Correct 4 ms 3420 KB Output is correct
33 Correct 5 ms 3932 KB Output is correct
34 Correct 6 ms 3932 KB Output is correct
35 Correct 6 ms 4700 KB Output is correct
36 Correct 5 ms 4700 KB Output is correct
37 Correct 4 ms 3932 KB Output is correct
38 Correct 4 ms 3248 KB Output is correct
39 Correct 3 ms 860 KB Output is correct
40 Correct 5 ms 4444 KB Output is correct
41 Correct 3 ms 1112 KB Output is correct
42 Correct 6 ms 1880 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 4 ms 2244 KB Output is correct
45 Correct 4 ms 2140 KB Output is correct
46 Correct 90 ms 54796 KB Output is correct
47 Correct 115 ms 62292 KB Output is correct
48 Correct 152 ms 124500 KB Output is correct
49 Correct 172 ms 133892 KB Output is correct
50 Correct 196 ms 148052 KB Output is correct
51 Correct 179 ms 145788 KB Output is correct
52 Correct 88 ms 73052 KB Output is correct
53 Correct 53 ms 43860 KB Output is correct
54 Correct 90 ms 20564 KB Output is correct
55 Correct 189 ms 140884 KB Output is correct
56 Correct 130 ms 65188 KB Output is correct
57 Correct 108 ms 53988 KB Output is correct
58 Correct 117 ms 59472 KB Output is correct
59 Correct 140 ms 84044 KB Output is correct
60 Correct 143 ms 83872 KB Output is correct
61 Correct 427 ms 255268 KB Output is correct
62 Correct 493 ms 244500 KB Output is correct
63 Runtime error 497 ms 262144 KB Execution killed with signal 9
64 Halted 0 ms 0 KB -