# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
344148 | Nurlykhan | Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) | C++17 | 3066 ms | 112336 KiB |
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;
const int N = (int)1e6 + 10;
const int K = 450;
int n, m;
int w[N];
int ans[N];
int L[N], R[N], k[N];
vector<int> query[N];
int suff_w[N], suff_mood[N];
void solve(int l = 0, int r = n - 1) {
if (l == r)
return;
int mid = (l + r) / 2;
// 5 4 100
set<int> s; // TODO: replace set with fenwick
suff_w[mid + 1] = suff_mood[mid + 1] = 0;
for (int i = mid; i >= l; i--) {
suff_w[i] = max(suff_w[i + 1], w[i]);
suff_mood[i] = suff_mood[i + 1];
auto it = s.lower_bound(w[i]);
if (it != s.begin()) {
it--;
suff_mood[i] = max(suff_mood[i], *it + w[i]);
}
s.insert(w[i]);
}
s.clear();
int mx = 0, mxx = 0;
for (int i = mid + 1; i <= r; i++) {
if (mxx <= w[i]) {
mxx = w[i];
} else {
mx = max(mx, mxx + w[i]);
}
s.insert(w[i]);
// TODO: optimize iteration over queries
for (auto j : query[i]) {
if (L[j] >= l && L[j] <= mid) {
int cur_mx = max(suff_mood[L[j]], mx);
int tmp = suff_w[L[j]];
auto it = s.lower_bound(tmp);
if (it != s.begin()) {
it--;
cur_mx = max(cur_mx, *it + tmp);
}
if (k[j] >= cur_mx) {
ans[j] = 1;
} else {
ans[j] = 0;
}
}
}
}
solve(l, mid);
solve(mid + 1, r);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &w[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &L[i], &R[i], &k[i]);
--L[i], --R[i];
query[R[i]].push_back(i);
}
solve();
for (int i = 0; i < m; i++) {
if (L[i] == R[i]) ans[i] = 1;
printf("%d\n", ans[i]);
}
return 0;
}
Compilation message (stderr)
# | 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... |