이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <set>
using namespace std;
int a[1000000];
set<int> t[4000000];
int ans[4000000];
int Max(const set<int>& x)
{
if (x.empty())
cerr << "Max got empty set as argument.";
return *x.rbegin();
}
void Build(int v, int vl, int vr)
{
if (vl == vr)
{
ans[v] = 0;
t[v].insert(a[vr]);
return;
}
int m = vl + (vr - vl) / 2;
Build(v * 2, vl, m);
Build(v * 2 + 1, m + 1, vr);
t[v].insert(t[v * 2].begin(), t[v * 2].end());
t[v].insert(t[v * 2 + 1].begin(), t[v * 2 + 1].end());
ans[v] = max(ans[v * 2], ans[v * 2 + 1]);
auto smaller = t[v * 2 + 1].lower_bound(Max(t[v * 2]));
if (smaller != t[v * 2 + 1].begin())
{
--smaller;
ans[v] = max(ans[v], Max(t[v * 2]) + *smaller);
}
}
int Query(int v, int vl, int vr, int l, int r, int maxAtLeft = 0)
{
// cerr << "(" << l << ", " << r << ") / (" << vl << ", " << vr << ")\n";
if (vl == l && vr == r)
{
int res = 0;
auto small = t[v].lower_bound(maxAtLeft);
if (small != t[v].begin())
{
--small;
res = max(res, maxAtLeft + *small);
}
return max(res, ans[v]);
}
int m = vl + (vr - vl) / 2;
if (r <= m)
return Query(v * 2, vl, m, l, r, maxAtLeft);
else if (l > m)
return Query(v * 2 + 1, m + 1, vr, l, r, maxAtLeft);
int res = Query(v * 2, vl, m, l, m, maxAtLeft);
res = max(res, Query(v * 2, m + 1, vr, m + 1, r, max(Max(t[v * 2]), maxAtLeft)));
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i)
cin >> a[i];
Build(1, 0, n - 1);
while (m--)
{
int l, r, w;
cin >> l >> r >> w;
cout << (Query(1, 0, n - 1, l - 1, r - 1) <= w) << '\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... |