답안 #495574

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
495574 2021-12-19T11:21:18 Z 600Mihnea 3단 점프 (JOI19_jumps) C++17
0 / 100
4000 ms 15832 KB
#include <bits/stdc++.h>

using namespace std;

const int N = (int) 5e5 + 7;
const int K = 19;
const int INF = (int) 1e9 + 7;
int n;
int q;
int a[N];
int lg[N];
int maxA[K][N];
int nextBigger[N];

int getMaxA(int l, int r) {
  if (l > r) {
    return -INF;
  }
  assert(1 <= l && l <= r && r <= n);
  int k = lg[r - l + 1];
  return max(maxA[k][l], maxA[k][r - (1 << k) + 1]);
}

signed main() {
  ios::sync_with_stdio(0); cin.tie(0);

  //freopen ("input", "r", stdin);


  cin >> n;
  for (int i = 2; i <= n; i++) {
    lg[i] = lg[i / 2] + 1;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    maxA[0][i] = a[i];
  }
  for (int k = 1; (1 << k) <= n; k++) {
    for (int i = 1; i + (1 << k) - 1 <= n; i++) {
      maxA[k][i] = max(maxA[k - 1][i], maxA[k - 1][i + (1 << (k - 1))]);
    }
  }

  {
    vector<int> stk;
    for (int i = n; i >= 1; i--) {
      while (!stk.empty() && a[i] >= a[stk.back()]) {
        stk.pop_back();
      }
      nextBigger[i] = (stk.empty() ? n : stk.back()) + 1;
      stk.push_back(i);
    }
  }

  cin >> q;
  for (int iq = 1; iq <= q; iq++) {
    int l, r, best = 0;
    cin >> l >> r;
    for (int i = l; i <= r; i++) {
      /// j is the maximum on the path [i + 1, j]
      for (int j = i + 1; j <= r; j = nextBigger[j]) {
        int minK = 2 * j - i;
        int maxK = r;

        best = max(best, a[i] + a[j] + getMaxA(minK, maxK));
      }
    }
    cout << best << "\n";
  }


  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4043 ms 15832 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -