답안 #783005

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
783005 2023-07-14T14:08:19 Z tvladm2009 3단 점프 (JOI19_jumps) C++17
46 / 100
4000 ms 108556 KB
#include <bits/stdc++.h>

using namespace std;
const int N = (int) 5e5 + 7;
const int INF = (int) 2e9;
int n, a[N], nextBigger[N], q;

struct rmq {
  const int K = 20;
  vector<vector<int>> tab;
  vector<int> lg2;
  int n;

  void init(int nn) {
    n = nn;
    lg2.resize(n + 1);
    for (int i = 2; i <= n; i++) {
      lg2[i] = 1 + lg2[i / 2];
    }
    tab.resize(K);
    for (int i = 0; i < K; i++) {
      tab[i].resize(n + 1);
    }
  }

  void build(int a[]) {
    for (int i = 1; i <= n; i++) {
      tab[0][i] = a[i];
    }
    for (int k = 1; k < K; k++) {
      for (int i = 1; i + (1 << k) - 1 <= n; i++) {
        tab[k][i] = max(tab[k - 1][i], tab[k - 1][i + (1 << (k - 1))]);
      }
    }
  }

  int query(int x, int y) {
    if (x > y) {
      return -INF;
    }
    assert(1 <= x && x <= y && y <= n);
    int p = lg2[y - x + 1];
    return max(tab[p][x], tab[p][y - (1 << p) + 1]);
  }
};

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

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

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  rmq rmq;
  rmq.init(n);
  rmq.build(a);
  if (n <= 5000) {
    vector<vector<int>> mx(n + 1, vector<int>(n + 1));
    for (int l = 1; l <= n; l++) {
      for (int r = l + 1; r <= n; r++) { 
        mx[l][r] = a[l] + a[r] + rmq.query(l + 1, (l + r) / 2);
      }
    }
    for (int l = n; l >= 1; l--) {
      for (int r = l + 2; r <= n; r++) {
        mx[l][r] = max(mx[l][r], mx[l][r - 1]);
        mx[l][r] = max(mx[l][r], mx[l + 1][r]);
      }
    }
    cin >> q;
    while (q--) {
      int l, r;
      cin >> l >> r;
      cout << mx[l][r] << "\n";
    }
    return 0;
  }

  {
    vector<int> st;
    for (int i = n; i >= 1; i--) {
      while (!st.empty() && a[i] >= a[st.back()]) {
        st.pop_back();
      }
      if (st.empty()) {
        nextBigger[i] = n + 1;
      } else {
        nextBigger[i] = st.back();
      }
      st.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++) {
      for (int j = i + 1; j <= min(n, nextBigger[i]); j = nextBigger[j]) {
        best = max(best, a[i] + a[j] + rmq.query(2 * j - i, r));
      }
    }
    cout << best << "\n";
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 284 ms 108512 KB Output is correct
12 Correct 267 ms 108420 KB Output is correct
13 Correct 286 ms 108452 KB Output is correct
14 Correct 274 ms 108556 KB Output is correct
15 Correct 296 ms 108476 KB Output is correct
16 Correct 274 ms 107784 KB Output is correct
17 Correct 297 ms 107768 KB Output is correct
18 Correct 301 ms 107788 KB Output is correct
19 Correct 287 ms 108452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 18388 KB Output is correct
2 Correct 25 ms 19316 KB Output is correct
3 Correct 29 ms 18492 KB Output is correct
4 Correct 29 ms 18276 KB Output is correct
5 Correct 29 ms 18268 KB Output is correct
6 Correct 26 ms 18268 KB Output is correct
7 Correct 27 ms 18404 KB Output is correct
8 Correct 27 ms 18372 KB Output is correct
9 Correct 27 ms 18296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 284 ms 108512 KB Output is correct
12 Correct 267 ms 108420 KB Output is correct
13 Correct 286 ms 108452 KB Output is correct
14 Correct 274 ms 108556 KB Output is correct
15 Correct 296 ms 108476 KB Output is correct
16 Correct 274 ms 107784 KB Output is correct
17 Correct 297 ms 107768 KB Output is correct
18 Correct 301 ms 107788 KB Output is correct
19 Correct 287 ms 108452 KB Output is correct
20 Correct 28 ms 18388 KB Output is correct
21 Correct 25 ms 19316 KB Output is correct
22 Correct 29 ms 18492 KB Output is correct
23 Correct 29 ms 18276 KB Output is correct
24 Correct 29 ms 18268 KB Output is correct
25 Correct 26 ms 18268 KB Output is correct
26 Correct 27 ms 18404 KB Output is correct
27 Correct 27 ms 18372 KB Output is correct
28 Correct 27 ms 18296 KB Output is correct
29 Execution timed out 4019 ms 50056 KB Time limit exceeded
30 Halted 0 ms 0 KB -