답안 #988124

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
988124 2024-05-24T05:41:18 Z icyalmond 비밀 (JOI14_secret) C++17
100 / 100
363 ms 4692 KB
#include "secret.h"

int n, pref[1005][15], a[1005], mask[1005];

void build(int l, int r, int h)
{
    if (l == r) return;
    int m = l + r >> 1;
    pref[m][h] = a[m];
    for (int i = m - 1; i >= l; i--) pref[i][h] = Secret(a[i], pref[i + 1][h]);
    pref[m + 1][h] = a[m + 1];
    for (int i = m + 2; i <= r; i++) pref[i][h] = Secret(pref[i - 1][h], a[i]);
    for (int i = m + 1; i <= r; i++) mask[i] |= 1 << h;
    if (l != m) build(l, m, h + 1);
    if (m + 1 != r) build(m + 1, r, h + 1);
}

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

int Query(int L, int R)
{
    if (L == R) return(a[L + 1]);
    else
    {
//        int h = __builtin_ctz(mask[L + 1] ^ mask[R + 1]);
//        return(Secret(pref[L + 1][h], pref[R + 1][h]));
        int l = 1, r = n, h = 0;
        while (l < r)
        {
            int m = l + r >> 1;
            if (L + 1 <= m && m + 1 <= R + 1) return(Secret(pref[L + 1][h], pref[R + 1][h]));
            else if (L + 1 > m)
            {
                l = m + 1;
                h++;
            }
            else if (m + 1 > R + 1)
            {
                r = m;
                h++;
            }
        }
    }
}

//coded by icyalmond

Compilation message

secret.cpp: In function 'void build(int, int, int)':
secret.cpp:8:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    8 |     int m = l + r >> 1;
      |             ~~^~~
secret.cpp: In function 'int Query(int, int)':
secret.cpp:35:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |             int m = l + r >> 1;
      |                     ~~^~~
secret.cpp:49:1: warning: control reaches end of non-void function [-Wreturn-type]
   49 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 3668 KB Output is correct - number of calls to Secret by Init = 3578, maximum number of calls to Secret by Query = 1
2 Correct 101 ms 2384 KB Output is correct - number of calls to Secret by Init = 3586, maximum number of calls to Secret by Query = 1
3 Correct 101 ms 2384 KB Output is correct - number of calls to Secret by Init = 3595, maximum number of calls to Secret by Query = 1
4 Correct 357 ms 4436 KB Output is correct - number of calls to Secret by Init = 7969, maximum number of calls to Secret by Query = 1
5 Correct 362 ms 4692 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1
6 Correct 356 ms 4432 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1
7 Correct 357 ms 4432 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1
8 Correct 363 ms 4384 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1
9 Correct 361 ms 4624 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1
10 Correct 358 ms 4436 KB Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1