Submission #1267862

#TimeUsernameProblemLanguageResultExecution timeMemory
1267862kawhietHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++20
0 / 100
1052 ms49208 KiB
#include <bits/stdc++.h>
using namespace std;

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

    SegmentTree(int sz) {
        n = sz;
        st.resize(4 * n);
    }

    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 0LL;
        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;
    }
    vector<pair<int, int>> v;
    stack<pair<int, int>> st;
    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();
            v.push_back({j, x + a[i]});
        }
        st.push({a[i], i});
    }
    sort(v.begin(), v.end(), [&](pair<int, int> x, pair<int, int> y) {
        return x.second > y.second;
    });
    sort(qs.begin(), qs.end(), [&](Query x, Query y) {
        return x.k > y.k;
    });
    vector<int> res(m);
    for (int i = 0; i < m; i++) {
        auto [l, r, k, id] = qs[i];
        while (!v.empty() && v.back().second > k) {
            t.update(v.back().first, 1);
            v.pop_back();
        }
        res[id] = (t.get(l, r) ? 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...