답안 #894274

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894274 2023-12-28T03:21:31 Z Olympia Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
77 / 100
3000 ms 234432 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 mn;
    int val;
    vector<int> v;
    Node (int mn, int mx) {
        this->mx = mx, this->mn = mn, this->val = 0;
    }
};
struct SegmentTree {
    vector<Node> vec;
    vector<int> nodes;
    Node merge (Node &left, Node &right) {
        Node ans = {min(left.mn, right.mn), 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.mn < left.mx) {
            int l = 0;
            int r = right.v.size() - 1;
            while (l != r) {
                int m = (l + r + 1)/2;
                if (right.v[m] < left.mx) {
                    l = m;
                } else {
                    r = m - 1;
                }
            }
            ans.val = max(ans.val, right.v[l] + left.mx);
        }
        return ans;
    }
    vector<int> intervals;
    void query (int dum, int tl, int tr, int l, int r) {
        if (tl >= l && tr <= r) {
            intervals.push_back(dum);
        } else if (tl <= r && tr >= l) {
            query(2 * dum + 1, tl, (tl + tr)/2,l, r), query(2 * dum + 2, (tl + tr)/2 + 1, tr, l, r);
        }
    }
    int query (int l, int r) {
        intervals.clear();
        query(0, 0, (int)vec.size()/2 - 1, l, r);
        Node ans = vec[intervals[0]];
        for (int i = 1; i < intervals.size(); i++) {
            ans = merge(ans, vec[intervals[i]]);
        }
        return ans.val;
    }
    void build (int dum, int tl, int tr) {
        if (tl == tr) {
            vec[dum] = Node(nodes[tl], nodes[tl]);
            vec[dum].v = {nodes[tl]};
        } else {
            build(2 * dum + 1, tl, (tl + tr)/2), build(2 * dum + 2, (tl + tr)/2 + 1, tr);
            vec[dum] = merge(vec[2 * dum + 1], vec[2 * dum + 2]);
            vec[dum].v.reserve(vec[2 * dum + 1].v.size() + vec[2 * dum + 2].v.size());
            std::merge(vec[2 * dum + 1].v.begin(), vec[2 * dum + 1].v.end(),
                       vec[2 * dum + 2].v.begin(), vec[2 * dum + 2].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;
        vec.assign(2 * n, Node(INT_MAX, -INT_MAX));
        this->nodes.reserve(vec.size());
        while (this->nodes.size() < vec.size()) {
            this->nodes.push_back((int)1e9);
        }
        build(0, 0, (int)vec.size()/2 - 1);
    }
};

inline char getChar() {
    static char buf[1 << 16];
    static int ps = 0, sz = 0;
    if (ps == sz) {
        ps = 0;
        sz = fread(buf, sizeof(buf[0]), sizeof(buf) / sizeof(buf[0]), stdin);
    }
    return buf[ps++];
}

inline int ni() {
    char c = getChar();
    for (; c == '\n' || c == ' ';) c = getChar();
    int o = 0;
    for (; c != ' ' && c != '\n'; c = getChar()) o = o * 10 + c - '0';
    return o;
}

inline void putChar(int c) {
    static char buf[1 << 16];
    static int pos = 0;
    if (c == EOF || pos == 1 << 16) {
        fwrite(buf, sizeof(buf[0]), pos, stdout);
        pos = 0;
    }
    if (c == EOF) return;
    buf[pos++] = c;
}

inline void printInt(int val) {
    static char buf[12];
    int pos = 0, sign = val < 0 ? -1 : 1;
    val = abs(val);
    if (val == 0) buf[pos++] = 48;
    for (; val; val /= 10) buf[pos++] = 48 + val % 10;
    if (sign == -1) buf[pos++] = '-';
    buf[pos] = '\n';
    for (; pos >= 0;) putChar(buf[pos--]);
}

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

Compilation message

sortbooks.cpp: In member function 'int SegmentTree::query(int, int)':
sortbooks.cpp:54:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |         for (int i = 1; i < intervals.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 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 2 ms 856 KB Output is correct
12 Correct 3 ms 1884 KB Output is correct
13 Correct 3 ms 1884 KB Output is correct
14 Correct 4 ms 1884 KB Output is correct
15 Correct 4 ms 1884 KB Output is correct
16 Correct 4 ms 1884 KB Output is correct
17 Correct 4 ms 2132 KB Output is correct
18 Correct 4 ms 2120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1755 ms 234136 KB Output is correct
2 Correct 1707 ms 233832 KB Output is correct
3 Correct 1788 ms 234036 KB Output is correct
4 Correct 1747 ms 234312 KB Output is correct
5 Correct 1083 ms 234096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 27860 KB Output is correct
2 Correct 111 ms 27860 KB Output is correct
3 Correct 93 ms 28044 KB Output is correct
4 Correct 91 ms 27952 KB Output is correct
5 Correct 77 ms 27860 KB Output is correct
6 Correct 64 ms 28192 KB Output is correct
7 Correct 67 ms 27972 KB Output is correct
8 Correct 291 ms 28156 KB Output is correct
9 Correct 21 ms 860 KB Output is correct
10 Correct 273 ms 27972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 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 2 ms 856 KB Output is correct
12 Correct 3 ms 1884 KB Output is correct
13 Correct 3 ms 1884 KB Output is correct
14 Correct 4 ms 1884 KB Output is correct
15 Correct 4 ms 1884 KB Output is correct
16 Correct 4 ms 1884 KB Output is correct
17 Correct 4 ms 2132 KB Output is correct
18 Correct 4 ms 2120 KB Output is correct
19 Correct 270 ms 56388 KB Output is correct
20 Correct 268 ms 56572 KB Output is correct
21 Correct 221 ms 56384 KB Output is correct
22 Correct 218 ms 56948 KB Output is correct
23 Correct 216 ms 56388 KB Output is correct
24 Correct 141 ms 56648 KB Output is correct
25 Correct 147 ms 56964 KB Output is correct
26 Correct 164 ms 56424 KB Output is correct
27 Correct 182 ms 56628 KB Output is correct
28 Correct 185 ms 56396 KB Output is correct
29 Correct 188 ms 56384 KB Output is correct
30 Correct 186 ms 56396 KB Output is correct
31 Correct 200 ms 56384 KB Output is correct
32 Correct 181 ms 56384 KB Output is correct
33 Correct 204 ms 56388 KB Output is correct
34 Correct 136 ms 56384 KB Output is correct
35 Correct 137 ms 56488 KB Output is correct
36 Correct 136 ms 56384 KB Output is correct
37 Correct 138 ms 56464 KB Output is correct
38 Correct 146 ms 56380 KB Output is correct
39 Correct 895 ms 56592 KB Output is correct
40 Correct 1432 ms 56428 KB Output is correct
41 Correct 1106 ms 56804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 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 2 ms 856 KB Output is correct
12 Correct 3 ms 1884 KB Output is correct
13 Correct 3 ms 1884 KB Output is correct
14 Correct 4 ms 1884 KB Output is correct
15 Correct 4 ms 1884 KB Output is correct
16 Correct 4 ms 1884 KB Output is correct
17 Correct 4 ms 2132 KB Output is correct
18 Correct 4 ms 2120 KB Output is correct
19 Correct 1755 ms 234136 KB Output is correct
20 Correct 1707 ms 233832 KB Output is correct
21 Correct 1788 ms 234036 KB Output is correct
22 Correct 1747 ms 234312 KB Output is correct
23 Correct 1083 ms 234096 KB Output is correct
24 Correct 120 ms 27860 KB Output is correct
25 Correct 111 ms 27860 KB Output is correct
26 Correct 93 ms 28044 KB Output is correct
27 Correct 91 ms 27952 KB Output is correct
28 Correct 77 ms 27860 KB Output is correct
29 Correct 64 ms 28192 KB Output is correct
30 Correct 67 ms 27972 KB Output is correct
31 Correct 291 ms 28156 KB Output is correct
32 Correct 21 ms 860 KB Output is correct
33 Correct 273 ms 27972 KB Output is correct
34 Correct 270 ms 56388 KB Output is correct
35 Correct 268 ms 56572 KB Output is correct
36 Correct 221 ms 56384 KB Output is correct
37 Correct 218 ms 56948 KB Output is correct
38 Correct 216 ms 56388 KB Output is correct
39 Correct 141 ms 56648 KB Output is correct
40 Correct 147 ms 56964 KB Output is correct
41 Correct 164 ms 56424 KB Output is correct
42 Correct 182 ms 56628 KB Output is correct
43 Correct 185 ms 56396 KB Output is correct
44 Correct 188 ms 56384 KB Output is correct
45 Correct 186 ms 56396 KB Output is correct
46 Correct 200 ms 56384 KB Output is correct
47 Correct 181 ms 56384 KB Output is correct
48 Correct 204 ms 56388 KB Output is correct
49 Correct 136 ms 56384 KB Output is correct
50 Correct 137 ms 56488 KB Output is correct
51 Correct 136 ms 56384 KB Output is correct
52 Correct 138 ms 56464 KB Output is correct
53 Correct 146 ms 56380 KB Output is correct
54 Correct 895 ms 56592 KB Output is correct
55 Correct 1432 ms 56428 KB Output is correct
56 Correct 1106 ms 56804 KB Output is correct
57 Correct 1747 ms 234000 KB Output is correct
58 Correct 1791 ms 234216 KB Output is correct
59 Correct 1848 ms 234412 KB Output is correct
60 Correct 1726 ms 234192 KB Output is correct
61 Correct 1644 ms 234156 KB Output is correct
62 Correct 1688 ms 234408 KB Output is correct
63 Correct 705 ms 234184 KB Output is correct
64 Correct 690 ms 234184 KB Output is correct
65 Correct 1060 ms 234432 KB Output is correct
66 Correct 1069 ms 234184 KB Output is correct
67 Correct 1081 ms 234264 KB Output is correct
68 Correct 1090 ms 234092 KB Output is correct
69 Correct 1073 ms 234036 KB Output is correct
70 Correct 1102 ms 234240 KB Output is correct
71 Correct 1097 ms 234168 KB Output is correct
72 Correct 1080 ms 233876 KB Output is correct
73 Correct 715 ms 234404 KB Output is correct
74 Correct 737 ms 234176 KB Output is correct
75 Correct 725 ms 234308 KB Output is correct
76 Correct 735 ms 234368 KB Output is correct
77 Correct 733 ms 234200 KB Output is correct
78 Execution timed out 3064 ms 234004 KB Time limit exceeded
79 Halted 0 ms 0 KB -