/ SeriousOJ /

Record Detail

Compile Error

foo.cc: In member function 'SegmentTree::Node SegmentTree::query(int, int, int, int, int)':
foo.cc:58:21: error: 'INT_MAX' was not declared in this scope
   58 |             return {INT_MAX, INT_MIN, true};
      |                     ^~~~~~~
foo.cc:3:1: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
    2 | #include <vector>
  +++ |+#include <climits>
    3 | using namespace std;
foo.cc:58:30: error: 'INT_MIN' was not declared in this scope
   58 |             return {INT_MAX, INT_MIN, true};
      |                              ^~~~~~~
foo.cc:58:30: note: 'INT_MIN' is defined in header '<climits>'; did you forget to '#include <climits>'?
foo.cc:58:43: error: could not convert '{<expression error>, <expression error>, true}' from '<brace-enclosed initializer list>' to 'SegmentTree::Node'
   58 |             return {INT_MAX, INT_MIN, true};
      |                                           ^
      |                                           |
      |                                           <brace-enclosed initializer list>

Code

#include <iostream>
#include <vector>
using namespace std;

class SegmentTree {
public:
    SegmentTree(const vector<int>& data) {
        n = data.size();
        tree.resize(4 * n);
        build(data, 0, 0, n - 1);
    }

    void update(int idx, int value) {
        update(idx, value, 0, 0, n - 1);
    }

    bool range_query(int l, int r) {
        return query(l, r, 0, 0, n - 1).sorted;
    }

private:
    struct Node {
        int min_val;
        int max_val;
        bool sorted;
    };

    vector<Node> tree;
    int n;

    void build(const vector<int>& data, int node, int start, int end) {
        if (start == end) {
            tree[node] = {data[start], data[start], true};
        } else {
            int mid = (start + end) / 2;
            build(data, 2 * node + 1, start, mid);
            build(data, 2 * node + 2, mid + 1, end);
            tree[node] = merge(tree[2 * node + 1], tree[2 * node + 2]);
        }
    }

    void update(int idx, int value, int node, int start, int end) {
        if (start == end) {
            tree[node] = {value, value, true};
        } else {
            int mid = (start + end) / 2;
            if (start <= idx && idx <= mid) {
                update(idx, value, 2 * node + 1, start, mid);
            } else {
                update(idx, value, 2 * node + 2, mid + 1, end);
            }
            tree[node] = merge(tree[2 * node + 1], tree[2 * node + 2]);
        }
    }

    Node query(int l, int r, int node, int start, int end) {
        if (r < start || end < l) {
            return {INT_MAX, INT_MIN, true};
        }
        if (l <= start && end <= r) {
            return tree[node];
        }
        int mid = (start + end) / 2;
        Node left_result = query(l, r, 2 * node + 1, start, mid);
        Node right_result = query(l, r, 2 * node + 2, mid + 1, end);
        return merge(left_result, right_result);
    }

    Node merge(const Node& left, const Node& right) {
        if (left.sorted && right.sorted && left.max_val <= right.min_val) {
            return {left.min_val, right.max_val, true};
        } else {
            return {min(left.min_val, right.min_val), max(left.max_val, right.max_val), false};
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, Q;
    cin >> N >> Q;
    vector<int> array(N);
    for (int i = 0; i < N; ++i) {
        cin >> array[i];
    }

    SegmentTree st(array);

    while (Q--) {
        int type;
        cin >> type;
        if (type == 1) {
            int i, x;
            cin >> i >> x;
            st.update(i - 1, x);
        } else if (type == 2) {
            int l, r;
            cin >> l >> r;
            if (st.range_query(l - 1, r - 1)) {
                cout << "YES\n";
            } else {
                cout << "NO\n";
            }
        }
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1085 Sorted or !Sorted
Language
C++17 (G++ 13.2.0)
Submit At
2024-09-05 10:51:01
Judged At
2024-11-11 03:04:15
Judged By
Score
0
Total Time
0ms
Peak Memory
0 Bytes