Submission #1103954

# Submission time Handle Problem Language Result Execution time Memory
1103954 2024-10-22T12:47:41 Z TrendBattles Triple Jump (JOI19_jumps) C++14
100 / 100
812 ms 72412 KB
#include <bits/stdc++.h>
using namespace std;
using lli = int64_t;

#define INFILE "tjump.inp"
#define OUTFILE "tjump.out"

int maximise(int& x, int y) {
    if (x < y) return x = y, true;
    return false;
}

const int MAX_N = (int) 5e5;
int value[MAX_N + 10];

//int debugging = false;
struct SegmentTree {
    vector <int> max_value, max_lazy, max_all;
    int N;
    void build(int v, int tl, int tr) {
        if (tl == tr) {
            max_value[v] = value[tl];
            return;
        }

        int tm = (tl + tr) >> 1;
        build(v << 1, tl, tm);
        build(v << 1 | 1, tm + 1, tr);

        max_value[v] = max(max_value[v << 1], max_value[v << 1 | 1]);
    }
    SegmentTree(int N): N(N), max_value(N << 2), max_lazy(N << 2), max_all(N << 2) {
        build(1, 1, N);
    }

    void Apply(int v, int delta) {
        maximise(max_lazy[v], delta);
        maximise(max_all[v], max_lazy[v] + max_value[v]);
    }
    void Push(int v) {
        for (int child : {v << 1, v << 1 | 1}) {
            Apply(child, max_lazy[v]);
        }
    }
    void update_range(int v, int tl, int tr, int l, int r, int delta) {
        if (l > r) return;

        if (l == tl and r == tr) {
            Apply(v, delta);
            
            return;
        }

        Push(v);
        int tm = (tl + tr) >> 1;
        update_range(v << 1, tl, tm, l, min(r, tm), delta);
        update_range(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r, delta);

        //max_lazy[v] = max(max_lazy[v << 1], max_lazy[v << 1 | 1]);
        max_all[v] = max(max_all[v << 1], max_all[v << 1 | 1]);
    }
    void update_range(int l, int r, int delta) {
        update_range(1, 1, N, l, r, delta);
    }

    int get_range(int v, int tl, int tr, int l, int r) {
        if (l > r) return 0;

        if (l == tl and r == tr) return max_all[v];
        Push(v);
        int tm = (tl + tr) >> 1;

        return max(
            get_range(v << 1, tl, tm, l, min(r, tm)),
            get_range(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r)
        );
    }

    int get_range(int l, int r) {
        return get_range(1, 1, N, l, r);
    }
};
int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    if (fopen(INFILE, "r")) {
        freopen(INFILE, "r", stdin);
        freopen(OUTFILE, "w", stdout);
    }   

    int N; cin >> N;
    // if (N <= 5000) {
    //     SUBTASK_1::main(N);
    //     return 0;
    // }

    for (int i = 1; i <= N; ++i) cin >> value[i];
    SegmentTree tree(N);

    vector <vector <int>> query_line(N + 1);
    vector <int> stack;
    
    int Q; cin >> Q;
    vector <int> start(Q), dest(Q);
    for (int qu = 0; qu < Q; ++qu) {
        cin >> start[qu] >> dest[qu];
        query_line[start[qu]].push_back(qu);
    }

    vector <int> finale(Q);
    for (int i = N; i >= 1; --i) {
        while (not stack.empty()) {
            int j = stack.back(); 

           // debugging = i == 11 and j == 12;

            tree.update_range(2 * j - i, N, value[i] + value[j]);

            if (value[i] < value[j]) break;
            stack.pop_back();
        }   

        
        stack.push_back(i);
        for (int id : query_line[i]) {
            finale[id] = tree.get_range(i + 2, dest[id]);
        }
    }

    for (int qu = 0; qu < Q; ++qu) cout << finale[qu] << '\n';
    return 0;
}

Compilation message

jumps.cpp: In constructor 'SegmentTree::SegmentTree(int)':
jumps.cpp:19:9: warning: 'SegmentTree::N' will be initialized after [-Wreorder]
   19 |     int N;
      |         ^
jumps.cpp:18:18: warning:   'std::vector<int> SegmentTree::max_value' [-Wreorder]
   18 |     vector <int> max_value, max_lazy, max_all;
      |                  ^~~~~~~~~
jumps.cpp:32:5: warning:   when initialized here [-Wreorder]
   32 |     SegmentTree(int N): N(N), max_value(N << 2), max_lazy(N << 2), max_all(N << 2) {
      |     ^~~~~~~~~~~
jumps.cpp: In function 'int main()':
jumps.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(INFILE, "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
jumps.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         freopen(OUTFILE, "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 206 ms 17736 KB Output is correct
12 Correct 208 ms 18248 KB Output is correct
13 Correct 214 ms 17372 KB Output is correct
14 Correct 196 ms 17736 KB Output is correct
15 Correct 232 ms 15176 KB Output is correct
16 Correct 214 ms 16672 KB Output is correct
17 Correct 227 ms 14664 KB Output is correct
18 Correct 207 ms 14408 KB Output is correct
19 Correct 203 ms 14920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 15952 KB Output is correct
2 Correct 63 ms 16332 KB Output is correct
3 Correct 64 ms 16208 KB Output is correct
4 Correct 114 ms 16976 KB Output is correct
5 Correct 114 ms 16208 KB Output is correct
6 Correct 118 ms 16196 KB Output is correct
7 Correct 111 ms 16208 KB Output is correct
8 Correct 112 ms 15952 KB Output is correct
9 Correct 112 ms 16456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 206 ms 17736 KB Output is correct
12 Correct 208 ms 18248 KB Output is correct
13 Correct 214 ms 17372 KB Output is correct
14 Correct 196 ms 17736 KB Output is correct
15 Correct 232 ms 15176 KB Output is correct
16 Correct 214 ms 16672 KB Output is correct
17 Correct 227 ms 14664 KB Output is correct
18 Correct 207 ms 14408 KB Output is correct
19 Correct 203 ms 14920 KB Output is correct
20 Correct 118 ms 15952 KB Output is correct
21 Correct 63 ms 16332 KB Output is correct
22 Correct 64 ms 16208 KB Output is correct
23 Correct 114 ms 16976 KB Output is correct
24 Correct 114 ms 16208 KB Output is correct
25 Correct 118 ms 16196 KB Output is correct
26 Correct 111 ms 16208 KB Output is correct
27 Correct 112 ms 15952 KB Output is correct
28 Correct 112 ms 16456 KB Output is correct
29 Correct 727 ms 57356 KB Output is correct
30 Correct 560 ms 68024 KB Output is correct
31 Correct 694 ms 65880 KB Output is correct
32 Correct 759 ms 63048 KB Output is correct
33 Correct 812 ms 65864 KB Output is correct
34 Correct 782 ms 61512 KB Output is correct
35 Correct 784 ms 65040 KB Output is correct
36 Correct 777 ms 63816 KB Output is correct
37 Correct 702 ms 62536 KB Output is correct
38 Correct 537 ms 69704 KB Output is correct
39 Correct 522 ms 72412 KB Output is correct
40 Correct 526 ms 62280 KB Output is correct
41 Correct 525 ms 66888 KB Output is correct
42 Correct 539 ms 67040 KB Output is correct
43 Correct 590 ms 70632 KB Output is correct
44 Correct 634 ms 63304 KB Output is correct
45 Correct 639 ms 63328 KB Output is correct
46 Correct 561 ms 61824 KB Output is correct
47 Correct 541 ms 61768 KB Output is correct
48 Correct 554 ms 68936 KB Output is correct
49 Correct 555 ms 70560 KB Output is correct
50 Correct 630 ms 67128 KB Output is correct
51 Correct 712 ms 66932 KB Output is correct
52 Correct 669 ms 60232 KB Output is correct
53 Correct 665 ms 65096 KB Output is correct
54 Correct 682 ms 65864 KB Output is correct
55 Correct 669 ms 68676 KB Output is correct