#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;
set<int> s;
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) {
ans.val = max(ans.val, *(--right.s.lower_bound(left.mx)) + 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].s = {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]);
for (int x: vec[2 * dum + 1].s) {
vec[dum].s.insert(x);
}
for (int x: vec[2 * dum + 2].s) {
vec[dum].s.insert(x);
}
}
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:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 1; i < intervals.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
772 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
772 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
1628 KB |
Output is correct |
12 |
Correct |
13 ms |
4956 KB |
Output is correct |
13 |
Correct |
13 ms |
5136 KB |
Output is correct |
14 |
Correct |
13 ms |
5212 KB |
Output is correct |
15 |
Correct |
13 ms |
5212 KB |
Output is correct |
16 |
Correct |
120 ms |
5344 KB |
Output is correct |
17 |
Correct |
199 ms |
4828 KB |
Output is correct |
18 |
Correct |
4 ms |
2136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
324 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
392 ms |
73584 KB |
Output is correct |
2 |
Correct |
336 ms |
73840 KB |
Output is correct |
3 |
Correct |
113 ms |
31680 KB |
Output is correct |
4 |
Correct |
115 ms |
31684 KB |
Output is correct |
5 |
Correct |
121 ms |
31676 KB |
Output is correct |
6 |
Correct |
93 ms |
31932 KB |
Output is correct |
7 |
Correct |
97 ms |
31680 KB |
Output is correct |
8 |
Correct |
112 ms |
30852 KB |
Output is correct |
9 |
Correct |
476 ms |
1020 KB |
Output is correct |
10 |
Correct |
96 ms |
30848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
772 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
1628 KB |
Output is correct |
12 |
Correct |
13 ms |
4956 KB |
Output is correct |
13 |
Correct |
13 ms |
5136 KB |
Output is correct |
14 |
Correct |
13 ms |
5212 KB |
Output is correct |
15 |
Correct |
13 ms |
5212 KB |
Output is correct |
16 |
Correct |
120 ms |
5344 KB |
Output is correct |
17 |
Correct |
199 ms |
4828 KB |
Output is correct |
18 |
Correct |
4 ms |
2136 KB |
Output is correct |
19 |
Correct |
1067 ms |
223980 KB |
Output is correct |
20 |
Correct |
1072 ms |
226752 KB |
Output is correct |
21 |
Correct |
915 ms |
226972 KB |
Output is correct |
22 |
Correct |
905 ms |
228108 KB |
Output is correct |
23 |
Correct |
926 ms |
227976 KB |
Output is correct |
24 |
Correct |
737 ms |
228092 KB |
Output is correct |
25 |
Correct |
712 ms |
226960 KB |
Output is correct |
26 |
Correct |
728 ms |
228000 KB |
Output is correct |
27 |
Correct |
727 ms |
226740 KB |
Output is correct |
28 |
Correct |
718 ms |
223876 KB |
Output is correct |
29 |
Correct |
737 ms |
226892 KB |
Output is correct |
30 |
Correct |
728 ms |
223936 KB |
Output is correct |
31 |
Correct |
758 ms |
222488 KB |
Output is correct |
32 |
Correct |
731 ms |
227500 KB |
Output is correct |
33 |
Correct |
721 ms |
223048 KB |
Output is correct |
34 |
Correct |
1282 ms |
227120 KB |
Output is correct |
35 |
Correct |
1262 ms |
227668 KB |
Output is correct |
36 |
Correct |
1277 ms |
227096 KB |
Output is correct |
37 |
Correct |
1218 ms |
226960 KB |
Output is correct |
38 |
Correct |
1401 ms |
227084 KB |
Output is correct |
39 |
Execution timed out |
3068 ms |
226840 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
772 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
1628 KB |
Output is correct |
12 |
Correct |
13 ms |
4956 KB |
Output is correct |
13 |
Correct |
13 ms |
5136 KB |
Output is correct |
14 |
Correct |
13 ms |
5212 KB |
Output is correct |
15 |
Correct |
13 ms |
5212 KB |
Output is correct |
16 |
Correct |
120 ms |
5344 KB |
Output is correct |
17 |
Correct |
199 ms |
4828 KB |
Output is correct |
18 |
Correct |
4 ms |
2136 KB |
Output is correct |
19 |
Runtime error |
324 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |