#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
struct SGT {
int n;
vector<int> mx, mn;
vector<bool> lazy;
SGT(int _n) {
n = _n;
mx.resize(4 * n), mn.resize(4 * n, INF), lazy.resize(4 * n, 0);
}
void build(int node, int l, int r, vector<int> &a) {
if (l == r) mx[node] = a[l];
else {
int mid = (l + r) / 2;
build(2 * node, l, mid, a);
build(2 * node + 1, mid + 1, r, a);
mx[node] = max(mx[2 * node], mx[2 * node + 1]);
}
}
void push(int node) {
if (not lazy[node]) return;
mn[2 * node] = mn[2 * node + 1] = mn[node];
lazy[node] = 0;
}
void update(int node, int l, int r, int L, int R, int x) {
if (R < l or r < L) return;
else if (L <= l and r <= R) {
mn[node] = x;
lazy[node] = 1;
} else {
push(node);
int mid = (l + r) / 2;
update(2 * node, l, mid, L, R, x);
update(2 * node + 1, mid + 1, r, L, R, x);
mn[node] = min(mn[2 * node], mn[2 * node + 1]);
}
}
int query(int node, int l, int r, int L, int R) {
if (R < l or r < L) return 0;
else if (L <= l and r <= R) return mx[node] - mn[node];
else {
push(node);
int mid = (l + r) / 2;
return max(query(2 * node, l, mid, L, R), query(2 * node + 1, mid + 1, r, L, R));
}
}
};
int n, m;
vector<int> a, ans;
vector<pair<int, int>> q;
vector<vector<int>> r2q;
vector<int> mx, mn, lazy;
void solve() {
SGT sgt = SGT(n);
sgt.build(1, 0, n - 1, a);
stack<int> st;
ans.resize(m);
for (int r = 0; r < n; r++) {
while (not st.empty() and a[st.top()] > a[r]) st.pop();
sgt.update(1, 0, n - 1, (st.empty() ? 0 : st.top() + 1), r, a[r]);
st.push(r);
for (int qi : r2q[r]) {
auto [l, k] = q[qi];
ans[qi] = (k >= sgt.query(1, 0, n - 1, l, r));
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
a.resize(n);
for (int &x : a) {
cin >> x;
}
q.resize(m), r2q.resize(n);
for (int i = 0; i < m; i++) {
int l, r, k;
cin >> l >> r >> k;
l--, r--;
q[i] = make_pair(l, k);
r2q[r].push_back(i);
}
solve();
for (int i = 0; i < m; i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1429 ms |
121264 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
76 ms |
11100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |