#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int inf = 1e9;
const int lg = 20;
template<typename T> struct sparse_table {
vector<vector<T>> rmq;
T calc(T a, T b) {
return std::max(a, b);
}
void build(vector<T> &a) {
int n = a.size();
int ln = 31 - __builtin_clz(n);
if ((1 << ln) < n) {
ln++;
}
rmq.assign(ln + 1, vector<T>(n));
for (int j = 0; j <= ln; j++) {
for (int i = 0; i <= n - (1 << j); i++) {
if (j > 0) {
rmq[j][i] = calc(rmq[j - 1][i], rmq[j - 1][i + (1 << (j - 1))]);
} else {
rmq[j][i] = a[i];
}
}
}
}
T get(int l, int r) {
assert(l <= r);
int x = 31 - __builtin_clz(r - l + 1);
return calc(rmq[x][l], rmq[x][r - (1 << x) + 1]);
}
};
void solve() {
int n, q;
cin >> n >> q;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
stack<int> st;
vector jump(lg, vector(n + 1, 0));
for (int i = 1; i <= n; i++) {
while (!st.empty() and a[st.top()] <= a[i]) st.pop();
jump[0][i] = st.empty() ? 0 : st.top();
st.push(i);
}
for (int i = 1; i <= n; i++) {
if (!jump[0][i]) continue;
for (int j = 1; j < lg; j++) {
jump[j][i] = jump[j - 1][jump[j - 1][i]];
if (!jump[j][i]) break;
}
}
vector<int> left(n + 1);
for (int i = 1; i <= n; i++) left[i] = jump[0][i];
sparse_table<int> sp; sp.build(left);
while (q--) {
int l, r, k;
cin >> l >> r >> k;
int lx = l, rx = r, best = 0;
while (lx <= rx) {
int m = (lx + rx) >> 1;
if (sp.get(m, r) >= l) {
best = m;
lx = m + 1;
} else {
rx = m - 1;
}
}
r = best;
int ans = 0;
for (int i = lg - 1; i >= 0; i--) {
if (!jump[i][r] or jump[0][jump[i][r]] < l) continue;
r = jump[i][r];
}
if (jump[0][r] >= l) {
ans = a[jump[0][r]] + a[r];
}
cout << (int) (ans <= k) << '\n';
}
}
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int queries = 1;
// cin >> queries;
for (int test_case = 1; test_case <= queries; test_case++) {
#ifdef sunnatov
cout << "Test case: " << test_case << endl;
#endif
solve();
cout << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1436 ms |
174860 KB |
Output is correct |
2 |
Correct |
1482 ms |
174804 KB |
Output is correct |
3 |
Correct |
1434 ms |
174504 KB |
Output is correct |
4 |
Correct |
1430 ms |
174476 KB |
Output is correct |
5 |
Correct |
711 ms |
174528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
65 ms |
16592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |