#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MXN = 1e6 + 5;
int segtree[MXN << 2], A[MXN];
void build(const int &v, const int &tl, const int &tr) {
if (tl == tr)
segtree[v] = A[tl];
else {
int tm = (tl + tr) >> 1;
build(v << 1, tl, tm);
build(v << 1 | 1, tm + 1, tr);
segtree[v] = max(segtree[v << 1], segtree[v << 1 | 1]);
}
}
void update(const int &v, const int &tl, const int &tr, const int &pos, const int &target) {
if (tl == tr)
segtree[v] = max(segtree[v], target);
else {
int tm = (tl + tr) >> 1;
if (pos > tm)
update(v << 1 | 1, tm + 1, tr, pos, target);
else
update(v << 1, tl, tm, pos, target);
segtree[v] = max(segtree[v << 1], segtree[v << 1 | 1]);
}
}
int getans(const int &v, const int &tl, const int &tr, const int &l, const int &r) {
if (l > r)
return 0;
if (l == tl && r == tr)
return segtree[v];
int tm = (tl + tr) >> 1;
return max(getans(v << 1, tl, tm, l, min(r, tm)), getans(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r));
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
for (int i = 1; i <= N; i++)
cin >> A[i];
vector<array<int, 4>> Q(M);
vector<int> res(M);
for (int i = 0; i < M; i++)
cin >> Q[i][0] >> Q[i][1] >> Q[i][2], Q[i][3] = i;
sort(Q.begin(), Q.end(), [&](const array<int, 4> &a, const array<int, 4> &b) -> bool {
return a[1] < b[1];
});
stack<array<int, 2>> stk;
int prev = Q[0][1];
stk.push({A[1], 1});
for (int i = 2; i <= prev; i++) {
if (stk.empty())
stk.push({A[i], i});
else {
while (!stk.empty() && stk.top()[0] <= A[i])
stk.pop();
if (!stk.empty())
update(1, 1, N, stk.top()[1], stk.top()[0] + A[i]);
stk.push({A[i], i});
}
}
for (int i = 0; i < M; i++) {
int L = Q[i][0], R = Q[i][1], K = Q[i][2], idx = Q[i][3];
if (R == prev) {
res[idx] = getans(1, 1, N, L, R) <= K;
continue;
}
for (int j = prev + 1; j <= R; j++) {
if (stk.empty())
stk.push({A[j], j});
else {
while (!stk.empty() && stk.top()[0] <= A[j])
stk.pop();
if (!stk.empty())
update(1, 1, N, stk.top()[1], stk.top()[0] + A[j]);
stk.push({A[j], j});
}
}
res[idx] = getans(1, 1, N, L, R) <= K;
prev = R;
}
for (int i = 0; i < M; i++)
cout << res[i] << '\n';
return 0;
}
# | 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... |