結果

問題 No.649 ここでちょっとQK!
ユーザー toyuzukotoyuzuko
提出日時 2020-06-06 12:30:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,488 ms / 3,000 ms
コード長 4,480 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 82,476 KB
実行使用メモリ 100,152 KB
最終ジャッジ日時 2024-12-23 12:09:50
合計ジャッジ時間 20,677 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,864 KB
testcase_01 AC 38 ms
52,992 KB
testcase_02 AC 38 ms
52,608 KB
testcase_03 AC 109 ms
85,612 KB
testcase_04 AC 191 ms
93,152 KB
testcase_05 AC 274 ms
95,688 KB
testcase_06 AC 214 ms
92,952 KB
testcase_07 AC 34 ms
53,120 KB
testcase_08 AC 33 ms
52,864 KB
testcase_09 AC 32 ms
52,992 KB
testcase_10 AC 33 ms
53,248 KB
testcase_11 AC 34 ms
52,992 KB
testcase_12 AC 770 ms
89,776 KB
testcase_13 AC 740 ms
88,360 KB
testcase_14 AC 723 ms
89,096 KB
testcase_15 AC 857 ms
90,172 KB
testcase_16 AC 863 ms
89,704 KB
testcase_17 AC 856 ms
89,864 KB
testcase_18 AC 939 ms
91,768 KB
testcase_19 AC 1,005 ms
91,968 KB
testcase_20 AC 1,023 ms
93,112 KB
testcase_21 AC 1,143 ms
93,360 KB
testcase_22 AC 1,202 ms
95,032 KB
testcase_23 AC 1,262 ms
95,524 KB
testcase_24 AC 1,302 ms
96,388 KB
testcase_25 AC 1,388 ms
96,576 KB
testcase_26 AC 1,488 ms
100,152 KB
testcase_27 AC 119 ms
76,544 KB
testcase_28 AC 146 ms
77,056 KB
testcase_29 AC 143 ms
76,752 KB
testcase_30 AC 812 ms
88,752 KB
testcase_31 AC 871 ms
90,108 KB
testcase_32 AC 36 ms
52,736 KB
testcase_33 AC 35 ms
52,608 KB
testcase_34 AC 33 ms
52,608 KB
testcase_35 AC 33 ms
52,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SplayNode():
    def __init__(self, val):
        self.val = val
        self.par = None
        self.lt = None
        self.rt = None
        self.size = 1

class SplayTree():
    def __init__(self):
        self.size = 0
        self.root = None

    def update(self, node):
        node.size = 1
        if node.lt is not None:
            node.size += node.lt.size
        if node.rt is not None:
            node.size += node.rt.size

    def rotate(self, node):
        par = node.par
        if par is None:
            return
        ppar = par.par
        if par.lt == node:
            rt = node.rt
            par.lt = rt
            par.par = node
            node.rt = par
            self.update(par)
            self.update(node)
            if rt is not None:
                rt.par = par
            node.par = ppar
            if ppar is None:
                return
            if ppar.lt == par:
                ppar.lt = node
            else:
                ppar.rt = node
        else:
            lt = node.lt
            par.rt = lt
            par.par = node
            node.lt = par
            self.update(par)
            self.update(node)
            if lt is not None:
                lt.par = par
            node.par = ppar
            if ppar is None:
                return
            if ppar.rt == par:
                ppar.rt = node
            else:
                ppar.lt = node

    def splay(self, node):
        while node.par is not None:
            self.rotate(node)
        self.size = node.size
        self.root = node

    def get(self, k):
        node = self.root
        if self.size <= k:
            return False
        while True:
            lsize = 0 if node.lt is None else node.lt.size
            if lsize == k:
                self.splay(node)
                return node
            if lsize > k:
                node = node.lt
            else:
                k -= lsize + 1
                node = node.rt

    def find(self, val):
        node = self.root
        if node is None:
            return False, 0
        count = 0
        while True:
            lsize = 0 if node.lt is None else node.lt.size
            if node.val == val:
                self.splay(node)
                return True, count + lsize
            if node.val > val:
                if node.lt is None:
                    return False, count + lsize
                else:
                    node = node.lt
            else:
                if node.rt is None:
                    return False, count + lsize + 1
                else:
                    count += lsize + 1
                    node = node.rt

    def merge(self, other):
        if self.size == 0:
            self.size = other.size
            self.root = other.root
            return
        if other.size == 0:
            return
        lnode = self.get(self.size - 1)
        rnode = other.get(0)
        rnode.par = lnode
        lnode.rt = rnode
        self.update(lnode)
        self.root = lnode
        self.size = lnode.size

    def split(self, k):
        other = SplayTree()
        if k == 0:
            other.root = self.root
            other.size = self.size
            self.size = 0
            self.root = None
            return other
        if k == self.size:
            return other
        root = self.get(k)
        lroot = root.lt
        rroot = root
        rroot.lt = None
        lroot.par = None
        self.update(lroot)
        self.update(rroot)
        self.size = lroot.size
        self.root = lroot
        other.size = rroot.size
        other.root = rroot
        return other

    def insert(self, val):
        _, k = self.find(val)
        rtree = self.split(k)
        nnode = SplayNode(val)
        ntree = SplayTree()
        ntree.root = nnode
        ntree.size = 1
        self.merge(ntree)
        self.merge(rtree)

    def delete(self, val):
        exist, k = self.find(val)
        if not exist:
            return
        rtree = self.split(k + 1)
        deleted = self.split(k)
        self.merge(rtree)


import sys
input = sys.stdin.readline

Q, K = map(int, input().split())

st = SplayTree()
res = []

for _ in range(Q):
    q = tuple(map(int, input().split()))
    if q[0] == 1:
        st.insert(q[1])
    else:
        v = st.get(K - 1)
        if not v:
            res.append(-1)
        else:
            res.append(v.val)
            st.delete(v.val)

print('\n'.join(map(str, res)))
0