#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;
}
Node (int mn, int mx) {
this->mx = mx, this->mn = mn, this->val = 0;
}
} vec[(int)2097152];
struct SegmentTree {
vector<int> nodes;
int n;
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.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);
} else if (tl <= r && tr >= l) {
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();
intervals.reserve(10);
query(0, 0, n - 1, l, r);
Node ans = vec[intervals[0]];
ans.v.reserve(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(nodes[tl], nodes[tl]);
vec[dum].v = {nodes[tl]};
} else {
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]);
vec[dum].v.reserve(vec[2 * dum + 1].v.size() + vec[2 * dum + 2].v.size());
std::merge(vec[2 * dum + 1].v.begin(), vec[2 * dum + 1].v.end(),
vec[2 * dum + 2].v.begin(), vec[2 * dum + 2].v.end(),
std::back_inserter(vec[dum].v));
}
}
SegmentTree (vector<int> nodes) {
this->nodes = nodes;
this->n = nodes.size();
this->n = (1 << (int)log2(n)) * 2;
this->nodes.reserve(2 * n);
while (this->nodes.size() < 2 * n) {
this->nodes.push_back((int)1e9);
}
build(0, 0, n - 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 constructor 'SegmentTree::SegmentTree(std::vector<int>)':
sortbooks.cpp:82:35: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
82 | while (this->nodes.size() < 2 * n) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
82268 KB |
Output is correct |
2 |
Correct |
19 ms |
82520 KB |
Output is correct |
3 |
Correct |
22 ms |
82524 KB |
Output is correct |
4 |
Correct |
18 ms |
82524 KB |
Output is correct |
5 |
Correct |
21 ms |
82524 KB |
Output is correct |
6 |
Correct |
19 ms |
82528 KB |
Output is correct |
7 |
Correct |
19 ms |
82524 KB |
Output is correct |
8 |
Correct |
19 ms |
82588 KB |
Output is correct |
9 |
Correct |
18 ms |
82580 KB |
Output is correct |
10 |
Correct |
19 ms |
82384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
82268 KB |
Output is correct |
2 |
Correct |
19 ms |
82520 KB |
Output is correct |
3 |
Correct |
22 ms |
82524 KB |
Output is correct |
4 |
Correct |
18 ms |
82524 KB |
Output is correct |
5 |
Correct |
21 ms |
82524 KB |
Output is correct |
6 |
Correct |
19 ms |
82528 KB |
Output is correct |
7 |
Correct |
19 ms |
82524 KB |
Output is correct |
8 |
Correct |
19 ms |
82588 KB |
Output is correct |
9 |
Correct |
18 ms |
82580 KB |
Output is correct |
10 |
Correct |
19 ms |
82384 KB |
Output is correct |
11 |
Correct |
21 ms |
82524 KB |
Output is correct |
12 |
Correct |
22 ms |
83340 KB |
Output is correct |
13 |
Correct |
28 ms |
83288 KB |
Output is correct |
14 |
Correct |
24 ms |
83288 KB |
Output is correct |
15 |
Correct |
24 ms |
83440 KB |
Output is correct |
16 |
Correct |
24 ms |
83292 KB |
Output is correct |
17 |
Correct |
23 ms |
83292 KB |
Output is correct |
18 |
Correct |
24 ms |
83596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2394 ms |
234228 KB |
Output is correct |
2 |
Correct |
2274 ms |
234000 KB |
Output is correct |
3 |
Correct |
2399 ms |
233828 KB |
Output is correct |
4 |
Correct |
2430 ms |
234436 KB |
Output is correct |
5 |
Correct |
1593 ms |
234280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
99664 KB |
Output is correct |
2 |
Correct |
157 ms |
99768 KB |
Output is correct |
3 |
Correct |
128 ms |
99704 KB |
Output is correct |
4 |
Correct |
126 ms |
99564 KB |
Output is correct |
5 |
Correct |
128 ms |
99648 KB |
Output is correct |
6 |
Correct |
121 ms |
99736 KB |
Output is correct |
7 |
Correct |
135 ms |
99688 KB |
Output is correct |
8 |
Correct |
348 ms |
99648 KB |
Output is correct |
9 |
Correct |
67 ms |
83056 KB |
Output is correct |
10 |
Correct |
357 ms |
99744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
82268 KB |
Output is correct |
2 |
Correct |
19 ms |
82520 KB |
Output is correct |
3 |
Correct |
22 ms |
82524 KB |
Output is correct |
4 |
Correct |
18 ms |
82524 KB |
Output is correct |
5 |
Correct |
21 ms |
82524 KB |
Output is correct |
6 |
Correct |
19 ms |
82528 KB |
Output is correct |
7 |
Correct |
19 ms |
82524 KB |
Output is correct |
8 |
Correct |
19 ms |
82588 KB |
Output is correct |
9 |
Correct |
18 ms |
82580 KB |
Output is correct |
10 |
Correct |
19 ms |
82384 KB |
Output is correct |
11 |
Correct |
21 ms |
82524 KB |
Output is correct |
12 |
Correct |
22 ms |
83340 KB |
Output is correct |
13 |
Correct |
28 ms |
83288 KB |
Output is correct |
14 |
Correct |
24 ms |
83288 KB |
Output is correct |
15 |
Correct |
24 ms |
83440 KB |
Output is correct |
16 |
Correct |
24 ms |
83292 KB |
Output is correct |
17 |
Correct |
23 ms |
83292 KB |
Output is correct |
18 |
Correct |
24 ms |
83596 KB |
Output is correct |
19 |
Correct |
397 ms |
117824 KB |
Output is correct |
20 |
Correct |
406 ms |
118064 KB |
Output is correct |
21 |
Correct |
320 ms |
118080 KB |
Output is correct |
22 |
Correct |
317 ms |
118088 KB |
Output is correct |
23 |
Correct |
329 ms |
118320 KB |
Output is correct |
24 |
Correct |
230 ms |
117996 KB |
Output is correct |
25 |
Correct |
258 ms |
117952 KB |
Output is correct |
26 |
Correct |
266 ms |
117972 KB |
Output is correct |
27 |
Correct |
282 ms |
118004 KB |
Output is correct |
28 |
Correct |
297 ms |
117828 KB |
Output is correct |
29 |
Correct |
287 ms |
117976 KB |
Output is correct |
30 |
Correct |
287 ms |
117824 KB |
Output is correct |
31 |
Correct |
287 ms |
118028 KB |
Output is correct |
32 |
Correct |
294 ms |
118064 KB |
Output is correct |
33 |
Correct |
304 ms |
117948 KB |
Output is correct |
34 |
Correct |
253 ms |
118068 KB |
Output is correct |
35 |
Correct |
239 ms |
117916 KB |
Output is correct |
36 |
Correct |
219 ms |
118080 KB |
Output is correct |
37 |
Correct |
234 ms |
118488 KB |
Output is correct |
38 |
Correct |
246 ms |
117892 KB |
Output is correct |
39 |
Correct |
1010 ms |
118084 KB |
Output is correct |
40 |
Correct |
1636 ms |
118300 KB |
Output is correct |
41 |
Correct |
1237 ms |
118096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
82268 KB |
Output is correct |
2 |
Correct |
19 ms |
82520 KB |
Output is correct |
3 |
Correct |
22 ms |
82524 KB |
Output is correct |
4 |
Correct |
18 ms |
82524 KB |
Output is correct |
5 |
Correct |
21 ms |
82524 KB |
Output is correct |
6 |
Correct |
19 ms |
82528 KB |
Output is correct |
7 |
Correct |
19 ms |
82524 KB |
Output is correct |
8 |
Correct |
19 ms |
82588 KB |
Output is correct |
9 |
Correct |
18 ms |
82580 KB |
Output is correct |
10 |
Correct |
19 ms |
82384 KB |
Output is correct |
11 |
Correct |
21 ms |
82524 KB |
Output is correct |
12 |
Correct |
22 ms |
83340 KB |
Output is correct |
13 |
Correct |
28 ms |
83288 KB |
Output is correct |
14 |
Correct |
24 ms |
83288 KB |
Output is correct |
15 |
Correct |
24 ms |
83440 KB |
Output is correct |
16 |
Correct |
24 ms |
83292 KB |
Output is correct |
17 |
Correct |
23 ms |
83292 KB |
Output is correct |
18 |
Correct |
24 ms |
83596 KB |
Output is correct |
19 |
Correct |
2394 ms |
234228 KB |
Output is correct |
20 |
Correct |
2274 ms |
234000 KB |
Output is correct |
21 |
Correct |
2399 ms |
233828 KB |
Output is correct |
22 |
Correct |
2430 ms |
234436 KB |
Output is correct |
23 |
Correct |
1593 ms |
234280 KB |
Output is correct |
24 |
Correct |
204 ms |
99664 KB |
Output is correct |
25 |
Correct |
157 ms |
99768 KB |
Output is correct |
26 |
Correct |
128 ms |
99704 KB |
Output is correct |
27 |
Correct |
126 ms |
99564 KB |
Output is correct |
28 |
Correct |
128 ms |
99648 KB |
Output is correct |
29 |
Correct |
121 ms |
99736 KB |
Output is correct |
30 |
Correct |
135 ms |
99688 KB |
Output is correct |
31 |
Correct |
348 ms |
99648 KB |
Output is correct |
32 |
Correct |
67 ms |
83056 KB |
Output is correct |
33 |
Correct |
357 ms |
99744 KB |
Output is correct |
34 |
Correct |
397 ms |
117824 KB |
Output is correct |
35 |
Correct |
406 ms |
118064 KB |
Output is correct |
36 |
Correct |
320 ms |
118080 KB |
Output is correct |
37 |
Correct |
317 ms |
118088 KB |
Output is correct |
38 |
Correct |
329 ms |
118320 KB |
Output is correct |
39 |
Correct |
230 ms |
117996 KB |
Output is correct |
40 |
Correct |
258 ms |
117952 KB |
Output is correct |
41 |
Correct |
266 ms |
117972 KB |
Output is correct |
42 |
Correct |
282 ms |
118004 KB |
Output is correct |
43 |
Correct |
297 ms |
117828 KB |
Output is correct |
44 |
Correct |
287 ms |
117976 KB |
Output is correct |
45 |
Correct |
287 ms |
117824 KB |
Output is correct |
46 |
Correct |
287 ms |
118028 KB |
Output is correct |
47 |
Correct |
294 ms |
118064 KB |
Output is correct |
48 |
Correct |
304 ms |
117948 KB |
Output is correct |
49 |
Correct |
253 ms |
118068 KB |
Output is correct |
50 |
Correct |
239 ms |
117916 KB |
Output is correct |
51 |
Correct |
219 ms |
118080 KB |
Output is correct |
52 |
Correct |
234 ms |
118488 KB |
Output is correct |
53 |
Correct |
246 ms |
117892 KB |
Output is correct |
54 |
Correct |
1010 ms |
118084 KB |
Output is correct |
55 |
Correct |
1636 ms |
118300 KB |
Output is correct |
56 |
Correct |
1237 ms |
118096 KB |
Output is correct |
57 |
Correct |
2386 ms |
233872 KB |
Output is correct |
58 |
Correct |
2310 ms |
234192 KB |
Output is correct |
59 |
Correct |
2267 ms |
234292 KB |
Output is correct |
60 |
Correct |
2062 ms |
233996 KB |
Output is correct |
61 |
Correct |
2076 ms |
234092 KB |
Output is correct |
62 |
Correct |
2121 ms |
234440 KB |
Output is correct |
63 |
Correct |
1025 ms |
234108 KB |
Output is correct |
64 |
Correct |
1036 ms |
234040 KB |
Output is correct |
65 |
Correct |
1405 ms |
234084 KB |
Output is correct |
66 |
Correct |
1394 ms |
234148 KB |
Output is correct |
67 |
Correct |
1385 ms |
234208 KB |
Output is correct |
68 |
Correct |
1378 ms |
234148 KB |
Output is correct |
69 |
Correct |
1403 ms |
234000 KB |
Output is correct |
70 |
Correct |
1407 ms |
234372 KB |
Output is correct |
71 |
Correct |
1433 ms |
234368 KB |
Output is correct |
72 |
Correct |
1381 ms |
233996 KB |
Output is correct |
73 |
Correct |
1053 ms |
233992 KB |
Output is correct |
74 |
Correct |
1071 ms |
234032 KB |
Output is correct |
75 |
Correct |
1056 ms |
234080 KB |
Output is correct |
76 |
Correct |
1065 ms |
234000 KB |
Output is correct |
77 |
Correct |
1068 ms |
234112 KB |
Output is correct |
78 |
Execution timed out |
3050 ms |
233912 KB |
Time limit exceeded |
79 |
Halted |
0 ms |
0 KB |
- |