답안 #1067030

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067030 2024-08-20T09:53:49 Z manhlinh1501 Bigger segments (IZhO19_segments) C++17
73 / 100
460 ms 262144 KB
#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: In function 'int main()':
segments.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         freopen(TASK ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         freopen(TASK ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 348 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 348 KB Output is correct
15 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 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 348 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 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 856 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 2 ms 1116 KB Output is correct
19 Correct 1 ms 1112 KB Output is correct
20 Correct 1 ms 1116 KB Output is correct
21 Correct 1 ms 1116 KB Output is correct
22 Correct 1 ms 860 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 0 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 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 348 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 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 856 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 2 ms 1116 KB Output is correct
19 Correct 1 ms 1112 KB Output is correct
20 Correct 1 ms 1116 KB Output is correct
21 Correct 1 ms 1116 KB Output is correct
22 Correct 1 ms 860 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 0 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 3 ms 2648 KB Output is correct
32 Correct 4 ms 3420 KB Output is correct
33 Correct 6 ms 3932 KB Output is correct
34 Correct 5 ms 3748 KB Output is correct
35 Correct 5 ms 4700 KB Output is correct
36 Correct 6 ms 4696 KB Output is correct
37 Correct 7 ms 3932 KB Output is correct
38 Correct 3 ms 3164 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 5 ms 4444 KB Output is correct
41 Correct 2 ms 1112 KB Output is correct
42 Correct 3 ms 1628 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 3 ms 2136 KB Output is correct
45 Correct 3 ms 2140 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 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 348 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 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 856 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 2 ms 1116 KB Output is correct
19 Correct 1 ms 1112 KB Output is correct
20 Correct 1 ms 1116 KB Output is correct
21 Correct 1 ms 1116 KB Output is correct
22 Correct 1 ms 860 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 0 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 3 ms 2648 KB Output is correct
32 Correct 4 ms 3420 KB Output is correct
33 Correct 6 ms 3932 KB Output is correct
34 Correct 5 ms 3748 KB Output is correct
35 Correct 5 ms 4700 KB Output is correct
36 Correct 6 ms 4696 KB Output is correct
37 Correct 7 ms 3932 KB Output is correct
38 Correct 3 ms 3164 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 5 ms 4444 KB Output is correct
41 Correct 2 ms 1112 KB Output is correct
42 Correct 3 ms 1628 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 3 ms 2136 KB Output is correct
45 Correct 3 ms 2140 KB Output is correct
46 Correct 80 ms 54352 KB Output is correct
47 Correct 111 ms 61776 KB Output is correct
48 Correct 142 ms 123768 KB Output is correct
49 Correct 149 ms 133168 KB Output is correct
50 Correct 166 ms 148048 KB Output is correct
51 Correct 158 ms 145752 KB Output is correct
52 Correct 112 ms 73424 KB Output is correct
53 Correct 59 ms 43860 KB Output is correct
54 Correct 66 ms 20432 KB Output is correct
55 Correct 164 ms 140808 KB Output is correct
56 Correct 105 ms 65104 KB Output is correct
57 Correct 112 ms 54028 KB Output is correct
58 Correct 97 ms 59444 KB Output is correct
59 Correct 114 ms 84112 KB Output is correct
60 Correct 116 ms 83640 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 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 348 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 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 856 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 2 ms 1116 KB Output is correct
19 Correct 1 ms 1112 KB Output is correct
20 Correct 1 ms 1116 KB Output is correct
21 Correct 1 ms 1116 KB Output is correct
22 Correct 1 ms 860 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 0 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 3 ms 2648 KB Output is correct
32 Correct 4 ms 3420 KB Output is correct
33 Correct 6 ms 3932 KB Output is correct
34 Correct 5 ms 3748 KB Output is correct
35 Correct 5 ms 4700 KB Output is correct
36 Correct 6 ms 4696 KB Output is correct
37 Correct 7 ms 3932 KB Output is correct
38 Correct 3 ms 3164 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 5 ms 4444 KB Output is correct
41 Correct 2 ms 1112 KB Output is correct
42 Correct 3 ms 1628 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 3 ms 2136 KB Output is correct
45 Correct 3 ms 2140 KB Output is correct
46 Correct 80 ms 54352 KB Output is correct
47 Correct 111 ms 61776 KB Output is correct
48 Correct 142 ms 123768 KB Output is correct
49 Correct 149 ms 133168 KB Output is correct
50 Correct 166 ms 148048 KB Output is correct
51 Correct 158 ms 145752 KB Output is correct
52 Correct 112 ms 73424 KB Output is correct
53 Correct 59 ms 43860 KB Output is correct
54 Correct 66 ms 20432 KB Output is correct
55 Correct 164 ms 140808 KB Output is correct
56 Correct 105 ms 65104 KB Output is correct
57 Correct 112 ms 54028 KB Output is correct
58 Correct 97 ms 59444 KB Output is correct
59 Correct 114 ms 84112 KB Output is correct
60 Correct 116 ms 83640 KB Output is correct
61 Correct 382 ms 255060 KB Output is correct
62 Correct 416 ms 244832 KB Output is correct
63 Runtime error 460 ms 262144 KB Execution killed with signal 9
64 Halted 0 ms 0 KB -