結果

問題 No.318 学学学学学
ユーザー brthyyjpbrthyyjp
提出日時 2021-12-28 16:07:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 523 ms / 2,000 ms
コード長 3,210 bytes
コンパイル時間 1,094 ms
コンパイル使用メモリ 82,456 KB
実行使用メモリ 130,024 KB
最終ジャッジ日時 2024-10-02 08:43:23
合計ジャッジ時間 10,086 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
79,200 KB
testcase_01 AC 158 ms
84,568 KB
testcase_02 AC 171 ms
87,220 KB
testcase_03 AC 140 ms
81,996 KB
testcase_04 AC 164 ms
85,784 KB
testcase_05 AC 522 ms
129,996 KB
testcase_06 AC 508 ms
113,152 KB
testcase_07 AC 427 ms
108,376 KB
testcase_08 AC 363 ms
103,308 KB
testcase_09 AC 317 ms
101,052 KB
testcase_10 AC 252 ms
98,832 KB
testcase_11 AC 523 ms
130,024 KB
testcase_12 AC 415 ms
112,892 KB
testcase_13 AC 381 ms
108,192 KB
testcase_14 AC 342 ms
103,488 KB
testcase_15 AC 304 ms
101,092 KB
testcase_16 AC 241 ms
98,560 KB
testcase_17 AC 394 ms
112,460 KB
testcase_18 AC 336 ms
112,348 KB
testcase_19 AC 374 ms
112,476 KB
testcase_20 AC 251 ms
96,744 KB
testcase_21 AC 36 ms
53,620 KB
testcase_22 AC 36 ms
52,856 KB
testcase_23 AC 38 ms
54,384 KB
testcase_24 AC 36 ms
53,308 KB
testcase_25 AC 38 ms
53,196 KB
testcase_26 AC 36 ms
52,752 KB
testcase_27 AC 37 ms
54,264 KB
testcase_28 AC 36 ms
53,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class LazySegTree:
    # RMQ and RUQ
    def __init__(self, init_val, seg_ide, lazy_ide, segfunc):
        self.n = len(init_val)
        self.num = 2**(self.n-1).bit_length()

        self.seg_ide = seg_ide
        self.lazy_ide = lazy_ide

        self.segfunc = segfunc

        # seg, lazy: 1-indexed
        self.seg = [seg_ide]*2*self.num
        for i in range(self.n):
            self.seg[i+self.num] = init_val[i]
        for i in range(self.num-1, 0, -1):
            self.seg[i] = self.segfunc(self.seg[2*i], self.seg[2*i+1])

        self.lazy = [lazy_ide]*2*self.num

    def gindex(self, l, r):
        # l, r: 1-indexed
        L = l + self.num
        R = r + self.num
        lm = (L // (L & -L)) >> 1
        rm = (R // (R & -R)) >> 1
        while L < R:
            if R <= rm:
                yield R
            if L <= lm:
                yield L
            L = L >> 1
            R = R >> 1
        while L:
            yield L
            L = L >> 1

    def propagate(self, *ids):
        # ids: 1-indexed
        for i in reversed(ids):
            v = self.lazy[i]
            if v is None:
                continue
            self.lazy[2*i] = self.lazy[2*i+1] = self.seg[2*i] = self.seg[2*i+1] = v
            self.lazy[i] = None

    def update(self, l, r, x):
        # l, r: 0-indexed
        # change x to [l, r)
        *ids, = self.gindex(l, r)
        self.propagate(*ids)
        L = l + self.num
        R = r + self.num
        while L < R:
            if R & 1:
                R -= 1
                self.lazy[R] = self.seg[R] = x
            if L & 1:
                self.lazy[L] = self.seg[L] = x
                L += 1
            L = L >> 1
            R = R >> 1
        for i in ids:
            # i: 1-indexed
            self.seg[i] = self.segfunc(self.seg[2*i], self.seg[2*i+1])

    def query(self, l, r):
        # l, r: 0-indexed
        # query for [l, r)
        if r <= l:
            return self.seg_ide

        self.propagate(*self.gindex(l, r))
        L = l + self.num
        R = r + self.num
        res = self.seg_ide
        while L < R:
            if R & 1:
                R -= 1
                res = self.segfunc(res, self.seg[R])
            if L & 1:
                res = self.segfunc(self.seg[L], res)
                L += 1
            L = L >> 1
            R = R >> 1
        return res

    def __str__(self): # for debug
        arr = [self.query(i,i+1) for i in range(self.n)]
        return str(arr)

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

def main():
    n = int(input())
    A = list(map(int, input().split()))
    X = set(A)
    X = list(X)
    X.sort()
    toid = {}
    for i, x in enumerate(X):
        toid[x] = i
    A = [toid[a] for a in A]

    N = len(X)
    I = [[] for i in range(N)]
    for i, a in enumerate(A):
        I[a].append(i)

    seg = LazySegTree([-1]*n, -1, None, max)
    for t in range(N):
        if not I[t]:
            continue
        l = I[t][0]
        r = I[t][-1]
        seg.update(l, r+1, t)
    ans = [0]*n
    for i in range(n):
        j = seg.query(i, i+1)
        ans[i] = X[j]
    print(*ans)

if __name__ == '__main__':
    main()
0