Submission #1266826

#TimeUsernameProblemLanguageResultExecution timeMemory
1266826islam_2010Hedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++20
100 / 100
1850 ms237784 KiB
#include <bits/stdc++.h>
using namespace std;

const int sz = 1e6 + 5;

int n, q;
int a[sz];
vector<int> st[sz << 2];
int mx[sz << 2];

void build(int l, int r, int in) {
    mx[in] = 0;
    if (l == r) {
        st[in].push_back(a[l]);
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, in << 1);
    build(mid + 1, r, in << 1 | 1);

    int i = 0, j = 0, k = 0;
    st[in].resize(st[in << 1].size() + st[in << 1 | 1].size());
    while (i < (int)st[in << 1].size() && j < (int)st[in << 1 | 1].size()) {
        if (st[in << 1][i] < st[in << 1 | 1][j]) st[in][k++] = st[in << 1][i++];
        else st[in][k++] = st[in << 1 | 1][j++];
    }
    while (i < (int)st[in << 1].size()) st[in][k++] = st[in << 1][i++];
    while (j < (int)st[in << 1 | 1].size()) st[in][k++] = st[in << 1 | 1][j++];

    if (!st[in << 1].empty() && !st[in << 1 | 1].empty() && st[in << 1].back() > st[in << 1 | 1][0]) {
        auto it = lower_bound(st[in << 1 | 1].begin(), st[in << 1 | 1].end(), st[in << 1].back());
        if (it != st[in << 1 | 1].begin()) {
            --it;
            mx[in] = st[in << 1].back() + *it;
        }
    }
    mx[in] = max({mx[in], mx[in << 1], mx[in << 1 | 1]});
}

vector<int> nodes;

void get_nodes(int l, int r, int in, int ql, int qr) {
    if (l > qr || r < ql) return;
    if (l >= ql && r <= qr) {
        nodes.push_back(in);
        return;
    }
    int mid = (l + r) >> 1;
    get_nodes(l, mid, in << 1, ql, qr);
    get_nodes(mid + 1, r, in << 1 | 1, ql, qr);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> a[i];
    build(1, n, 1);

    while (q--) {
        nodes.clear();
        int L, R, x;
        cin >> L >> R >> x;
        get_nodes(1, n, 1, L, R);

        int ans = 0, cur = 0;
        for (int idx : nodes) ans = max(ans, mx[idx]);

        for (int i = 0; i + 1 < (int)nodes.size(); i++) {
            int l = nodes[i], r = nodes[i + 1];
            cur = max(cur, st[l].back());
            if (cur > st[r][0]) {
                auto it = lower_bound(st[r].begin(), st[r].end(), cur);
                if (it != st[r].begin()) {
                    --it;
                    ans = max(ans, cur + *it);
                }
            }
        }

        cout << (ans <= x) << '\n';
    }
}
#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...