답안 #52468

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52468 2018-06-26T05:04:26 Z 강태규(#1363) 비밀 (JOI14_secret) C++11
100 / 100
668 ms 5700 KB
#include "secret.h"
#include <map>

using namespace std;
typedef pair<int, int> pii;

map<pii, int> mp;
map<pii, int>::iterator it;
int query(int x, int y) {
    if ((it = mp.find(pii(x, y))) != mp.end()) return it->second;
    return mp[pii(x, y)] = Secret(x, y);
}

int n;
int a[1000];
int pr[1000][10];

void dnc(int s, int e, int d = 0) {
    if (e - s < 2) return;
    int m = (s + e) / 2;
    int x;
    pr[m][d] = a[m];
    for (int i = m - 1; i >= s; --i) {
        pr[i][d] = query(a[i], pr[i + 1][d]);
    }
    pr[m + 1][d] = a[m + 1];
    for (int i = m + 2; i <= e; ++i) {
        pr[i][d] = query(pr[i - 1][d], a[i]);
    }
    dnc(s, m - 1, d + 1);
    dnc(m + 2, e, d + 1);
}

void Init(int N, int A[]) {
    n = N;
    for (int i = 0; i < n; ++i) {
        a[i] = A[i];
    }
    dnc(0, n - 1);
}

int getAns(int s, int e, int l, int r, int d = 0) {
    if (e - s < 2) throw 0;
    int m = (s + e) / 2;
    if (l <= m && m < r) return query(pr[l][d], pr[r][d]);
    if (l == m + 1) return pr[r][d];
    if (r == m) return pr[l][d];
    if (r < m) return getAns(s, m - 1, l, r, d + 1);
    if (m + 1 < l) return getAns(m + 2, e, l, r, d + 1);
    throw 0;
}

int Query(int L, int R) {
    if (R - L < 2) {
        if (L == R) return a[L];
        return query(a[L], a[R]);
    }
    return getAns(0, n - 1, L, R);
}

Compilation message

secret.cpp: In function 'void dnc(int, int, int)':
secret.cpp:21:9: warning: unused variable 'x' [-Wunused-variable]
     int x;
         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 169 ms 3196 KB Output is correct - number of calls to Secret by Init = 3084, maximum number of calls to Secret by Query = 1
2 Correct 170 ms 3316 KB Output is correct - number of calls to Secret by Init = 3092, maximum number of calls to Secret by Query = 1
3 Correct 171 ms 3316 KB Output is correct - number of calls to Secret by Init = 3100, maximum number of calls to Secret by Query = 1
4 Correct 595 ms 5484 KB Output is correct - number of calls to Secret by Init = 6988, maximum number of calls to Secret by Query = 1
5 Correct 588 ms 5588 KB Output is correct - number of calls to Secret by Init = 6996, maximum number of calls to Secret by Query = 1
6 Correct 578 ms 5588 KB Output is correct - number of calls to Secret by Init = 6996, maximum number of calls to Secret by Query = 1
7 Correct 613 ms 5652 KB Output is correct - number of calls to Secret by Init = 6996, maximum number of calls to Secret by Query = 1
8 Correct 609 ms 5652 KB Output is correct - number of calls to Secret by Init = 6996, maximum number of calls to Secret by Query = 1
9 Correct 668 ms 5700 KB Output is correct - number of calls to Secret by Init = 6996, maximum number of calls to Secret by Query = 1
10 Correct 607 ms 5700 KB Output is correct - number of calls to Secret by Init = 6996, maximum number of calls to Secret by Query = 1