#include <bits/stdc++.h>
using namespace std;
const int MAX = (1 << 20) + 10;
struct Node {
int mx;
int val;
Node () {
this->mx = -INT_MAX, this->val = 0;
}
Node(int mx) {
this->mx = mx, this->val = 0;
}
} vec[2 * MAX];
struct SegmentTree {
vector<int> nodes;
vector<int> v[2 * MAX];
int sz;
Node merge(Node &left, int ind) {
Node ans = {max(left.mx, vec[ind].mx)};
ans.val = max(left.val, vec[ind].val);
if (vec[ind].mx < left.mx) {
ans.val = max(ans.val, left.mx + vec[ind].mx);
} else if (v[ind][0] < left.mx) {
ans.val = max(ans.val, *(--lower_bound(v[ind].begin(), v[ind].end(), left.mx)) + left.mx);
}
return ans;
}
int query(int l, int r) {
r += 1;
vector<int> vec1, vec2;
for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
vec1.push_back(l++);
}
if (r & 1) {
vec2.push_back(--r);
}
}
reverse(vec2.begin(), vec2.end());
for (int x: vec2) {
vec1.push_back(x);
}
Node ans = vec[vec1[0]];
for (int i = 1; i < vec1.size(); i++) {
ans = merge(ans, vec1[i]);
}
return ans.val;
}
void build(int dum, int tl, int tr) {
if (tl == tr) {
vec[dum] = Node(nodes[tl]);
v[dum] = {nodes[tl]};
} else {
build(2 * dum, tl, (tl + tr) / 2), build(2 * dum + 1, (tl + tr) / 2 + 1, tr);
vec[dum] = merge(vec[2 * dum], 2 * dum + 1);
std::merge(v[2 * dum].begin(), v[2 * dum].end(),
v[2 * dum + 1].begin(), v[2 * dum + 1].end(),
std::back_inserter(v[dum]));
}
}
SegmentTree(vector<int> nodes) {
this->nodes = nodes;
int n = nodes.size();
n = (1 << (int) log2(n)) * 2;
this->sz = n;
this->nodes.reserve(2 * sz);
while (this->nodes.size() < 2 * sz) {
this->nodes.push_back((int) 1e9);
}
build(1, 0, sz - 1);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
SegmentTree st(v);
while (q--) {
int l, r, k;
cin >> l >> r >> k;
l--, r--;
cout << (st.query(l, r) <= k) << '\n';
}
}
Compilation message
sortbooks.cpp: In member function 'int SegmentTree::query(int, int)':
sortbooks.cpp:50:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 1; i < vec1.size(); i++) {
| ~~^~~~~~~~~~~~~
sortbooks.cpp: In constructor 'SegmentTree::SegmentTree(std::vector<int>)':
sortbooks.cpp:75:35: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
75 | while (this->nodes.size() < 2 * sz) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
65872 KB |
Output is correct |
2 |
Correct |
34 ms |
65872 KB |
Output is correct |
3 |
Correct |
41 ms |
66064 KB |
Output is correct |
4 |
Correct |
34 ms |
66040 KB |
Output is correct |
5 |
Correct |
33 ms |
65880 KB |
Output is correct |
6 |
Correct |
32 ms |
66004 KB |
Output is correct |
7 |
Correct |
33 ms |
66132 KB |
Output is correct |
8 |
Correct |
40 ms |
66132 KB |
Output is correct |
9 |
Correct |
34 ms |
66132 KB |
Output is correct |
10 |
Correct |
33 ms |
66140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
65872 KB |
Output is correct |
2 |
Correct |
34 ms |
65872 KB |
Output is correct |
3 |
Correct |
41 ms |
66064 KB |
Output is correct |
4 |
Correct |
34 ms |
66040 KB |
Output is correct |
5 |
Correct |
33 ms |
65880 KB |
Output is correct |
6 |
Correct |
32 ms |
66004 KB |
Output is correct |
7 |
Correct |
33 ms |
66132 KB |
Output is correct |
8 |
Correct |
40 ms |
66132 KB |
Output is correct |
9 |
Correct |
34 ms |
66132 KB |
Output is correct |
10 |
Correct |
33 ms |
66140 KB |
Output is correct |
11 |
Correct |
34 ms |
66128 KB |
Output is correct |
12 |
Correct |
37 ms |
66908 KB |
Output is correct |
13 |
Correct |
38 ms |
66904 KB |
Output is correct |
14 |
Correct |
39 ms |
66908 KB |
Output is correct |
15 |
Correct |
40 ms |
66896 KB |
Output is correct |
16 |
Correct |
42 ms |
67064 KB |
Output is correct |
17 |
Correct |
38 ms |
66908 KB |
Output is correct |
18 |
Correct |
40 ms |
66884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2182 ms |
221500 KB |
Output is correct |
2 |
Correct |
2164 ms |
221564 KB |
Output is correct |
3 |
Correct |
2143 ms |
221540 KB |
Output is correct |
4 |
Correct |
2165 ms |
221548 KB |
Output is correct |
5 |
Correct |
1796 ms |
221456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
83796 KB |
Output is correct |
2 |
Correct |
183 ms |
83656 KB |
Output is correct |
3 |
Correct |
183 ms |
83812 KB |
Output is correct |
4 |
Correct |
160 ms |
83656 KB |
Output is correct |
5 |
Correct |
161 ms |
83656 KB |
Output is correct |
6 |
Correct |
149 ms |
83656 KB |
Output is correct |
7 |
Correct |
143 ms |
83768 KB |
Output is correct |
8 |
Correct |
138 ms |
83644 KB |
Output is correct |
9 |
Correct |
69 ms |
66388 KB |
Output is correct |
10 |
Correct |
154 ms |
83976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
65872 KB |
Output is correct |
2 |
Correct |
34 ms |
65872 KB |
Output is correct |
3 |
Correct |
41 ms |
66064 KB |
Output is correct |
4 |
Correct |
34 ms |
66040 KB |
Output is correct |
5 |
Correct |
33 ms |
65880 KB |
Output is correct |
6 |
Correct |
32 ms |
66004 KB |
Output is correct |
7 |
Correct |
33 ms |
66132 KB |
Output is correct |
8 |
Correct |
40 ms |
66132 KB |
Output is correct |
9 |
Correct |
34 ms |
66132 KB |
Output is correct |
10 |
Correct |
33 ms |
66140 KB |
Output is correct |
11 |
Correct |
34 ms |
66128 KB |
Output is correct |
12 |
Correct |
37 ms |
66908 KB |
Output is correct |
13 |
Correct |
38 ms |
66904 KB |
Output is correct |
14 |
Correct |
39 ms |
66908 KB |
Output is correct |
15 |
Correct |
40 ms |
66896 KB |
Output is correct |
16 |
Correct |
42 ms |
67064 KB |
Output is correct |
17 |
Correct |
38 ms |
66908 KB |
Output is correct |
18 |
Correct |
40 ms |
66884 KB |
Output is correct |
19 |
Correct |
392 ms |
102460 KB |
Output is correct |
20 |
Correct |
407 ms |
102464 KB |
Output is correct |
21 |
Correct |
340 ms |
102356 KB |
Output is correct |
22 |
Correct |
346 ms |
102720 KB |
Output is correct |
23 |
Correct |
347 ms |
102460 KB |
Output is correct |
24 |
Correct |
283 ms |
102344 KB |
Output is correct |
25 |
Correct |
271 ms |
102520 KB |
Output is correct |
26 |
Correct |
330 ms |
102540 KB |
Output is correct |
27 |
Correct |
344 ms |
102552 KB |
Output is correct |
28 |
Correct |
352 ms |
102444 KB |
Output is correct |
29 |
Correct |
345 ms |
102464 KB |
Output is correct |
30 |
Correct |
353 ms |
102516 KB |
Output is correct |
31 |
Correct |
339 ms |
102352 KB |
Output is correct |
32 |
Correct |
343 ms |
102476 KB |
Output is correct |
33 |
Correct |
347 ms |
102564 KB |
Output is correct |
34 |
Correct |
266 ms |
102464 KB |
Output is correct |
35 |
Correct |
271 ms |
102468 KB |
Output is correct |
36 |
Correct |
252 ms |
102404 KB |
Output is correct |
37 |
Correct |
268 ms |
102508 KB |
Output is correct |
38 |
Correct |
253 ms |
102464 KB |
Output is correct |
39 |
Correct |
320 ms |
102460 KB |
Output is correct |
40 |
Correct |
254 ms |
101968 KB |
Output is correct |
41 |
Correct |
278 ms |
102472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
65872 KB |
Output is correct |
2 |
Correct |
34 ms |
65872 KB |
Output is correct |
3 |
Correct |
41 ms |
66064 KB |
Output is correct |
4 |
Correct |
34 ms |
66040 KB |
Output is correct |
5 |
Correct |
33 ms |
65880 KB |
Output is correct |
6 |
Correct |
32 ms |
66004 KB |
Output is correct |
7 |
Correct |
33 ms |
66132 KB |
Output is correct |
8 |
Correct |
40 ms |
66132 KB |
Output is correct |
9 |
Correct |
34 ms |
66132 KB |
Output is correct |
10 |
Correct |
33 ms |
66140 KB |
Output is correct |
11 |
Correct |
34 ms |
66128 KB |
Output is correct |
12 |
Correct |
37 ms |
66908 KB |
Output is correct |
13 |
Correct |
38 ms |
66904 KB |
Output is correct |
14 |
Correct |
39 ms |
66908 KB |
Output is correct |
15 |
Correct |
40 ms |
66896 KB |
Output is correct |
16 |
Correct |
42 ms |
67064 KB |
Output is correct |
17 |
Correct |
38 ms |
66908 KB |
Output is correct |
18 |
Correct |
40 ms |
66884 KB |
Output is correct |
19 |
Correct |
2182 ms |
221500 KB |
Output is correct |
20 |
Correct |
2164 ms |
221564 KB |
Output is correct |
21 |
Correct |
2143 ms |
221540 KB |
Output is correct |
22 |
Correct |
2165 ms |
221548 KB |
Output is correct |
23 |
Correct |
1796 ms |
221456 KB |
Output is correct |
24 |
Correct |
188 ms |
83796 KB |
Output is correct |
25 |
Correct |
183 ms |
83656 KB |
Output is correct |
26 |
Correct |
183 ms |
83812 KB |
Output is correct |
27 |
Correct |
160 ms |
83656 KB |
Output is correct |
28 |
Correct |
161 ms |
83656 KB |
Output is correct |
29 |
Correct |
149 ms |
83656 KB |
Output is correct |
30 |
Correct |
143 ms |
83768 KB |
Output is correct |
31 |
Correct |
138 ms |
83644 KB |
Output is correct |
32 |
Correct |
69 ms |
66388 KB |
Output is correct |
33 |
Correct |
154 ms |
83976 KB |
Output is correct |
34 |
Correct |
392 ms |
102460 KB |
Output is correct |
35 |
Correct |
407 ms |
102464 KB |
Output is correct |
36 |
Correct |
340 ms |
102356 KB |
Output is correct |
37 |
Correct |
346 ms |
102720 KB |
Output is correct |
38 |
Correct |
347 ms |
102460 KB |
Output is correct |
39 |
Correct |
283 ms |
102344 KB |
Output is correct |
40 |
Correct |
271 ms |
102520 KB |
Output is correct |
41 |
Correct |
330 ms |
102540 KB |
Output is correct |
42 |
Correct |
344 ms |
102552 KB |
Output is correct |
43 |
Correct |
352 ms |
102444 KB |
Output is correct |
44 |
Correct |
345 ms |
102464 KB |
Output is correct |
45 |
Correct |
353 ms |
102516 KB |
Output is correct |
46 |
Correct |
339 ms |
102352 KB |
Output is correct |
47 |
Correct |
343 ms |
102476 KB |
Output is correct |
48 |
Correct |
347 ms |
102564 KB |
Output is correct |
49 |
Correct |
266 ms |
102464 KB |
Output is correct |
50 |
Correct |
271 ms |
102468 KB |
Output is correct |
51 |
Correct |
252 ms |
102404 KB |
Output is correct |
52 |
Correct |
268 ms |
102508 KB |
Output is correct |
53 |
Correct |
253 ms |
102464 KB |
Output is correct |
54 |
Correct |
320 ms |
102460 KB |
Output is correct |
55 |
Correct |
254 ms |
101968 KB |
Output is correct |
56 |
Correct |
278 ms |
102472 KB |
Output is correct |
57 |
Correct |
2201 ms |
221452 KB |
Output is correct |
58 |
Correct |
2155 ms |
221452 KB |
Output is correct |
59 |
Correct |
2114 ms |
221500 KB |
Output is correct |
60 |
Correct |
2082 ms |
221608 KB |
Output is correct |
61 |
Correct |
2114 ms |
221508 KB |
Output is correct |
62 |
Correct |
2107 ms |
221572 KB |
Output is correct |
63 |
Correct |
1202 ms |
221640 KB |
Output is correct |
64 |
Correct |
1215 ms |
221720 KB |
Output is correct |
65 |
Correct |
1792 ms |
221616 KB |
Output is correct |
66 |
Correct |
1824 ms |
221452 KB |
Output is correct |
67 |
Correct |
1813 ms |
221656 KB |
Output is correct |
68 |
Correct |
1793 ms |
221644 KB |
Output is correct |
69 |
Correct |
1787 ms |
221452 KB |
Output is correct |
70 |
Correct |
1837 ms |
221508 KB |
Output is correct |
71 |
Correct |
1803 ms |
221468 KB |
Output is correct |
72 |
Correct |
1809 ms |
221448 KB |
Output is correct |
73 |
Correct |
1124 ms |
221520 KB |
Output is correct |
74 |
Correct |
1104 ms |
221708 KB |
Output is correct |
75 |
Correct |
1096 ms |
221460 KB |
Output is correct |
76 |
Correct |
1108 ms |
221536 KB |
Output is correct |
77 |
Correct |
1131 ms |
221456 KB |
Output is correct |
78 |
Correct |
1545 ms |
221524 KB |
Output is correct |
79 |
Correct |
1084 ms |
217940 KB |
Output is correct |
80 |
Correct |
1317 ms |
221476 KB |
Output is correct |