Submission #495590

# Submission time Handle Problem Language Result Execution time Memory
495590 2021-12-19T11:51:54 Z 600Mihnea Triple Jump (JOI19_jumps) C++17
0 / 100
4000 ms 49288 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]);
}

struct Offer {
  int value;
  int lft;
  int rgh;
  int mx;
};

struct Question {
  int l;
  int r;
  int ind;
};

vector<Question> questionsAt[N];
vector<Offer> offersAt[N];

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))]);
    }
  }

  { /// just some stack bro
    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 + 1) : stk.back());
      stk.push_back(i);
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= min(n, nextBigger[i]); j = nextBigger[j]) {
      if (2 * j - i <= n) {
        offersAt[2 * j - i].push_back({a[i] + a[j], i, 2 * j - i});
      }
    }
  }

  cin >> q;
  vector<int> sol(q, -1);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    questionsAt[r].push_back({l, r, i});
  }


  for (int r = 1; r <= n; r++) {
    for (int r2 = 1; r2 <= r; r2++) {
      for (auto &it : offersAt[r2]) {
        it.mx = max(it.mx, a[r]);
      }
    }
    for (auto &it : questionsAt[r]) {

      int l = it.l, r = it.r, ind = it.ind, best = 0;
      for (int r2 = 1; r2 <= n; r2++) {
        for (auto &offer : offersAt[r2]) {
          if (offer.lft >= l) {
            best = max(best, offer.value + offer.mx);
          }
        }
      }

      sol[ind] = best;
    }
  }

  for (auto &x : sol) {
    assert(x != -1);
    cout << x << "\n";
  }



  return 0;
}

Compilation message

jumps.cpp: In function 'int main()':
jumps.cpp:96:21: warning: unused variable 'r' [-Wunused-variable]
   96 |       int l = it.l, r = it.r, ind = it.ind, best = 0;
      |                     ^
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23728 KB Output is correct
2 Incorrect 12 ms 23792 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23728 KB Output is correct
2 Incorrect 12 ms 23792 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4038 ms 49288 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23728 KB Output is correct
2 Incorrect 12 ms 23792 KB Output isn't correct
3 Halted 0 ms 0 KB -