This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n, m, w[1000002];
struct nodA {
int max, inv;
set<int>s;
} aint[4000002];
int ans = 0, mx = 0;
void build (int nod, int st, int dr) {
if (st == dr) {
aint[nod].max = w[st];
aint[nod].inv = 0;
aint[nod].s.insert(w[st]);
return;
}
int mid = (st + dr) / 2;
build(2 * nod, st, mid);
build(2 * nod + 1, mid + 1, dr);
aint[nod].inv = max(aint[2 * nod].inv, aint[2 * nod + 1].inv);
aint[nod].max = max(aint[2 * nod].max, aint[2 * nod + 1].max);
auto it = aint[2 * nod + 1].s.lower_bound(aint[2 * nod].max);
if (it != aint[2 * nod + 1].s.begin()) {
it--;
aint[nod].inv = max(aint[nod].inv, aint[2 * nod].max + *it);
}
for (int i = st; i <= dr; i++)
aint[nod].s.insert(w[i]);
}
void query (int nod, int st, int dr, int a, int b) {
if (a <= st && dr <= b) {
if (mx == -1) {
ans = aint[nod].inv;
mx = max(mx, aint[nod].max);
}
else {
ans = max(ans, aint[nod].inv);
auto it = aint[nod].s.lower_bound(mx);
if (it != aint[nod].s.begin()) {
it--;
ans = max(ans, mx + *it);
}
}
return;
}
int mid = (st + dr) / 2;
if (a <= mid)
query(2 * nod, st, mid, a, b);
if (b > mid)
query(2 * nod + 1, mid + 1, dr, a, b);
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> w[i];
build(1, 1, n);
for (int i = 1; i <= m; i++) {
int l, r, k;
cin >> l >> r >> k;
ans = mx = -1;
query(1, 1, n, l, r);
cout << (ans <= k) << "\n";
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |