이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
const int mxN = 5005;
const int lgN = 14;
const int mxQ = 105;
int N, Q, A[mxN];
struct SparseTable {
int T[mxN][lgN];
void build() {
FOR(i,1,N) T[i][0] = i;
FOR(k,1,lgN-1){
FOR(i,1,N-(1<<k)+1){
int p = T[i][k-1], q = T[i+(1<<(k-1))][k-1];
T[i][k] = (A[p] > A[q] ? p : q);
}
}
}
int query(int L, int R) {
if (L > R) return -1e9;
int k = floor(log2(R-L+1));
int p = T[L][k], q = T[R-(1<<k)+1][k];
return max(A[p],A[q]);
}
} st;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
FOR(i,1,N){
cin >> A[i];
}
st.build();
cin >> Q;
FOR(i,1,Q){
int L, R; cin >> L >> R;
int ans = 0;
FOR(x,L,R){
FOR(y,x+1,R){
ans = max(ans,A[x]+A[y]+st.query(max(L,x-(y-x)),x-1));
}
}
cout << ans << '\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... |