#include <bits/stdc++.h>
using namespace std;
// [3, 5, 1, 8, 4]
// [3, 5, 1, 8, 4]
// each number + the largest one to the left of it, only if it strictly larger
// e.g. 4 + 8 yes
// 8 + 5 no
// 1 + 5 yes
// 5 + 3 yes
// when adding a new entry, update values [i+1, next highest]
// [3, 5, 1, 8, 4]
// [0, 0, 0, 0, 0]
// [0, 0, 0, 0, 8]
// [0, 0, 0, 1, 8]
// [0, 0, 5, 5, 8]
// [0, 3, 5, 5, 8]
// then do range maximum on that
struct Tree {
vector<int> tag, info;
Tree(int size) {
tag.resize(size*4);
info.resize(size*4);
}
void pushdown(int x) {
if (tag[x] == 0) return;
info[x] = tag[x];
if (x*2 < tag.size()) tag[x*2] = tag[x];
if (x*2+1 < tag.size()) tag[x*2+1] = tag[x];
tag[x] = 0;
}
void update(int l, int r, int x, int xl, int xr, int val) {
if (l > r) return;
pushdown(x); // ordered tag
if (l == xl && r == xr) {
tag[x] = val;
} else {
int xm = (xl + xr) / 2;
update(l, min(r, xm), x*2, xl, xm, val);
update(max(l, xm+1), r, x*2+1, xm+1, xr, val);
pushdown(x); pushdown(x*2); pushdown(x*2+1);
info[x] = max(info[x*2], info[x*2+1]);
}
}
int query(int l, int r, int x, int xl, int xr) {
if (l > r) return 0;
pushdown(x);
if (l == xl && r == xr) {
return info[x];
} else {
int xm = (xl + xr) / 2;
int left = query(l, min(r, xm), x*2, xl, xm);
int right = query(max(l, xm+1), r, x*2+1, xm+1, xr);
return max(left, right);
}
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M;
cin >> N >> M;
vector<int> w(N);
for (int i=0; i<N; i++) cin >> w[i];
vector<int> next(N, -1);
stack<int> S;
for (int i=0; i<N; i++) {
while (S.size() && w[S.top()] > w[i]) {
next[S.top()] = i;
S.pop();
}
S.push(i);
}
vector<array<int,4>> queries(M);
for (int i=0; i<M; i++) {
int l, r, k;
cin >> l >> r >> k;
queries[i] = {{l-1, r-1, k, i}};
}
sort(queries.rbegin(), queries.rend());
Tree tree(N);
vector<int> ans(M);
int line = N;
for (int i=0; i<M; i++) {
int l = queries[i][0], r = queries[i][1];
int k = queries[i][2], q = queries[i][3];
while (line > l) {
line--;
if (next[line] != -1) {
tree.update(line+1, next[line], 1, 0, N-1, w[line]);
}
}
int cost = tree.query(l, r, 1, 0, N-1);
if (cost <= k) ans[q] = 1;
}
for (int x : ans) cout << x << "\n";
// for (int i=0; i<N; i++) cout << minTree.query(i, i, 1, 0, N-1).m << " "; cout << "\n";
return 0;
}
Compilation message
sortbooks.cpp: In member function 'void Tree::pushdown(int)':
sortbooks.cpp:30:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | if (x*2 < tag.size()) tag[x*2] = tag[x];
| ~~~~^~~~~~~~~~~~
sortbooks.cpp:31:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if (x*2+1 < tag.size()) tag[x*2+1] = tag[x];
| ~~~~~~^~~~~~~~~~~~
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1100 ms |
60952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
86 ms |
6484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |