#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 (int mn, int mx) {
this->mx = mx, this->mn = mn, this->val = 0;
}
};
struct SegmentTree {
vector<Node> vec;
vector<int> nodes;
int sz;
Node merge (Node &left, Node &right) {
Node ans = {min(left.mn, right.mn), 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.v[0] < left.mx) {
ans.val = max(ans.val, *(--lower_bound(right.v.begin(), right.v.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, vec[vec1[i]]);
}
return ans.val;
}
void build (int dum, int tl, int tr) {
if (tl == tr) {
vec[dum] = Node(nodes[tl], nodes[tl]);
vec[dum].v = {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], vec[2 * dum + 1]);
std::merge(vec[2 * dum].v.begin(), vec[2 * dum].v.end(),
vec[2 * dum + 1].v.begin(), vec[2 * dum + 1].v.end(),
std::back_inserter(vec[dum].v));
}
}
SegmentTree (vector<int> nodes) {
this->nodes = nodes;
int n = nodes.size();
n = (1 << (int)log2(n)) * 2;
this->sz = n;
vec.assign(2 * n, Node(INT_MAX, -INT_MAX));
this->nodes.reserve(vec.size());
while (this->nodes.size() < vec.size()) {
this->nodes.push_back((int)1e9);
}
build( 1, 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:47:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i = 1; i < vec1.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
11 |
Correct |
3 ms |
696 KB |
Output is correct |
12 |
Correct |
5 ms |
1884 KB |
Output is correct |
13 |
Correct |
5 ms |
1884 KB |
Output is correct |
14 |
Correct |
7 ms |
1884 KB |
Output is correct |
15 |
Correct |
7 ms |
2088 KB |
Output is correct |
16 |
Correct |
6 ms |
1884 KB |
Output is correct |
17 |
Correct |
5 ms |
1884 KB |
Output is correct |
18 |
Correct |
6 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2358 ms |
237996 KB |
Output is correct |
2 |
Correct |
2229 ms |
237996 KB |
Output is correct |
3 |
Correct |
2393 ms |
237988 KB |
Output is correct |
4 |
Correct |
2273 ms |
238020 KB |
Output is correct |
5 |
Correct |
1848 ms |
238016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
163 ms |
28416 KB |
Output is correct |
2 |
Correct |
154 ms |
28456 KB |
Output is correct |
3 |
Correct |
148 ms |
28380 KB |
Output is correct |
4 |
Correct |
127 ms |
28204 KB |
Output is correct |
5 |
Correct |
126 ms |
28360 KB |
Output is correct |
6 |
Correct |
113 ms |
28372 KB |
Output is correct |
7 |
Correct |
108 ms |
28364 KB |
Output is correct |
8 |
Correct |
356 ms |
28412 KB |
Output is correct |
9 |
Correct |
47 ms |
756 KB |
Output is correct |
10 |
Correct |
325 ms |
28360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
11 |
Correct |
3 ms |
696 KB |
Output is correct |
12 |
Correct |
5 ms |
1884 KB |
Output is correct |
13 |
Correct |
5 ms |
1884 KB |
Output is correct |
14 |
Correct |
7 ms |
1884 KB |
Output is correct |
15 |
Correct |
7 ms |
2088 KB |
Output is correct |
16 |
Correct |
6 ms |
1884 KB |
Output is correct |
17 |
Correct |
5 ms |
1884 KB |
Output is correct |
18 |
Correct |
6 ms |
1884 KB |
Output is correct |
19 |
Correct |
381 ms |
57276 KB |
Output is correct |
20 |
Correct |
387 ms |
57404 KB |
Output is correct |
21 |
Correct |
328 ms |
57412 KB |
Output is correct |
22 |
Correct |
328 ms |
57412 KB |
Output is correct |
23 |
Correct |
318 ms |
57272 KB |
Output is correct |
24 |
Correct |
255 ms |
57416 KB |
Output is correct |
25 |
Correct |
242 ms |
57412 KB |
Output is correct |
26 |
Correct |
307 ms |
57192 KB |
Output is correct |
27 |
Correct |
283 ms |
57244 KB |
Output is correct |
28 |
Correct |
349 ms |
57420 KB |
Output is correct |
29 |
Correct |
318 ms |
57408 KB |
Output is correct |
30 |
Correct |
322 ms |
57392 KB |
Output is correct |
31 |
Correct |
312 ms |
57412 KB |
Output is correct |
32 |
Correct |
324 ms |
57412 KB |
Output is correct |
33 |
Correct |
316 ms |
57356 KB |
Output is correct |
34 |
Correct |
232 ms |
57408 KB |
Output is correct |
35 |
Correct |
239 ms |
57352 KB |
Output is correct |
36 |
Correct |
237 ms |
57412 KB |
Output is correct |
37 |
Correct |
234 ms |
57408 KB |
Output is correct |
38 |
Correct |
232 ms |
57408 KB |
Output is correct |
39 |
Correct |
1002 ms |
57412 KB |
Output is correct |
40 |
Correct |
1594 ms |
56908 KB |
Output is correct |
41 |
Correct |
1169 ms |
57512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
11 |
Correct |
3 ms |
696 KB |
Output is correct |
12 |
Correct |
5 ms |
1884 KB |
Output is correct |
13 |
Correct |
5 ms |
1884 KB |
Output is correct |
14 |
Correct |
7 ms |
1884 KB |
Output is correct |
15 |
Correct |
7 ms |
2088 KB |
Output is correct |
16 |
Correct |
6 ms |
1884 KB |
Output is correct |
17 |
Correct |
5 ms |
1884 KB |
Output is correct |
18 |
Correct |
6 ms |
1884 KB |
Output is correct |
19 |
Correct |
2358 ms |
237996 KB |
Output is correct |
20 |
Correct |
2229 ms |
237996 KB |
Output is correct |
21 |
Correct |
2393 ms |
237988 KB |
Output is correct |
22 |
Correct |
2273 ms |
238020 KB |
Output is correct |
23 |
Correct |
1848 ms |
238016 KB |
Output is correct |
24 |
Correct |
163 ms |
28416 KB |
Output is correct |
25 |
Correct |
154 ms |
28456 KB |
Output is correct |
26 |
Correct |
148 ms |
28380 KB |
Output is correct |
27 |
Correct |
127 ms |
28204 KB |
Output is correct |
28 |
Correct |
126 ms |
28360 KB |
Output is correct |
29 |
Correct |
113 ms |
28372 KB |
Output is correct |
30 |
Correct |
108 ms |
28364 KB |
Output is correct |
31 |
Correct |
356 ms |
28412 KB |
Output is correct |
32 |
Correct |
47 ms |
756 KB |
Output is correct |
33 |
Correct |
325 ms |
28360 KB |
Output is correct |
34 |
Correct |
381 ms |
57276 KB |
Output is correct |
35 |
Correct |
387 ms |
57404 KB |
Output is correct |
36 |
Correct |
328 ms |
57412 KB |
Output is correct |
37 |
Correct |
328 ms |
57412 KB |
Output is correct |
38 |
Correct |
318 ms |
57272 KB |
Output is correct |
39 |
Correct |
255 ms |
57416 KB |
Output is correct |
40 |
Correct |
242 ms |
57412 KB |
Output is correct |
41 |
Correct |
307 ms |
57192 KB |
Output is correct |
42 |
Correct |
283 ms |
57244 KB |
Output is correct |
43 |
Correct |
349 ms |
57420 KB |
Output is correct |
44 |
Correct |
318 ms |
57408 KB |
Output is correct |
45 |
Correct |
322 ms |
57392 KB |
Output is correct |
46 |
Correct |
312 ms |
57412 KB |
Output is correct |
47 |
Correct |
324 ms |
57412 KB |
Output is correct |
48 |
Correct |
316 ms |
57356 KB |
Output is correct |
49 |
Correct |
232 ms |
57408 KB |
Output is correct |
50 |
Correct |
239 ms |
57352 KB |
Output is correct |
51 |
Correct |
237 ms |
57412 KB |
Output is correct |
52 |
Correct |
234 ms |
57408 KB |
Output is correct |
53 |
Correct |
232 ms |
57408 KB |
Output is correct |
54 |
Correct |
1002 ms |
57412 KB |
Output is correct |
55 |
Correct |
1594 ms |
56908 KB |
Output is correct |
56 |
Correct |
1169 ms |
57512 KB |
Output is correct |
57 |
Correct |
2194 ms |
237984 KB |
Output is correct |
58 |
Correct |
2232 ms |
238028 KB |
Output is correct |
59 |
Correct |
2187 ms |
238040 KB |
Output is correct |
60 |
Correct |
2271 ms |
238016 KB |
Output is correct |
61 |
Correct |
2268 ms |
238032 KB |
Output is correct |
62 |
Correct |
2263 ms |
238024 KB |
Output is correct |
63 |
Correct |
1373 ms |
238016 KB |
Output is correct |
64 |
Correct |
1211 ms |
238032 KB |
Output is correct |
65 |
Correct |
1924 ms |
238092 KB |
Output is correct |
66 |
Correct |
1919 ms |
238064 KB |
Output is correct |
67 |
Correct |
1859 ms |
238040 KB |
Output is correct |
68 |
Correct |
1888 ms |
237920 KB |
Output is correct |
69 |
Correct |
1871 ms |
238092 KB |
Output is correct |
70 |
Correct |
1893 ms |
238092 KB |
Output is correct |
71 |
Correct |
1908 ms |
238096 KB |
Output is correct |
72 |
Correct |
1879 ms |
238036 KB |
Output is correct |
73 |
Correct |
1205 ms |
237968 KB |
Output is correct |
74 |
Correct |
1203 ms |
237944 KB |
Output is correct |
75 |
Correct |
1253 ms |
238148 KB |
Output is correct |
76 |
Correct |
1264 ms |
238100 KB |
Output is correct |
77 |
Correct |
1245 ms |
238108 KB |
Output is correct |
78 |
Execution timed out |
3064 ms |
238036 KB |
Time limit exceeded |
79 |
Halted |
0 ms |
0 KB |
- |