제출 #1267868

#제출 시각아이디문제언어결과실행 시간메모리
1267868kawhietHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++20
100 / 100
1137 ms53328 KiB
#include <bits/stdc++.h>
using namespace std;

struct SegmentTree {
    int n;
    vector<int> st;

    SegmentTree(int sz) {
        n = sz;
        st.assign(4 * n, -1);
    }

    int merge(int x, int y) {
        return max(x, y);
    }

    void update(int id, int tl, int tr, int i, int v) {
        if (tl == tr) {
            st[id] = v;
            return;
        }
        int tm = (tl + tr) / 2, x = 2 * id + 1, y = x + 1;
        if (i <= tm) {
            update(x, tl, tm, i, v);
        }
        else {
            update(y, tm + 1, tr, i, v);
        }
        st[id] = merge(st[x], st[y]);
    }

    int get(int id, int tl, int tr, int l, int r) {
        if (r < tl || tr < l) return -1;
        if (l <= tl && tr <= r)
            return st[id];
        int tm = (tl + tr) / 2, x = 2 * id + 1, y = x + 1;
        return merge(get(x, tl, tm, l, r), get(y, tm + 1, tr, l, r));
    }

    void update(int i, int v) {
        update(0, 0, n - 1, i, v);
    }

    int get(int l, int r) {
        return get(0, 0, n - 1, l, r);
    }
};

struct Query {
    int l, r, k, id;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    SegmentTree t(n);
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<Query> qs(m);
    for (int i = 0; i < m; i++) {
        cin >> qs[i].l >> qs[i].r >> qs[i].k;
        qs[i].l--; qs[i].r--;
        qs[i].id = i;
    }
    stack<pair<int, int>> st;
    vector<array<int, 3>> b(n);
    for (int i = 0; i < n; i++) {
        while (!st.empty() && st.top().first <= a[i]) {
            st.pop();
        }
        if (!st.empty()) {
            auto [x, j] = st.top();
            b[i] = {j, i, x + a[i]};
        }
        st.push({a[i], i});
    }
    sort(b.begin(), b.end(), [&](array<int, 3> x, array<int, 3> y) {
        return x[2] > y[2];
    });
    sort(qs.begin(), qs.end(), [&](Query x, Query y) {
        return x.k > y.k;
    });
    int j = 0;
    vector<int> res(m);
    for (int i = 0; i < m; i++) {
        auto [l, r, k, id] = qs[i];
        while (j < n && b[j][2] > k) {
            t.update(b[j][1], b[j][0]);
            j++;
        }
        res[id] = (t.get(l, r) >= l ? 0 : 1);
    }
    for (int i = 0; i < m; i++) {
        cout << res[i] << '\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...