結果

問題 No.1675 Strange Minimum Query
ユーザー brthyyjpbrthyyjp
提出日時 2021-09-10 22:03:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,666 ms / 2,000 ms
コード長 4,644 bytes
コンパイル時間 833 ms
コンパイル使用メモリ 82,144 KB
実行使用メモリ 126,184 KB
最終ジャッジ日時 2024-06-12 00:10:51
合計ジャッジ時間 34,213 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,504 KB
testcase_01 AC 42 ms
53,248 KB
testcase_02 AC 42 ms
52,992 KB
testcase_03 AC 1,045 ms
101,984 KB
testcase_04 AC 1,072 ms
103,076 KB
testcase_05 AC 340 ms
85,348 KB
testcase_06 AC 1,234 ms
105,852 KB
testcase_07 AC 1,387 ms
110,960 KB
testcase_08 AC 47 ms
60,672 KB
testcase_09 AC 110 ms
76,544 KB
testcase_10 AC 641 ms
105,528 KB
testcase_11 AC 373 ms
95,236 KB
testcase_12 AC 714 ms
106,828 KB
testcase_13 AC 766 ms
122,232 KB
testcase_14 AC 970 ms
126,184 KB
testcase_15 AC 836 ms
125,124 KB
testcase_16 AC 38 ms
53,364 KB
testcase_17 AC 403 ms
85,932 KB
testcase_18 AC 453 ms
88,572 KB
testcase_19 AC 667 ms
100,772 KB
testcase_20 AC 1,093 ms
111,280 KB
testcase_21 AC 1,001 ms
111,736 KB
testcase_22 AC 1,230 ms
115,932 KB
testcase_23 AC 942 ms
100,796 KB
testcase_24 AC 932 ms
101,940 KB
testcase_25 AC 686 ms
94,124 KB
testcase_26 AC 480 ms
91,356 KB
testcase_27 AC 945 ms
107,860 KB
testcase_28 AC 616 ms
100,136 KB
testcase_29 AC 523 ms
98,344 KB
testcase_30 AC 471 ms
90,904 KB
testcase_31 AC 1,206 ms
109,648 KB
testcase_32 AC 1,666 ms
122,788 KB
testcase_33 AC 1,569 ms
123,676 KB
testcase_34 AC 1,581 ms
123,228 KB
testcase_35 AC 1,483 ms
124,580 KB
testcase_36 AC 1,445 ms
124,552 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)

class SegTree:
    def __init__(self, init_val, ide_ele, segfunc):
        self.n = len(init_val)
        self.num = 2**(self.n-1).bit_length()
        self.ide_ele = ide_ele
        self.segfunc = segfunc
        self.seg = [ide_ele]*2*self.num
        # set_val
        for i in range(self.n):
            self.seg[i+self.num] = init_val[i]
        # built
        for i in range(self.num-1, 0, -1):
            self.seg[i] = self.segfunc(self.seg[2*i], self.seg[2*i+1])

    def update(self, k, x):
        k += self.num
        self.seg[k] = x
        while k:
            k = k >> 1
            self.seg[k] = self.segfunc(self.seg[2*k], self.seg[2*k+1])

    def query(self, l, r):
        if r <= l:
            return self.ide_ele
        l += self.num
        r += self.num
        lres = self.ide_ele
        rres = self.ide_ele
        while l < r:
            if r & 1:
                r -= 1
                rres = self.segfunc(self.seg[r], rres)
            if l & 1:
                lres = self.segfunc(lres, self.seg[l])
                l += 1
            l = l >> 1
            r = r >> 1
        res = self.segfunc(lres, rres)
        return res

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

INF = 10**18

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

def main():

    n, q = map(int, input().split())
    LR = [0]*q
    for i in range(q):
        l, r, b = map(int, input().split())
        l, r = l-1, r-1
        LR[i] = (l, r, b)
    seg = LazySegTree([INF]*n, INF, None, segfunc)
    LR.sort(key=lambda x: x[2])
    #print(LR)
    for l, r, b in LR:
        seg.update(l, r+1, b)
    A = [INF]*n
    for i in range(n):
        a = seg.query(i, i+1)
        if a == INF:
            A[i] = 10**9
        else:
            A[i] = a

    seg = SegTree(A, INF, segfunc)
    for l, r, b in LR:
        x = seg.query(l, r+1)
        if x != b:
            print(-1)
            exit()
    else:
        print(*A)

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