이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int calcMaxInv(vector<int>& a, int l, int r) {
int maxVal = 0, res = 0;
for (int i = l; i <= r; ++i) {
if (maxVal > a[i]) {
res = max(res, maxVal + a[i]);
}
maxVal = max(maxVal, a[i]);
}
return res;
}
const int K = 470;
const int N = 1e6;
vector<int> s[N / K + 10];
int maxInverse[N / K + 10];
int calcMaxInv2(vector<int>& a, int l, int r) {
int res = 0, maxVal = 0;
if (l % K != 0) {
res = calcMaxInv(a, l, min(r, l - l % K + K - 1));
maxVal = *max_element(a.begin() + l, a.begin() + l - l % K + K - 1 + 1);
l = l - l % K + K;
}
while (l + K - 1 <= r) {
res = max(res, maxInverse[l / K]);
auto it = lower_bound(s[l / K].begin(), s[l / K].end(), maxVal);
if (it != s[l / K].begin()) {
--it;
res = max(res, *it + maxVal);
}
maxVal = max(maxVal, *s[l / K].rbegin());
l += K;
}
if (l <= r) {
for (int i = l; i <= r; ++i) {
if (a[i] < maxVal) {
res = max(res, a[i] + maxVal);
}
}
res = max(res, calcMaxInv(a, l, r));
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> a(n);
for (auto& i : a) {
cin >> i;
}
for (int i = 0; i < n; ++i) {
s[i / K].push_back(a[i]);
}
for (auto& i : s) {
sort(i.begin(), i.end());
}
for (int i = 0; (i + 1) * K - 1 < n; ++i) {
maxInverse[i] = calcMaxInv(a, i * K, (i + 1) * K - 1);
}
int minVal = *min_element(a.begin(), a.end());
while (m--) {
int l, r, k;
cin >> l >> r >> k;
--l, --r;
if (k < minVal) {
cout << is_sorted(a.begin() + l, a.begin() + r + 1) << '\n';
} else {
cout << (calcMaxInv2(a, l, r) <= 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... |