#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct state {
set<int> contained;
int max = 0, lazy = 0;
void push(state &l, state &r) {
l.lazyMax(lazy);
r.lazyMax(lazy);
lazy = 0;
}
void apply(int x) {
lazyMax(x);
}
void lazyMax(int x) {
if (x > lazy) {
lazy = x;
set<int>::iterator lower = contained.lower_bound(x);
if (lower != contained.end() && lower-- != contained.begin()) {
max = std::max(max, *lower + x);
}
}
}
};
class lazyseg {
public:
int n;
vector<state> a;
lazyseg(int n, const vector<int> &books) : n(n), a(4 * n) {
build(0, n - 1, 1, books);
}
void build(int l, int r, int v, const vector<int> &books) {
a[v].contained.insert(books.begin() + l, books.begin() + r + 1);
if (l != r) {
int m = (l + r) / 2;
build(l, m, 2 * v, books);
build(m + 1, r, 2 * v + 1, books);
a[v].max = max(a[2 * v].max, a[2 * v + 1].max);
}
}
void upd(int ll, int rr, int l, int r, int v, int x) {
if (r < ll || rr < l) return;
else if (ll <= l && r <= rr) a[v].apply(x);
else {
a[v].push(a[2 * v], a[2 * v + 1]);
int m = (l + r) / 2;
upd(ll, rr, l, m, 2 * v, x);
upd(ll, rr, m + 1, r, 2 * v + 1, x);
a[v].max = max(a[2 * v].max, a[2 * v + 1].max);
}
}
int qry(int ll, int rr, int l, int r, int v) {
if (r < ll || rr < l) return 0;
else if (ll <= l && r <= rr) return a[v].max;
else {
a[v].push(a[2 * v], a[2 * v + 1]);
int m = (l + r) / 2;
return max(qry(ll, rr, l, m, 2 * v), qry(ll, rr, m + 1, r, 2 * v + 1));
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> books(n);
for (int &i : books) cin >> i;
lazyseg tree(n, books);
vector<array<int, 5>> queries;
queries.reserve(q);
for (int i = 0; i < q; i++) {
int l, r, k;
cin >> l >> r >> k;
l--, r--;
queries.push_back({l, r, k, i, -1});
}
sort(queries.begin(), queries.end(), [](const array<int, 5> &l, const array<int, 5> &r) {
return l[0] < r[0];
});
int ptr = q - 1;
for (int i = n - 1; i >= 0; i--) {
tree.upd(i, n - 1, 0, n - 1, 1, books[i]);
while (ptr >= 0 && queries[ptr][0] == i) {
queries[ptr][4] = tree.qry(i, queries[ptr][1], 0, n - 1, 1);
ptr--;
}
}
sort(queries.begin(), queries.end(), [](const array<int, 5> &l, const array<int, 5> &r) {
return l[3] < r[3];
});
for (int i = 0; i < q; i++) {
cout << "01"[queries[i][4] <= queries[i][2]] << '\n';
// cout << queries[i][4] << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |