답안 #565987

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
565987 2022-05-21T16:02:55 Z SSRS 비밀 (JOI14_secret) C++14
100 / 100
471 ms 4416 KB
#include <bits/stdc++.h>
#include "secret.h"
using namespace std;
template <typename T>
struct disjoint_sparse_table{
  function<T(T, T)> f;
  int N;
  vector<T> A;
  vector<vector<T>> D;
  disjoint_sparse_table(){
  }
  disjoint_sparse_table(vector<T> &A, function<T(T, T)> f): A(A), f(f){
    int N2 = A.size();
    N = 1;
    int LOG = 0;
    while (N < N2){
      N *= 2;
      LOG++;
    }
    A.resize(N);
    for (int i = N2; i < N; i++){
      A[i] = -1;
    }
    D = vector<vector<T>>(LOG, vector<T>(N));
    for (int i = 0; i < LOG; i++){
      for (int j = 0; j < N; j += 1 << (i + 1)){
        int d = 1 << i;
        D[i][j + d - 1] = A[j + d - 1];
        for (int k = j + d - 2; k >= j; k--){
          D[i][k] = f(A[k], D[i][k + 1]);
        }
        D[i][j + d] = A[j + d];
        for (int k = j + d + 1; k < j + d * 2; k++){
          D[i][k] = f(D[i][k - 1], A[k]);
        }
      }
    }
  }
  T query(int L, int R){
    if (R - L == 1){
      return A[L];
    } else {
      R--;
      int b = 31 - __builtin_clz(R ^ L);
      return f(D[b][L], D[b][R]);
    }
  }
};
int f(int a, int b){
  if (a == -1 || b == -1){
    return -1;
  }
  return Secret(a, b);
}
disjoint_sparse_table<int> DST;
void Init(int N, int A[]){
  vector<int> A2(N);
  for (int i = 0; i < N; i++){
    A2[i] = A[i];
  }
  DST = disjoint_sparse_table<int>(A2, f);
}
int Query(int L, int R){
  R++;
  return DST.query(L, R);
}

Compilation message

secret.cpp: In instantiation of 'disjoint_sparse_table<T>::disjoint_sparse_table(std::vector<_Tp>&, std::function<T(T, T)>) [with T = int]':
secret.cpp:61:41:   required from here
secret.cpp:8:13: warning: 'disjoint_sparse_table<int>::A' will be initialized after [-Wreorder]
    8 |   vector<T> A;
      |             ^
secret.cpp:6:21: warning:   'std::function<int(int, int)> disjoint_sparse_table<int>::f' [-Wreorder]
    6 |   function<T(T, T)> f;
      |                     ^
secret.cpp:12:3: warning:   when initialized here [-Wreorder]
   12 |   disjoint_sparse_table(vector<T> &A, function<T(T, T)> f): A(A), f(f){
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 2392 KB Output is correct - number of calls to Secret by Init = 3578, maximum number of calls to Secret by Query = 1
2 Correct 130 ms 2356 KB Output is correct - number of calls to Secret by Init = 3586, maximum number of calls to Secret by Query = 1
3 Correct 131 ms 2424 KB Output is correct - number of calls to Secret by Init = 4097, maximum number of calls to Secret by Query = 1
4 Correct 471 ms 4416 KB Output is correct - number of calls to Secret by Init = 7979, maximum number of calls to Secret by Query = 1
5 Correct 467 ms 4264 KB Output is correct - number of calls to Secret by Init = 7993, maximum number of calls to Secret by Query = 1
6 Correct 453 ms 4220 KB Output is correct - number of calls to Secret by Init = 7993, maximum number of calls to Secret by Query = 1
7 Correct 467 ms 4280 KB Output is correct - number of calls to Secret by Init = 7993, maximum number of calls to Secret by Query = 1
8 Correct 457 ms 4248 KB Output is correct - number of calls to Secret by Init = 7993, maximum number of calls to Secret by Query = 1
9 Correct 442 ms 4320 KB Output is correct - number of calls to Secret by Init = 7993, maximum number of calls to Secret by Query = 1
10 Correct 435 ms 4324 KB Output is correct - number of calls to Secret by Init = 7993, maximum number of calls to Secret by Query = 1