結果

問題 No.649 ここでちょっとQK!
ユーザー 👑 rin204rin204
提出日時 2022-07-04 14:13:06
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,687 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 82,288 KB
実行使用メモリ 661,576 KB
最終ジャッジ日時 2024-12-14 03:29:18
合計ジャッジ時間 36,351 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,352 KB
testcase_01 AC 41 ms
53,552 KB
testcase_02 AC 41 ms
53,356 KB
testcase_03 AC 465 ms
76,356 KB
testcase_04 AC 446 ms
109,256 KB
testcase_05 AC 440 ms
109,000 KB
testcase_06 AC 356 ms
76,136 KB
testcase_07 AC 40 ms
52,904 KB
testcase_08 AC 41 ms
53,920 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 1,142 ms
354,800 KB
testcase_13 AC 1,118 ms
353,700 KB
testcase_14 AC 1,050 ms
329,836 KB
testcase_15 AC 1,243 ms
364,476 KB
testcase_16 AC 1,455 ms
387,212 KB
testcase_17 AC 1,580 ms
419,900 KB
testcase_18 AC 1,687 ms
445,484 KB
testcase_19 AC 1,782 ms
464,932 KB
testcase_20 AC 2,025 ms
500,288 KB
testcase_21 MLE -
testcase_22 MLE -
testcase_23 MLE -
testcase_24 MLE -
testcase_25 MLE -
testcase_26 MLE -
testcase_27 AC 89 ms
74,776 KB
testcase_28 AC 89 ms
73,760 KB
testcase_29 AC 88 ms
73,500 KB
testcase_30 AC 416 ms
88,344 KB
testcase_31 AC 449 ms
89,752 KB
testcase_32 AC 42 ms
54,004 KB
testcase_33 RE -
testcase_34 AC 40 ms
54,120 KB
testcase_35 AC 40 ms
53,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Binary_Trie_Vertex:
    def __init__(self):
        self.left = None
        self.right = None
        self.parent = None
        self.size = 0

class Binary_Trie:
    def __init__(self, logn, ismulti = False):
        self.logn = logn
        self.root = Binary_Trie_Vertex()
        self.bi = [1 << i for i in range(logn)]
        self.multi = ismulti
        
    def add(self, x):
        V = self.root
        plus = False
        for i in range(self.logn - 1, -1, -1):
            if x >> i & 1:
                if V.right is None:
                    V.right = Binary_Trie_Vertex()
                    V.right.parent = V
                    plus = True
                V = V.right
                
            else:
                if V.left is None:
                    V.left = Binary_Trie_Vertex()
                    V.left.parent = V
                    plus = True
                V = V.left
        if plus or self.multi:
            while V.parent is not None:
                V.size += 1
                V = V.parent
            V.size += 1

    def remove(self, x):
        V = self.root
        for i in range(self.logn - 1, -1, -1):
            if x >> i & 1:
                if V.right is None:
                    return False
                V = V.right
            else:
                if V.left is None:
                    return False
                V = V.left
        V2 = V
        while V2.parent is not None:
            V2.size -= 1
            V2 = V2.parent
        V2.size -= 1
        while V.size == 0:
            P = V.parent
            if P.right == V:
                P.right = None
            else:
                P.left = None
            if P.right is None and P.left is None:
                pass
            else:
                return True
            V = P
        return True
    
    def search(self, x):
        V = self.root
        for i in range(self.logn - 1, -1, -1):
            if x >> i & 1:
                if V.right is None:
                    return False
                V = V.right
            else:
                if V.left is None:
                    return False
                V = V.left
        return True
    
    def xor_min(self, x):
        xor = 0
        V = self.root
        for i in range(self.logn - 1, -1, -1):
            if x >> i & 1:
                if V.right is not None:
                    V = V.right
                else:
                    xor += self.bi[i]
                    V = V.left
            else:
                if V.left is not None:
                    V = V.left
                else:
                    xor += self.bi[i]
                    V = V.right
        return xor
        
    def __getitem__(self, k):
        return self.Kth_element(k)
    
    def Kth_element(self, k):
        if k < 0:
            k = self.root.size + k
        k += 1
        if k > self.root.size or k <= 0:
            return None
        ret = 0
        V = self.root
        for i in range(self.logn - 1, -1, -1):
            if V.left is None:
                ret += self.bi[i]
                V = V.right
            elif V.right is None:
                V = V.left
            elif V.left.size >= k:
                V = V.left
            else:
                ret += self.bi[i]
                k -= V.left.size
                V = V.right
        return ret

Q, k = map(int, input().split())
bt = Binary_Trie(60, True)

for _ in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        bt.add(query[1])
    else:
        ans = bt[k - 1]
        if ans is None:
            print(-1)
        else:
            print(ans)
            bt.remove(ans)
0