#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <cassert>
#include <algorithm>
using namespace std;
struct Node {
int mx;
int mn;
int val;
vector<int> v;
Node () {
this->mx = INT_MIN, this->mn = INT_MAX, this->val = 0;
}
};
struct SegmentTree {
vector<Node> vec;
vector<int> nodes;
Node merge (Node &left, Node &right) {
Node ans = {};
ans.mn = min(left.mn, right.mn);
ans.mx = max(left.mx, right.mx);
ans.val = max(left.val, right.val);
if (right.mx < left.mx) {
ans.val = max(ans.val, left.mx + right.mx);
} else if (right.mn < left.mx) {
int l = 0;
int r = right.v.size() - 1;
while (l != r) {
int m = (l + r + 1)/2;
if (right.v[m] < left.mx) {
l = m;
} else {
r = m - 1;
}
}
ans.val = max(ans.val, right.v[l] + left.mx);
}
return ans;
}
vector<int> intervals;
void query (int dum, int tl, int tr, int l, int r) {
if (tl >= l && tr <= r) {
intervals.push_back(dum);
return;
}
if (tl > r || tr < l) {
return;
}
query(2 * dum + 1, tl, (tl + tr)/2,l, r), query(2 * dum + 2, (tl + tr)/2 + 1, tr, l, r);
}
int query (int l, int r) {
intervals.clear();
query(0, 0, (int)vec.size()/2 - 1, l, r);
Node ans = vec[intervals[0]];
for (int i = 1; i < intervals.size(); i++) {
ans = merge(ans, vec[intervals[i]]);
}
return ans.val;
}
void build (int dum, int tl, int tr) {
if (tl == tr) {
vec[dum] = Node();
vec[dum].mn = vec[dum].mx = nodes[tl];
vec[dum].v = {nodes[tl]};
return;
}
build(2 * dum + 1, tl, (tl + tr)/2), build(2 * dum + 2, (tl + tr)/2 + 1, tr);
vec[dum] = merge(vec[2 * dum + 1], vec[2 * dum + 2]);
int l = 0;
int r = 0;
while (l < vec[2 * dum + 1].v.size() || r < vec[2 * dum + 2].v.size()) {
if (l == vec[2 * dum + 1].v.size()) {
vec[dum].v.push_back(vec[2 * dum + 2].v[r++]);
} else if (r == vec[2 * dum + 2].v.size()) {
vec[dum].v.push_back(vec[2 * dum + 1].v[l++]);
} else if (vec[2 * dum + 1].v[l] < vec[2 * dum + 2].v[r]){
vec[dum].v.push_back(vec[2 * dum + 1].v[l++]);
} else {
vec[dum].v.push_back(vec[2 * dum + 2].v[r++]);
}
}
}
SegmentTree (vector<int> nodes) {
this->nodes = nodes;
int n = nodes.size();
n = (1 << (int)log2(n)) * 2;
vec.assign(2 * n, Node());
while (this->nodes.size() < vec.size()) {
this->nodes.push_back((int)1e9);
}
build(0, 0, (int)vec.size()/2 - 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:59:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = 1; i < intervals.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
sortbooks.cpp: In member function 'void SegmentTree::build(int, int, int)':
sortbooks.cpp:75:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | while (l < vec[2 * dum + 1].v.size() || r < vec[2 * dum + 2].v.size()) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:75:51: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | while (l < vec[2 * dum + 1].v.size() || r < vec[2 * dum + 2].v.size()) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:76:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | if (l == vec[2 * dum + 1].v.size()) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:78:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | } else if (r == vec[2 * dum + 2].v.size()) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
696 KB |
Output is correct |
12 |
Correct |
5 ms |
2012 KB |
Output is correct |
13 |
Correct |
5 ms |
2088 KB |
Output is correct |
14 |
Correct |
6 ms |
1880 KB |
Output is correct |
15 |
Correct |
7 ms |
1884 KB |
Output is correct |
16 |
Correct |
5 ms |
2084 KB |
Output is correct |
17 |
Correct |
6 ms |
1884 KB |
Output is correct |
18 |
Correct |
7 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2287 ms |
239744 KB |
Output is correct |
2 |
Correct |
2186 ms |
255068 KB |
Output is correct |
3 |
Correct |
2116 ms |
255124 KB |
Output is correct |
4 |
Correct |
2078 ms |
253908 KB |
Output is correct |
5 |
Correct |
1410 ms |
254556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
29604 KB |
Output is correct |
2 |
Correct |
143 ms |
29344 KB |
Output is correct |
3 |
Correct |
130 ms |
29316 KB |
Output is correct |
4 |
Correct |
108 ms |
29488 KB |
Output is correct |
5 |
Correct |
111 ms |
29492 KB |
Output is correct |
6 |
Correct |
103 ms |
29316 KB |
Output is correct |
7 |
Correct |
93 ms |
29492 KB |
Output is correct |
8 |
Correct |
303 ms |
29232 KB |
Output is correct |
9 |
Correct |
36 ms |
1628 KB |
Output is correct |
10 |
Correct |
315 ms |
29460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
696 KB |
Output is correct |
12 |
Correct |
5 ms |
2012 KB |
Output is correct |
13 |
Correct |
5 ms |
2088 KB |
Output is correct |
14 |
Correct |
6 ms |
1880 KB |
Output is correct |
15 |
Correct |
7 ms |
1884 KB |
Output is correct |
16 |
Correct |
5 ms |
2084 KB |
Output is correct |
17 |
Correct |
6 ms |
1884 KB |
Output is correct |
18 |
Correct |
7 ms |
1884 KB |
Output is correct |
19 |
Correct |
358 ms |
60692 KB |
Output is correct |
20 |
Correct |
366 ms |
60496 KB |
Output is correct |
21 |
Correct |
301 ms |
60616 KB |
Output is correct |
22 |
Correct |
291 ms |
61124 KB |
Output is correct |
23 |
Correct |
292 ms |
60936 KB |
Output is correct |
24 |
Correct |
211 ms |
60352 KB |
Output is correct |
25 |
Correct |
219 ms |
60180 KB |
Output is correct |
26 |
Correct |
245 ms |
60692 KB |
Output is correct |
27 |
Correct |
245 ms |
60952 KB |
Output is correct |
28 |
Correct |
271 ms |
60428 KB |
Output is correct |
29 |
Correct |
268 ms |
60952 KB |
Output is correct |
30 |
Correct |
249 ms |
60408 KB |
Output is correct |
31 |
Correct |
259 ms |
60600 KB |
Output is correct |
32 |
Correct |
275 ms |
60704 KB |
Output is correct |
33 |
Correct |
254 ms |
60688 KB |
Output is correct |
34 |
Correct |
208 ms |
60824 KB |
Output is correct |
35 |
Correct |
201 ms |
60908 KB |
Output is correct |
36 |
Correct |
202 ms |
61204 KB |
Output is correct |
37 |
Correct |
211 ms |
60304 KB |
Output is correct |
38 |
Correct |
199 ms |
61140 KB |
Output is correct |
39 |
Correct |
955 ms |
60840 KB |
Output is correct |
40 |
Correct |
1567 ms |
59384 KB |
Output is correct |
41 |
Correct |
1154 ms |
60196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
696 KB |
Output is correct |
12 |
Correct |
5 ms |
2012 KB |
Output is correct |
13 |
Correct |
5 ms |
2088 KB |
Output is correct |
14 |
Correct |
6 ms |
1880 KB |
Output is correct |
15 |
Correct |
7 ms |
1884 KB |
Output is correct |
16 |
Correct |
5 ms |
2084 KB |
Output is correct |
17 |
Correct |
6 ms |
1884 KB |
Output is correct |
18 |
Correct |
7 ms |
1884 KB |
Output is correct |
19 |
Correct |
2287 ms |
239744 KB |
Output is correct |
20 |
Correct |
2186 ms |
255068 KB |
Output is correct |
21 |
Correct |
2116 ms |
255124 KB |
Output is correct |
22 |
Correct |
2078 ms |
253908 KB |
Output is correct |
23 |
Correct |
1410 ms |
254556 KB |
Output is correct |
24 |
Correct |
157 ms |
29604 KB |
Output is correct |
25 |
Correct |
143 ms |
29344 KB |
Output is correct |
26 |
Correct |
130 ms |
29316 KB |
Output is correct |
27 |
Correct |
108 ms |
29488 KB |
Output is correct |
28 |
Correct |
111 ms |
29492 KB |
Output is correct |
29 |
Correct |
103 ms |
29316 KB |
Output is correct |
30 |
Correct |
93 ms |
29492 KB |
Output is correct |
31 |
Correct |
303 ms |
29232 KB |
Output is correct |
32 |
Correct |
36 ms |
1628 KB |
Output is correct |
33 |
Correct |
315 ms |
29460 KB |
Output is correct |
34 |
Correct |
358 ms |
60692 KB |
Output is correct |
35 |
Correct |
366 ms |
60496 KB |
Output is correct |
36 |
Correct |
301 ms |
60616 KB |
Output is correct |
37 |
Correct |
291 ms |
61124 KB |
Output is correct |
38 |
Correct |
292 ms |
60936 KB |
Output is correct |
39 |
Correct |
211 ms |
60352 KB |
Output is correct |
40 |
Correct |
219 ms |
60180 KB |
Output is correct |
41 |
Correct |
245 ms |
60692 KB |
Output is correct |
42 |
Correct |
245 ms |
60952 KB |
Output is correct |
43 |
Correct |
271 ms |
60428 KB |
Output is correct |
44 |
Correct |
268 ms |
60952 KB |
Output is correct |
45 |
Correct |
249 ms |
60408 KB |
Output is correct |
46 |
Correct |
259 ms |
60600 KB |
Output is correct |
47 |
Correct |
275 ms |
60704 KB |
Output is correct |
48 |
Correct |
254 ms |
60688 KB |
Output is correct |
49 |
Correct |
208 ms |
60824 KB |
Output is correct |
50 |
Correct |
201 ms |
60908 KB |
Output is correct |
51 |
Correct |
202 ms |
61204 KB |
Output is correct |
52 |
Correct |
211 ms |
60304 KB |
Output is correct |
53 |
Correct |
199 ms |
61140 KB |
Output is correct |
54 |
Correct |
955 ms |
60840 KB |
Output is correct |
55 |
Correct |
1567 ms |
59384 KB |
Output is correct |
56 |
Correct |
1154 ms |
60196 KB |
Output is correct |
57 |
Correct |
2074 ms |
257368 KB |
Output is correct |
58 |
Correct |
2036 ms |
258612 KB |
Output is correct |
59 |
Correct |
2015 ms |
258184 KB |
Output is correct |
60 |
Correct |
1993 ms |
257632 KB |
Output is correct |
61 |
Correct |
2053 ms |
257940 KB |
Output is correct |
62 |
Correct |
2106 ms |
257016 KB |
Output is correct |
63 |
Correct |
1002 ms |
256568 KB |
Output is correct |
64 |
Correct |
1016 ms |
257172 KB |
Output is correct |
65 |
Correct |
1370 ms |
257688 KB |
Output is correct |
66 |
Correct |
1376 ms |
259352 KB |
Output is correct |
67 |
Correct |
1413 ms |
257712 KB |
Output is correct |
68 |
Correct |
1401 ms |
258988 KB |
Output is correct |
69 |
Correct |
1380 ms |
258412 KB |
Output is correct |
70 |
Correct |
1366 ms |
259648 KB |
Output is correct |
71 |
Correct |
1379 ms |
258016 KB |
Output is correct |
72 |
Correct |
1380 ms |
258408 KB |
Output is correct |
73 |
Correct |
1054 ms |
253748 KB |
Output is correct |
74 |
Correct |
1020 ms |
255848 KB |
Output is correct |
75 |
Correct |
1060 ms |
254292 KB |
Output is correct |
76 |
Correct |
1060 ms |
254812 KB |
Output is correct |
77 |
Correct |
1058 ms |
255116 KB |
Output is correct |
78 |
Execution timed out |
3050 ms |
243852 KB |
Time limit exceeded |
79 |
Halted |
0 ms |
0 KB |
- |