이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 2;
const int L = 20;
int n, m;
int rx[L][N];
int mx[L][N];
int mxr[L][N];
int rmx(int l, int r) {
int w = __lg(r - l + 1);
return max(mx[w][l], mx[w][r - (1 << w) + 1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> mx[0][i];
}
stack<int> st;
for (int i = n; i >= 1; i--) {
while (!st.empty() && mx[0][i] > mx[0][st.top()]) {
st.pop();
}
rx[0][i] = (st.empty() ? n + 1 : st.top());
st.push(i);
}
rx[0][n + 1] = n + 1;
for (int j = 1; j < L; j++) {
for (int i = 1; i <= n + 1; i++) {
rx[j][i] = rx[j - 1][rx[j - 1][i]];
}
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
mx[j][i] = max(mx[j - 1][i], mx[j - 1][i + (1 << (j - 1))]);
}
}
for (int i = 1; i <= n + 1; i++) {
if (rx[0][i] > i + 1) {
mxr[0][i] = mx[0][i] + rmx(i + 1, rx[0][i] - 1);
}
}
for (int j = 1; j < L; j++) {
for (int i = 1; i <= n + 1; i++) {
mxr[j][i] = max(mxr[j - 1][i], mxr[j - 1][rx[j - 1][i]]);
}
}
while (m--) {
int l, r, k;
cin >> l >> r >> k;
bool f = true;
for (int i = L - 1; i >= 0; i--) {
if (rx[i][l] <= r) {
f &= (mxr[i][l] <= k);
l = rx[i][l];
}
}
if (l < r) {
f &= (mx[0][l] + rmx(l + 1, r) <= k);
}
cout << f << '\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... |