#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]) 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];
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[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 |
348 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 |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1404 ms |
174752 KB |
Output is correct |
2 |
Correct |
1363 ms |
207012 KB |
Output is correct |
3 |
Correct |
1394 ms |
206740 KB |
Output is correct |
4 |
Correct |
1452 ms |
207384 KB |
Output is correct |
5 |
Correct |
723 ms |
206820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
76 ms |
16588 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 |
348 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 |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |