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 <algorithm>
#include <iostream>
#include <bitset>
#include <vector>
using namespace std;
vector<int> a;
int w[1000'000];
bitset<1000'000> ans;
int* t[4000'000];
int tans[4000'000], p[4000'000];
void Build(int v, int vl, int vr)
{
t[v] = new int[vr - vl + 1];
p[v] = vr - vl;
if (vl == vr)
{
// ans[v] = 0;
t[v][0] = a[vr];
return;
}
int vm = vl + (vr - vl) / 2;
Build(v * 2, vl, vm);
Build(v * 2 + 1, vm + 1, vr);
merge(t[v * 2], t[v * 2] + vm - vl + 1,
t[v * 2 + 1], t[v * 2 + 1] + vr - vm,
t[v]);
tans[v] = max(tans[v * 2], tans[v * 2 + 1]);
auto smaller = lower_bound(t[v * 2 + 1], t[v * 2 + 1] + vr - vm, t[v * 2][vm - vl]);
if (smaller != t[v * 2 + 1])
tans[v] = max(tans[v], t[v * 2][vm - vl] + *--smaller);
}
struct UPD
{
int v, maxAtLeft, q;
};
vector<UPD> upd;
int Query(int v, int vl, int vr, int l, int r, int maxAtLeft, int q)
{
if (l > vr || vl > r)
return 0;
if (l <= vl && vr <= r)
{
ans[q] = min((bool)ans[q], tans[v] <= w[q]);
/*upd.push_back(UPD());
upd.back().v = v;
upd.back().maxAtLeft = maxAtLeft;
upd.back().q = q;*/
return t[v][vr - vl];
}
int vm = vl + (vr - vl) / 2;
maxAtLeft = max(Query(v * 2, vl, vm, l, r, maxAtLeft, q), maxAtLeft);
maxAtLeft = max(Query(v * 2 + 1, vm + 1, vr, l, r, maxAtLeft, q), maxAtLeft);
return maxAtLeft;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
a.resize(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
Build(1, 0, n - 1);
vector<int>().swap(a);
for (int i = 0; i < m; ++i)
{
ans[i] = 1;
int l, r;
cin >> l >> r >> w[i];
Query(1, 0, n - 1, --l, --r, 0, i);
}
sort(upd.begin(), upd.end(), [](const UPD& l, const UPD& r) {
return l.maxAtLeft > r.maxAtLeft;
});
for (UPD x: upd)
{
int v = x.v, maxAtLeft = x.maxAtLeft, q = x.q;
while (p[v] >= 0 && t[v][p[v]] >= maxAtLeft)
--p[v];
if (p[v] >= 0)
ans[q] = min((bool)ans[q], maxAtLeft + t[v][p[v]] <= w[q]);
}
for (int i = 0; i < m; ++i)
cout << ans[i] << '\n';
}
# | 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... |