Submission #348832

# Submission time Handle Problem Language Result Execution time Memory
348832 2021-01-15T20:35:30 Z spatarel Secret (JOI14_secret) C++17
100 / 100
521 ms 4844 KB
#include "secret.h"
#include <map>

struct StructQuery {
  int left;
  int right;
};

bool operator < (const StructQuery &a, const StructQuery &b) {
  return (a.left < b.left)
      || (a.left == b.left && a.right < b.right);
}

int n;
std::map<StructQuery, int> secrets;

void querySecrets(int left, int right, int A[]) {
  if (left == right) {
    secrets[StructQuery{left, right}] = A[left];
  } else {
    int avg = (left + right) / 2;
    secrets[StructQuery{avg, avg}] = A[avg];
    for (int i = avg - 1; i >= left; i--) {
      secrets[StructQuery{i, avg}] =
          Secret(A[i], secrets[StructQuery{i + 1, avg}]);
    }
    secrets[StructQuery{avg + 1, avg + 1}] = A[avg + 1];
    for (int i = avg + 2; i <= right; i++) {
      secrets[StructQuery{avg + 1, i}] =
          Secret(secrets[StructQuery{avg + 1, i - 1}], A[i]);
    }
    querySecrets(left, avg, A);
    querySecrets(avg + 1, right, A);
  }
}

void Init(int N, int A[]) {
  n = N;
  querySecrets(0, N - 1, A);
}

int Query(int L, int R) {
  int left = 0;
  int right = n - 1;
  while (true) {
    int avg = (left + right) / 2;
    if (R == avg || L == avg + 1) {
      return secrets[StructQuery{L, R}];
    } else if (L <= avg && avg + 1 <= R) {
      return Secret(secrets[StructQuery{L, avg}], secrets[StructQuery{avg + 1, R}]);
    } else if (R < avg) {
      right = avg;
    } else { // avg + 1 < L
      left = avg + 1;
    }
  }
  return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 144 ms 2668 KB Output is correct - number of calls to Secret by Init = 3578, maximum number of calls to Secret by Query = 1
2 Correct 146 ms 2668 KB Output is correct - number of calls to Secret by Init = 3586, maximum number of calls to Secret by Query = 1
3 Correct 145 ms 2668 KB Output is correct - number of calls to Secret by Init = 3595, maximum number of calls to Secret by Query = 1
4 Correct 510 ms 4844 KB Output is correct - number of calls to Secret by Init = 7969, maximum number of calls to Secret by Query = 1
5 Correct 521 ms 4844 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1
6 Correct 506 ms 4844 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1
7 Correct 510 ms 4844 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1
8 Correct 513 ms 4736 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1
9 Correct 511 ms 4844 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1
10 Correct 516 ms 4844 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1