/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) ((1ll << (x)))
#define debug(a, l, r) for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';
const int INF = 1e18 + 5;
struct SegmentTree{
int n; vector<int> st;
SegmentTree() {
}
SegmentTree(int n) : n(n) {
st.resize(4 * n + 5, 0ll);
}
void update(int id, int l, int r, int pos, int val) {
if (l > pos || r < pos) return;
if (l == r) {
st[id] = max(st[id], val);
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, pos, val);
update((id << 1) | 1, mid + 1, r, pos, val);
st[id] = max(st[id << 1], st[(id << 1) | 1]);
}
int get(int id, int l, int r, int u, int v) {
if (l > v || r < u) return -INF;
if (l >= u && r <= v) return st[id];
int mid = (l + r) >> 1;
int get1 = get(id << 1, l, mid, u, v);
int get2 = get((id << 1) | 1, mid + 1, r, u, v);
return max(get1, get2);
}
void update(int l, int r) {
update(1, 1, n, l, r);
}
int get(int l, int r) {
return get(1, 1, n, l, r);
}
};
struct Query {
int l, k, id;
};
const int MAXN = 1e6 + 6;
int n, q, a[MAXN], l[MAXN];
vector<Query> queries[MAXN];
void init() {
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= q; ++i) {
int l, r, k; cin >> l >> r >> k;
queries[r].push_back({l, k, i});
}
vector<int> stk; // Vector-based stack
for (int i = 1; i <= n; ++i) {
while (!stk.empty() && a[stk.back()] <= a[i]) stk.pop_back();
l[i] = (stk.empty() ? 0 : stk.back());
stk.push_back(i);
}
}
bitset<MAXN> ans;
void solve() {
SegmentTree seg(n + 5);
for (int r = 1; r <= n; ++r) {
if (l[r] != 0) seg.update(l[r], a[r] + a[l[r]]);
for (auto &qry : queries[r]) {
int l = qry.l, k = qry.k, id = qry.id;
int mxval = seg.get(l, r);
ans[id] = (mxval <= k);
}
}
for (int i = 1; i <= q; ++i) cout << ans[i] << '\n';
}
signed main() {
#ifdef NCTHANH
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
init();
solve();
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... |