#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int inf = 1e9;
const int lg = 20;
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]) break;
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] ? i : left[i - 1];
while (q--) {
int l, r, k;
cin >> l >> r >> k;
r = left[r];
int ans = 0;
for (int i = lg - 1; i >= 0; i--) {
if (!jump[i][r] or jump[i][r] < l) continue;
ans = a[jump[i][r]] + (i > 0 ? a[jump[i - 1][r]] : a[r]);
r = jump[i][r];
}
if (!jump[0][r] and 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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
963 ms |
119792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
11112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |