#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e6 + 7;
const int SZ = exp2(ceil(log2(mxN)));
int seg[2 * SZ];
void update(int pos, int val) {
seg[pos + SZ] = val;
for (pos /= 2; pos; pos /= 2) {
seg[pos] = max(seg[2 * pos], seg[2 * pos + 1]);
}
}
int query(int a, int b, int ind = 1, int l = 0, int r = SZ - 1) {
if (r < a || l > b) {
return 0;
}
if (a <= l && r <= b) {
return seg[ind];
}
int mid = (l + r) / 2;
return max(query(a, b, 2 * ind, l, mid),
query(a, b, 2 * ind + 1, mid + 1, r));
}
struct Inv {
int l, r;
bool operator<(Inv x) {
if (l == x.l) {
if (r - l + 1 == x.r - x.l + 1) {
return r < x.r;
}
return r - l + 1 < x.r - x.l + 1;
}
return l < x.l;
}
};
int arr[mxN];
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, q;
cin >> n >> q;
vector<int> st;
vector<Inv> invs;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
while (st.size() && arr[i] > arr[st.back()]) {
st.pop_back();
}
if (st.size()) {
Inv inv;
inv.l = st.back();
inv.r = i;
invs.push_back(inv);
}
st.push_back(i);
}
sort(invs.begin(), invs.end());
for (int i = 0; i < invs.size(); ++i) {
update(i, arr[invs[i].l] + arr[invs[i].r]);
}
while (q--) {
int l, r, k;
cin >> l >> r >> k;
int lb = 0, rb = invs.size() - 1, indl = -1;
while (lb <= rb) {
int mb = (lb + rb) / 2;
if (invs[mb].l < l) {
lb = mb + 1;
}
else if (invs[mb].l > r || invs[mb].r > r) {
rb = mb - 1;
}
else {
indl = mb;
rb = mb - 1;
}
}
if (indl == -1) {
cout << "0\n";
continue;
}
lb = 0, rb = invs.size() - 1;
int indr = indl;
while (lb <= rb) {
int mb = (lb + rb) / 2;
if (invs[mb].l < l) {
lb = mb + 1;
}
else if (invs[mb].l > r || invs[mb].r > r) {
rb = mb - 1;
}
else {
indr = mb;
lb = mb + 1;
}
}
cout << (query(indl, indr) <= k ? 1 : 0) << "\n";
}
}
Compilation message
sortbooks.cpp: In function 'int32_t main()':
sortbooks.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Inv>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < invs.size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1651 ms |
51888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
104 ms |
4424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |