Submission #894300

# Submission time Handle Problem Language Result Execution time Memory
894300 2023-12-28T04:14:18 Z Olympia Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
77 / 100
3000 ms 221840 KB
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <cassert>
#include <algorithm>

using namespace std;

struct Node {
    int mx;
    int val;
    vector<int> v;
    Node () {
        this->mx = -INT_MAX, this->val = 0;
    }
    Node(int mx) {
        this->mx = mx, this->val = 0;
    }
} vec[2 * ((1 << 20) + 7)];

struct SegmentTree {
    vector<int> nodes;
    int sz;

    Node merge(Node &left, Node &right) {
        Node ans = {max(left.mx, right.mx)};
        ans.val = max(left.val, right.val);
        if (right.mx < left.mx) {
            ans.val = max(ans.val, left.mx + right.mx);
        } else if (right.v[0] < left.mx) {
            ans.val = max(ans.val, *(--lower_bound(right.v.begin(), right.v.end(), left.mx)) + left.mx);
        }
        return ans;
    }

    int query(int l, int r) {
        r += 1;
        vector<int> vec1, vec2;
        for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
            if (l & 1) {
                vec1.push_back(l++);
            }
            if (r & 1) {
                vec2.push_back(--r);
            }
        }
        reverse(vec2.begin(), vec2.end());
        for (int x: vec2) {
            vec1.push_back(x);
        }
        Node ans = vec[vec1[0]];
        for (int i = 1; i < vec1.size(); i++) {
            ans = merge(ans, vec[vec1[i]]);
        }
        return ans.val;
    }

    void build(int dum, int tl, int tr) {
        if (tl == tr) {
            vec[dum] = Node(nodes[tl]);
            vec[dum].v = {nodes[tl]};
        } else {
            build(2 * dum, tl, (tl + tr) / 2), build(2 * dum + 1, (tl + tr) / 2 + 1, tr);
            vec[dum] = merge(vec[2 * dum], vec[2 * dum + 1]);
            std::merge(vec[2 * dum].v.begin(), vec[2 * dum].v.end(),
                       vec[2 * dum + 1].v.begin(), vec[2 * dum + 1].v.end(),
                       std::back_inserter(vec[dum].v));
        }
    }

    SegmentTree(vector<int> nodes) {
        this->nodes = nodes;
        int n = nodes.size();
        n = (1 << (int) log2(n)) * 2;
        this->sz = n;
        this->nodes.reserve(2 * sz);
        while (this->nodes.size() < 2 * sz) {
            this->nodes.push_back((int) 1e9);
        }
        build(1, 0, sz - 1);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, q;
    cin >> n >> q;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }
    SegmentTree st(v);
    while (q--) {
        int l, r, k;
        cin >> l >> r >> k;
        l--, r--;
        cout << (st.query(l, r) <= k) << '\n';
    }
}

Compilation message

sortbooks.cpp: In member function 'int SegmentTree::query(int, int)':
sortbooks.cpp:55:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |         for (int i = 1; i < vec1.size(); i++) {
      |                         ~~^~~~~~~~~~~~~
sortbooks.cpp: In constructor 'SegmentTree::SegmentTree(std::vector<int>)':
sortbooks.cpp:80:35: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   80 |         while (this->nodes.size() < 2 * sz) {
      |                ~~~~~~~~~~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 65884 KB Output is correct
2 Correct 15 ms 65884 KB Output is correct
3 Correct 15 ms 66140 KB Output is correct
4 Correct 15 ms 65948 KB Output is correct
5 Correct 15 ms 66140 KB Output is correct
6 Correct 16 ms 66176 KB Output is correct
7 Correct 15 ms 66140 KB Output is correct
8 Correct 16 ms 66140 KB Output is correct
9 Correct 15 ms 66136 KB Output is correct
10 Correct 15 ms 66136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 65884 KB Output is correct
2 Correct 15 ms 65884 KB Output is correct
3 Correct 15 ms 66140 KB Output is correct
4 Correct 15 ms 65948 KB Output is correct
5 Correct 15 ms 66140 KB Output is correct
6 Correct 16 ms 66176 KB Output is correct
7 Correct 15 ms 66140 KB Output is correct
8 Correct 16 ms 66140 KB Output is correct
9 Correct 15 ms 66136 KB Output is correct
10 Correct 15 ms 66136 KB Output is correct
11 Correct 17 ms 66140 KB Output is correct
12 Correct 19 ms 66908 KB Output is correct
13 Correct 20 ms 67028 KB Output is correct
14 Correct 21 ms 66984 KB Output is correct
15 Correct 21 ms 66908 KB Output is correct
16 Correct 20 ms 66984 KB Output is correct
17 Correct 20 ms 66904 KB Output is correct
18 Correct 21 ms 66908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2195 ms 221724 KB Output is correct
2 Correct 2181 ms 221560 KB Output is correct
3 Correct 2277 ms 221668 KB Output is correct
4 Correct 2228 ms 221540 KB Output is correct
5 Correct 1798 ms 221752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 83776 KB Output is correct
2 Correct 172 ms 83660 KB Output is correct
3 Correct 143 ms 83660 KB Output is correct
4 Correct 143 ms 83592 KB Output is correct
5 Correct 141 ms 83716 KB Output is correct
6 Correct 120 ms 83792 KB Output is correct
7 Correct 123 ms 83780 KB Output is correct
8 Correct 336 ms 83804 KB Output is correct
9 Correct 61 ms 66384 KB Output is correct
10 Correct 351 ms 83656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 65884 KB Output is correct
2 Correct 15 ms 65884 KB Output is correct
3 Correct 15 ms 66140 KB Output is correct
4 Correct 15 ms 65948 KB Output is correct
5 Correct 15 ms 66140 KB Output is correct
6 Correct 16 ms 66176 KB Output is correct
7 Correct 15 ms 66140 KB Output is correct
8 Correct 16 ms 66140 KB Output is correct
9 Correct 15 ms 66136 KB Output is correct
10 Correct 15 ms 66136 KB Output is correct
11 Correct 17 ms 66140 KB Output is correct
12 Correct 19 ms 66908 KB Output is correct
13 Correct 20 ms 67028 KB Output is correct
14 Correct 21 ms 66984 KB Output is correct
15 Correct 21 ms 66908 KB Output is correct
16 Correct 20 ms 66984 KB Output is correct
17 Correct 20 ms 66904 KB Output is correct
18 Correct 21 ms 66908 KB Output is correct
19 Correct 382 ms 102416 KB Output is correct
20 Correct 393 ms 102528 KB Output is correct
21 Correct 326 ms 102460 KB Output is correct
22 Correct 326 ms 102456 KB Output is correct
23 Correct 326 ms 102320 KB Output is correct
24 Correct 269 ms 102476 KB Output is correct
25 Correct 267 ms 102436 KB Output is correct
26 Correct 305 ms 102448 KB Output is correct
27 Correct 323 ms 102460 KB Output is correct
28 Correct 328 ms 102464 KB Output is correct
29 Correct 329 ms 102464 KB Output is correct
30 Correct 321 ms 102464 KB Output is correct
31 Correct 333 ms 102668 KB Output is correct
32 Correct 329 ms 102504 KB Output is correct
33 Correct 327 ms 102564 KB Output is correct
34 Correct 253 ms 102460 KB Output is correct
35 Correct 252 ms 102464 KB Output is correct
36 Correct 252 ms 102464 KB Output is correct
37 Correct 257 ms 102496 KB Output is correct
38 Correct 255 ms 102468 KB Output is correct
39 Correct 1004 ms 102616 KB Output is correct
40 Correct 1619 ms 101964 KB Output is correct
41 Correct 1202 ms 102468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 65884 KB Output is correct
2 Correct 15 ms 65884 KB Output is correct
3 Correct 15 ms 66140 KB Output is correct
4 Correct 15 ms 65948 KB Output is correct
5 Correct 15 ms 66140 KB Output is correct
6 Correct 16 ms 66176 KB Output is correct
7 Correct 15 ms 66140 KB Output is correct
8 Correct 16 ms 66140 KB Output is correct
9 Correct 15 ms 66136 KB Output is correct
10 Correct 15 ms 66136 KB Output is correct
11 Correct 17 ms 66140 KB Output is correct
12 Correct 19 ms 66908 KB Output is correct
13 Correct 20 ms 67028 KB Output is correct
14 Correct 21 ms 66984 KB Output is correct
15 Correct 21 ms 66908 KB Output is correct
16 Correct 20 ms 66984 KB Output is correct
17 Correct 20 ms 66904 KB Output is correct
18 Correct 21 ms 66908 KB Output is correct
19 Correct 2195 ms 221724 KB Output is correct
20 Correct 2181 ms 221560 KB Output is correct
21 Correct 2277 ms 221668 KB Output is correct
22 Correct 2228 ms 221540 KB Output is correct
23 Correct 1798 ms 221752 KB Output is correct
24 Correct 183 ms 83776 KB Output is correct
25 Correct 172 ms 83660 KB Output is correct
26 Correct 143 ms 83660 KB Output is correct
27 Correct 143 ms 83592 KB Output is correct
28 Correct 141 ms 83716 KB Output is correct
29 Correct 120 ms 83792 KB Output is correct
30 Correct 123 ms 83780 KB Output is correct
31 Correct 336 ms 83804 KB Output is correct
32 Correct 61 ms 66384 KB Output is correct
33 Correct 351 ms 83656 KB Output is correct
34 Correct 382 ms 102416 KB Output is correct
35 Correct 393 ms 102528 KB Output is correct
36 Correct 326 ms 102460 KB Output is correct
37 Correct 326 ms 102456 KB Output is correct
38 Correct 326 ms 102320 KB Output is correct
39 Correct 269 ms 102476 KB Output is correct
40 Correct 267 ms 102436 KB Output is correct
41 Correct 305 ms 102448 KB Output is correct
42 Correct 323 ms 102460 KB Output is correct
43 Correct 328 ms 102464 KB Output is correct
44 Correct 329 ms 102464 KB Output is correct
45 Correct 321 ms 102464 KB Output is correct
46 Correct 333 ms 102668 KB Output is correct
47 Correct 329 ms 102504 KB Output is correct
48 Correct 327 ms 102564 KB Output is correct
49 Correct 253 ms 102460 KB Output is correct
50 Correct 252 ms 102464 KB Output is correct
51 Correct 252 ms 102464 KB Output is correct
52 Correct 257 ms 102496 KB Output is correct
53 Correct 255 ms 102468 KB Output is correct
54 Correct 1004 ms 102616 KB Output is correct
55 Correct 1619 ms 101964 KB Output is correct
56 Correct 1202 ms 102468 KB Output is correct
57 Correct 2154 ms 221508 KB Output is correct
58 Correct 2197 ms 221840 KB Output is correct
59 Correct 2140 ms 221508 KB Output is correct
60 Correct 2153 ms 221452 KB Output is correct
61 Correct 2243 ms 221504 KB Output is correct
62 Correct 2217 ms 221456 KB Output is correct
63 Correct 1261 ms 221448 KB Output is correct
64 Correct 1280 ms 221616 KB Output is correct
65 Correct 1884 ms 221456 KB Output is correct
66 Correct 1792 ms 221504 KB Output is correct
67 Correct 1970 ms 221456 KB Output is correct
68 Correct 1827 ms 221712 KB Output is correct
69 Correct 1842 ms 221536 KB Output is correct
70 Correct 1818 ms 221508 KB Output is correct
71 Correct 1874 ms 221456 KB Output is correct
72 Correct 1803 ms 221712 KB Output is correct
73 Correct 1249 ms 221452 KB Output is correct
74 Correct 1265 ms 221508 KB Output is correct
75 Correct 1263 ms 221456 KB Output is correct
76 Correct 1244 ms 221776 KB Output is correct
77 Correct 1276 ms 221456 KB Output is correct
78 Execution timed out 3050 ms 221456 KB Time limit exceeded
79 Halted 0 ms 0 KB -