#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;
int l;
int r;
Node (int l, int r) {
this->mx = INT_MIN, this->mn = INT_MAX, this->val = 0, this->l = l, this->r = r;
}
};
Node id = {-1, -1};
struct MergeSortTree {
vector<set<int>> vec;
vector<int> v;
void build (int dum, int tl, int tr) {
if (tl == tr) {
vec[dum] = {v[tl]};
return;
}
build(2 * dum + 1, tl, (tl + tr)/2);
build(2 * dum + 2, (tl + tr)/2 + 1, tr);
std::set_union(std::begin(vec[2 * dum + 1]), std::end(vec[2 * dum + 1]),
std::begin(vec[2 * dum + 2]), std::end(vec[2 * dum + 2]),
std::inserter(vec[dum], std::begin(vec[dum])));
}
int query (int dum, int tl, int tr, int l, int r, int k) {
if (tl >= l && tr <= r) {
auto it = vec[dum].lower_bound(k);
if (it == vec[dum].begin()) {
return -INT_MAX;
}
return *(--it);
}
if (tl > r || l > tr) {
return -INT_MAX;
}
return max(query(2 * dum + 1, tl, (tl + tr)/2, l, r, k), query(2 * dum + 2, (tl + tr)/2 + 1, tr, l, r, k));
}
int query (int l, int r, int k) {
return query(0, 0, (int)vec.size()/2 - 1, l, r, k);
}
MergeSortTree (vector<int> v) {
assert(__builtin_popcount(v.size()) == 1);
this->v = v;
vec.reserve((int)v.size() * 2);
for (int i = 0; i < 2 * v.size(); i++) {
vec.push_back({});
}
build(0, 0, (int)vec.size()/2 - 1);
}
};
struct SegmentTree {
vector<Node> vec;
vector<int> nodes;
MergeSortTree ms = MergeSortTree({0});
Node merge (Node left, Node right) {
if (left.l == -1) {
return right;
}
if (right.l == -1) {
return left;
}
assert(left.r == right.l - 1);
Node ans = Node(left.l, right.r);
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, ms.query(right.l, right.r, left.mx - 1) + 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;
}
Node build (int dum, int tl, int tr) {
if (tl == tr) {
vec[dum] = Node(tl, tr);
vec[dum].mn = vec[dum].mx = nodes[tl];
return vec[dum];
}
build(2 * dum + 1, tl, (tl + tr)/2);
build(2 * dum + 2, (tl + tr)/2 + 1, tr);
return vec[dum] = merge( build(2 * dum + 1, tl, (tl + tr)/2), build(2 * dum + 2, (tl + tr)/2 + 1, tr));
}
SegmentTree (vector<int> nodes) {
this->nodes = nodes;
int n = nodes.size();
n = (1 << (int)log2(n)) * 2;
vec.assign(2 * n, Node(0, 0));
while (this->nodes.size() < vec.size()) {
this->nodes.push_back((int)1e9);
}
this->ms = MergeSortTree(this->nodes);
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 constructor 'MergeSortTree::MergeSortTree(std::vector<int>)':
sortbooks.cpp:55:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = 0; i < 2 * v.size(); i++) {
| ~~^~~~~~~~~~~~~~
sortbooks.cpp: In member function 'int SegmentTree::query(int, int)':
sortbooks.cpp:99:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int i = 1; i < intervals.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
1112 KB |
Output is correct |
7 |
Correct |
6 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
860 KB |
Output is correct |
9 |
Correct |
5 ms |
604 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
1112 KB |
Output is correct |
7 |
Correct |
6 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
860 KB |
Output is correct |
9 |
Correct |
5 ms |
604 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
79 ms |
2136 KB |
Output is correct |
12 |
Correct |
1197 ms |
7048 KB |
Output is correct |
13 |
Correct |
1171 ms |
7048 KB |
Output is correct |
14 |
Correct |
1186 ms |
7136 KB |
Output is correct |
15 |
Correct |
1173 ms |
7140 KB |
Output is correct |
16 |
Correct |
1121 ms |
7136 KB |
Output is correct |
17 |
Correct |
1088 ms |
6584 KB |
Output is correct |
18 |
Correct |
1102 ms |
4088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
187 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3071 ms |
101308 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
1112 KB |
Output is correct |
7 |
Correct |
6 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
860 KB |
Output is correct |
9 |
Correct |
5 ms |
604 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
79 ms |
2136 KB |
Output is correct |
12 |
Correct |
1197 ms |
7048 KB |
Output is correct |
13 |
Correct |
1171 ms |
7048 KB |
Output is correct |
14 |
Correct |
1186 ms |
7136 KB |
Output is correct |
15 |
Correct |
1173 ms |
7140 KB |
Output is correct |
16 |
Correct |
1121 ms |
7136 KB |
Output is correct |
17 |
Correct |
1088 ms |
6584 KB |
Output is correct |
18 |
Correct |
1102 ms |
4088 KB |
Output is correct |
19 |
Runtime error |
414 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
1112 KB |
Output is correct |
7 |
Correct |
6 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
860 KB |
Output is correct |
9 |
Correct |
5 ms |
604 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
79 ms |
2136 KB |
Output is correct |
12 |
Correct |
1197 ms |
7048 KB |
Output is correct |
13 |
Correct |
1171 ms |
7048 KB |
Output is correct |
14 |
Correct |
1186 ms |
7136 KB |
Output is correct |
15 |
Correct |
1173 ms |
7140 KB |
Output is correct |
16 |
Correct |
1121 ms |
7136 KB |
Output is correct |
17 |
Correct |
1088 ms |
6584 KB |
Output is correct |
18 |
Correct |
1102 ms |
4088 KB |
Output is correct |
19 |
Runtime error |
187 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |