#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int inf = 1e9;
const int lg = 20;
template<typename T> struct SegTree {
vector<T> tree;
int size;
T neutral_element = -1e9; // sum - 0, mx - (-INF), mn - INF
void init(int n) {
size = 1;
while (size <= n) size *= 2;
tree.assign(2 * size, neutral_element);
}
inline T type(T a, T b) {
return max(a, b);
}
SegTree(int n) {
init(n);
}
SegTree() {}
void set(int p, int value) { // set value at position p
p += size;
tree[p] = type(value, tree[p]);
for (; p > 1; p >>= 1) tree[p >> 1] = type(tree[p], tree[p ^ 1]);
}
T get(int l, int r) { // sum on interval [l, r]
if (l > r) return neutral_element;
int res = neutral_element;
for (l += size, r += size + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = type(res, tree[l++]);
if (r & 1) res = type(res, tree[--r]);
}
return res;
}
};
void solve() {
int n, q;
cin >> n >> q;
vector<int> a(n + 1), left(n + 1), ans(q);
stack<int> st;
vector<vector<int>> vec(n + 1);
vector<vector<tuple<int, int, int>>> queries(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < q; i++) {
int l, r, k;
cin >> l >> r >> k;
queries[l].emplace_back(r, k, i);
}
for (int i = 1; i <= n; i++) {
while (!st.empty() and a[st.top()] <= a[i]) st.pop();
left[i] = st.empty() ? 0 : st.top();
vec[left[i]].emplace_back(i);
st.push(i);
}
SegTree<int> sg(n + 1);
for (int l = n; l > 0; l--) {
for (int i: vec[l]) sg.set(i, a[i] + a[left[i]]);
for (auto [r, k, i]: queries[l]) {
ans[i] = sg.get(l, r) <= k;
}
}
for (int i = 0; i < q; i++) cout << ans[i] << '\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 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
869 ms |
128812 KB |
Output is correct |
2 |
Correct |
821 ms |
140092 KB |
Output is correct |
3 |
Correct |
803 ms |
140116 KB |
Output is correct |
4 |
Correct |
817 ms |
139948 KB |
Output is correct |
5 |
Correct |
759 ms |
127936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
11416 KB |
Output is correct |
2 |
Correct |
64 ms |
11712 KB |
Output is correct |
3 |
Correct |
49 ms |
12088 KB |
Output is correct |
4 |
Correct |
47 ms |
12236 KB |
Output is correct |
5 |
Correct |
50 ms |
12240 KB |
Output is correct |
6 |
Correct |
38 ms |
11472 KB |
Output is correct |
7 |
Correct |
37 ms |
11468 KB |
Output is correct |
8 |
Correct |
44 ms |
11392 KB |
Output is correct |
9 |
Correct |
24 ms |
3912 KB |
Output is correct |
10 |
Correct |
42 ms |
11532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
956 KB |
Output is correct |
19 |
Correct |
128 ms |
25608 KB |
Output is correct |
20 |
Correct |
153 ms |
28944 KB |
Output is correct |
21 |
Correct |
118 ms |
27876 KB |
Output is correct |
22 |
Correct |
115 ms |
27728 KB |
Output is correct |
23 |
Correct |
114 ms |
27728 KB |
Output is correct |
24 |
Correct |
96 ms |
25648 KB |
Output is correct |
25 |
Correct |
95 ms |
25712 KB |
Output is correct |
26 |
Correct |
107 ms |
26052 KB |
Output is correct |
27 |
Correct |
106 ms |
26060 KB |
Output is correct |
28 |
Correct |
120 ms |
26056 KB |
Output is correct |
29 |
Correct |
126 ms |
26572 KB |
Output is correct |
30 |
Correct |
114 ms |
26520 KB |
Output is correct |
31 |
Correct |
111 ms |
26512 KB |
Output is correct |
32 |
Correct |
117 ms |
26568 KB |
Output is correct |
33 |
Correct |
120 ms |
26392 KB |
Output is correct |
34 |
Correct |
85 ms |
25004 KB |
Output is correct |
35 |
Correct |
88 ms |
25296 KB |
Output is correct |
36 |
Correct |
85 ms |
24776 KB |
Output is correct |
37 |
Correct |
84 ms |
24844 KB |
Output is correct |
38 |
Correct |
85 ms |
25028 KB |
Output is correct |
39 |
Correct |
101 ms |
24920 KB |
Output is correct |
40 |
Correct |
80 ms |
19464 KB |
Output is correct |
41 |
Correct |
105 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
956 KB |
Output is correct |
19 |
Correct |
869 ms |
128812 KB |
Output is correct |
20 |
Correct |
821 ms |
140092 KB |
Output is correct |
21 |
Correct |
803 ms |
140116 KB |
Output is correct |
22 |
Correct |
817 ms |
139948 KB |
Output is correct |
23 |
Correct |
759 ms |
127936 KB |
Output is correct |
24 |
Correct |
52 ms |
11416 KB |
Output is correct |
25 |
Correct |
64 ms |
11712 KB |
Output is correct |
26 |
Correct |
49 ms |
12088 KB |
Output is correct |
27 |
Correct |
47 ms |
12236 KB |
Output is correct |
28 |
Correct |
50 ms |
12240 KB |
Output is correct |
29 |
Correct |
38 ms |
11472 KB |
Output is correct |
30 |
Correct |
37 ms |
11468 KB |
Output is correct |
31 |
Correct |
44 ms |
11392 KB |
Output is correct |
32 |
Correct |
24 ms |
3912 KB |
Output is correct |
33 |
Correct |
42 ms |
11532 KB |
Output is correct |
34 |
Correct |
128 ms |
25608 KB |
Output is correct |
35 |
Correct |
153 ms |
28944 KB |
Output is correct |
36 |
Correct |
118 ms |
27876 KB |
Output is correct |
37 |
Correct |
115 ms |
27728 KB |
Output is correct |
38 |
Correct |
114 ms |
27728 KB |
Output is correct |
39 |
Correct |
96 ms |
25648 KB |
Output is correct |
40 |
Correct |
95 ms |
25712 KB |
Output is correct |
41 |
Correct |
107 ms |
26052 KB |
Output is correct |
42 |
Correct |
106 ms |
26060 KB |
Output is correct |
43 |
Correct |
120 ms |
26056 KB |
Output is correct |
44 |
Correct |
126 ms |
26572 KB |
Output is correct |
45 |
Correct |
114 ms |
26520 KB |
Output is correct |
46 |
Correct |
111 ms |
26512 KB |
Output is correct |
47 |
Correct |
117 ms |
26568 KB |
Output is correct |
48 |
Correct |
120 ms |
26392 KB |
Output is correct |
49 |
Correct |
85 ms |
25004 KB |
Output is correct |
50 |
Correct |
88 ms |
25296 KB |
Output is correct |
51 |
Correct |
85 ms |
24776 KB |
Output is correct |
52 |
Correct |
84 ms |
24844 KB |
Output is correct |
53 |
Correct |
85 ms |
25028 KB |
Output is correct |
54 |
Correct |
101 ms |
24920 KB |
Output is correct |
55 |
Correct |
80 ms |
19464 KB |
Output is correct |
56 |
Correct |
105 ms |
24056 KB |
Output is correct |
57 |
Correct |
838 ms |
141364 KB |
Output is correct |
58 |
Correct |
871 ms |
141448 KB |
Output is correct |
59 |
Correct |
779 ms |
139164 KB |
Output is correct |
60 |
Correct |
758 ms |
138776 KB |
Output is correct |
61 |
Correct |
788 ms |
138832 KB |
Output is correct |
62 |
Correct |
836 ms |
138768 KB |
Output is correct |
63 |
Correct |
459 ms |
121492 KB |
Output is correct |
64 |
Correct |
465 ms |
121536 KB |
Output is correct |
65 |
Correct |
756 ms |
127088 KB |
Output is correct |
66 |
Correct |
761 ms |
127164 KB |
Output is correct |
67 |
Correct |
750 ms |
127340 KB |
Output is correct |
68 |
Correct |
797 ms |
129496 KB |
Output is correct |
69 |
Correct |
926 ms |
127028 KB |
Output is correct |
70 |
Correct |
889 ms |
128760 KB |
Output is correct |
71 |
Correct |
895 ms |
128784 KB |
Output is correct |
72 |
Correct |
867 ms |
126060 KB |
Output is correct |
73 |
Correct |
407 ms |
118988 KB |
Output is correct |
74 |
Correct |
415 ms |
120308 KB |
Output is correct |
75 |
Correct |
406 ms |
118956 KB |
Output is correct |
76 |
Correct |
403 ms |
118976 KB |
Output is correct |
77 |
Correct |
417 ms |
119008 KB |
Output is correct |
78 |
Correct |
601 ms |
123592 KB |
Output is correct |
79 |
Correct |
462 ms |
86260 KB |
Output is correct |
80 |
Correct |
585 ms |
118648 KB |
Output is correct |