#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+3, maxm = 1e6+3, maxw = 1e9+3;
int n, m;
int w[maxn];
int mxsum[4*maxn];
set<int> els[4*maxn];
void build(int l, int r, int v) {
if (l == r) {
mxsum[v] = -1;
els[v].insert(-w[l]);
} else {
int mid = (l+r)/2;
build(l, mid, v*2);
build(mid+1, r, v*2+1);
auto it = els[v*2+1].upper_bound(*els[v*2].begin());
mxsum[v] = max({mxsum[v*2], mxsum[v*2+1], (it == els[v*2+1].end() ? -1 : -*els[v*2].begin() - *it)});
for (int x : els[v*2]) els[v].insert(x);
for (int x : els[v*2+1]) els[v].insert(x);
}
}
int query2(int l, int r, int v, int lb, int rb) {
if (r < lb || l > rb) return -1;
if (l >= lb && r <= rb) return -*els[v].begin();
int mid = (l+r)/2;
return max(query2(l, mid, v*2, lb, rb), query2(mid+1, r, v*2+1, lb, rb));
}
int query3(int l, int r, int v, int lb, int rb, int k) {
if (r < lb || l > rb) return -1;
if (l >= lb && r <= rb) {
auto it = els[v].upper_bound(-k);
return (it == els[v].end() ? -1 : -*it);
}
int mid = (l+r)/2;
return max(query3(l, mid, v*2, lb, rb, k), query3(mid+1, r, v*2+1, lb, rb, k));
}
int query1(int l, int r, int v, int lb, int rb) {
if (r < lb || l > rb) return -1;
if (l >= lb && r <= rb) return mxsum[v];
int mid = (l+r)/2;
int mxl = -1, mxr = -1;
mxl = query2(l, mid, v*2, lb, mid);
if (mxl != -1) mxr = query3(mid+1, r, v*2+1, mid+1, rb, mxl);
return max({
query1(l, mid, v*2, lb, rb),
query1(mid+1, r, v*2+1, lb, rb),
(mxr == -1 ? -1 : mxl + mxr)
});
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i=0; i<n; i++) cin >> w[i];
build(0, n-1, 1);
// for (int i=0; i<n; i++) {
// for (int j=i; j<n; j++) {
// cout << i << ' ' << j << ' ' << query1(0, n-1, 1, i, j) << '\n';
// }
// }
while (m--) {
int l, r, k;
cin >> l >> r >> k;
cout << (query1(0, n-1, 1, l-1, r-1) <= k) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
188108 KB |
Output is correct |
2 |
Correct |
78 ms |
188180 KB |
Output is correct |
3 |
Correct |
81 ms |
188116 KB |
Output is correct |
4 |
Correct |
87 ms |
188176 KB |
Output is correct |
5 |
Correct |
86 ms |
188200 KB |
Output is correct |
6 |
Correct |
73 ms |
188352 KB |
Output is correct |
7 |
Correct |
88 ms |
188412 KB |
Output is correct |
8 |
Correct |
83 ms |
188440 KB |
Output is correct |
9 |
Correct |
93 ms |
188220 KB |
Output is correct |
10 |
Correct |
77 ms |
188156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
188108 KB |
Output is correct |
2 |
Correct |
78 ms |
188180 KB |
Output is correct |
3 |
Correct |
81 ms |
188116 KB |
Output is correct |
4 |
Correct |
87 ms |
188176 KB |
Output is correct |
5 |
Correct |
86 ms |
188200 KB |
Output is correct |
6 |
Correct |
73 ms |
188352 KB |
Output is correct |
7 |
Correct |
88 ms |
188412 KB |
Output is correct |
8 |
Correct |
83 ms |
188440 KB |
Output is correct |
9 |
Correct |
93 ms |
188220 KB |
Output is correct |
10 |
Correct |
77 ms |
188156 KB |
Output is correct |
11 |
Correct |
81 ms |
189056 KB |
Output is correct |
12 |
Correct |
88 ms |
191292 KB |
Output is correct |
13 |
Correct |
90 ms |
191372 KB |
Output is correct |
14 |
Correct |
104 ms |
191456 KB |
Output is correct |
15 |
Correct |
89 ms |
191508 KB |
Output is correct |
16 |
Correct |
99 ms |
191512 KB |
Output is correct |
17 |
Correct |
92 ms |
190784 KB |
Output is correct |
18 |
Correct |
91 ms |
188748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
301 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
654 ms |
242172 KB |
Output is correct |
2 |
Correct |
524 ms |
244120 KB |
Output is correct |
3 |
Correct |
435 ms |
201940 KB |
Output is correct |
4 |
Correct |
355 ms |
201928 KB |
Output is correct |
5 |
Correct |
338 ms |
201936 KB |
Output is correct |
6 |
Correct |
388 ms |
201672 KB |
Output is correct |
7 |
Correct |
365 ms |
201696 KB |
Output is correct |
8 |
Correct |
246 ms |
200772 KB |
Output is correct |
9 |
Correct |
130 ms |
189932 KB |
Output is correct |
10 |
Correct |
267 ms |
200752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
188108 KB |
Output is correct |
2 |
Correct |
78 ms |
188180 KB |
Output is correct |
3 |
Correct |
81 ms |
188116 KB |
Output is correct |
4 |
Correct |
87 ms |
188176 KB |
Output is correct |
5 |
Correct |
86 ms |
188200 KB |
Output is correct |
6 |
Correct |
73 ms |
188352 KB |
Output is correct |
7 |
Correct |
88 ms |
188412 KB |
Output is correct |
8 |
Correct |
83 ms |
188440 KB |
Output is correct |
9 |
Correct |
93 ms |
188220 KB |
Output is correct |
10 |
Correct |
77 ms |
188156 KB |
Output is correct |
11 |
Correct |
81 ms |
189056 KB |
Output is correct |
12 |
Correct |
88 ms |
191292 KB |
Output is correct |
13 |
Correct |
90 ms |
191372 KB |
Output is correct |
14 |
Correct |
104 ms |
191456 KB |
Output is correct |
15 |
Correct |
89 ms |
191508 KB |
Output is correct |
16 |
Correct |
99 ms |
191512 KB |
Output is correct |
17 |
Correct |
92 ms |
190784 KB |
Output is correct |
18 |
Correct |
91 ms |
188748 KB |
Output is correct |
19 |
Runtime error |
223 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
188108 KB |
Output is correct |
2 |
Correct |
78 ms |
188180 KB |
Output is correct |
3 |
Correct |
81 ms |
188116 KB |
Output is correct |
4 |
Correct |
87 ms |
188176 KB |
Output is correct |
5 |
Correct |
86 ms |
188200 KB |
Output is correct |
6 |
Correct |
73 ms |
188352 KB |
Output is correct |
7 |
Correct |
88 ms |
188412 KB |
Output is correct |
8 |
Correct |
83 ms |
188440 KB |
Output is correct |
9 |
Correct |
93 ms |
188220 KB |
Output is correct |
10 |
Correct |
77 ms |
188156 KB |
Output is correct |
11 |
Correct |
81 ms |
189056 KB |
Output is correct |
12 |
Correct |
88 ms |
191292 KB |
Output is correct |
13 |
Correct |
90 ms |
191372 KB |
Output is correct |
14 |
Correct |
104 ms |
191456 KB |
Output is correct |
15 |
Correct |
89 ms |
191508 KB |
Output is correct |
16 |
Correct |
99 ms |
191512 KB |
Output is correct |
17 |
Correct |
92 ms |
190784 KB |
Output is correct |
18 |
Correct |
91 ms |
188748 KB |
Output is correct |
19 |
Runtime error |
301 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |