結果

問題 No.649 ここでちょっとQK!
ユーザー toyuzukotoyuzuko
提出日時 2020-06-06 12:34:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,728 ms / 3,000 ms
コード長 4,606 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 82,252 KB
実行使用メモリ 99,872 KB
最終ジャッジ日時 2024-12-23 12:10:31
合計ジャッジ時間 23,815 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,608 KB
testcase_01 AC 41 ms
52,992 KB
testcase_02 AC 44 ms
52,992 KB
testcase_03 AC 115 ms
85,632 KB
testcase_04 AC 217 ms
93,312 KB
testcase_05 AC 295 ms
95,312 KB
testcase_06 AC 235 ms
93,056 KB
testcase_07 AC 39 ms
52,992 KB
testcase_08 AC 38 ms
52,952 KB
testcase_09 AC 39 ms
52,992 KB
testcase_10 AC 40 ms
52,992 KB
testcase_11 AC 39 ms
52,608 KB
testcase_12 AC 870 ms
88,892 KB
testcase_13 AC 875 ms
89,044 KB
testcase_14 AC 856 ms
87,088 KB
testcase_15 AC 973 ms
90,300 KB
testcase_16 AC 926 ms
89,568 KB
testcase_17 AC 1,018 ms
89,852 KB
testcase_18 AC 1,088 ms
91,772 KB
testcase_19 AC 1,202 ms
92,088 KB
testcase_20 AC 1,199 ms
93,644 KB
testcase_21 AC 1,312 ms
93,368 KB
testcase_22 AC 1,377 ms
94,396 KB
testcase_23 AC 1,531 ms
97,064 KB
testcase_24 AC 1,555 ms
96,452 KB
testcase_25 AC 1,600 ms
97,212 KB
testcase_26 AC 1,728 ms
99,872 KB
testcase_27 AC 160 ms
76,560 KB
testcase_28 AC 164 ms
76,780 KB
testcase_29 AC 159 ms
76,640 KB
testcase_30 AC 946 ms
88,128 KB
testcase_31 AC 1,055 ms
90,744 KB
testcase_32 AC 40 ms
52,992 KB
testcase_33 AC 39 ms
52,864 KB
testcase_34 AC 38 ms
52,992 KB
testcase_35 AC 39 ms
52,992 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)

    def delete_kth(self, k):
        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_kth(K - 1)

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