Submission #3402054


Source Code Expand

from collections import deque
import sys
readline = sys.stdin.readline
writelines = sys.stdout.writelines

def rotate(nd, d):
    c = nd[d]
    if d:
        e = c[1]
        nd[1] = c[0]
        c[0] = nd
    else:
        e = c[0]
        nd[0] = c[1]
        c[1] = nd

    r = c[4] = nd[4]
    nd[4] = r - (e[4] if e else 0) - 1
    return c


root = None
def insert(val, pri):
    global root
    st = []
    dr = []
    x = root
    while x:
        st.append(x)
        if x[2] == val:
            return
        d = (x[2] < val)
        dr.append(d)
        x = x[d]

    # [<left>, <right>, <key>, <priority>, <count>]
    nd = [None, None, val, pri, 1]
    while st:
        x = st.pop(); d = dr.pop()
        x[d] = nd
        x[4] += 1
        if x[3] >= nd[3]:
            break
        rotate(x, d)
    else:
        root = nd

    for x in st:
        x[4] += 1

def __delete(nd):
    st = []; dr = []
    while nd[0] or nd[1]:
        l = nd[0]; r = nd[1]
        d = (l[3] <= r[3]) if l and r else (l is None)
        st.append(rotate(nd, d))
        dr.append(d ^ 1)
    nd = x = None
    while st:
        nd = x; x = st.pop(); d = dr.pop()
        x[d] = nd
        x[4] -= 1
    return x

def delete(val):
    global root
    x = root

    st = []
    y = None
    while x:
        if val == x[2]:
            break
        y = x; d = (x[2] < val)
        st.append(y)
        x = x[d]
    else:
        return

    if y:
        y[d] = __delete(x)
        for x in st:
            x[4] -= 1
    else:
        root = __delete(x)

def find(val):
    x = root
    while x:
        if val == x[2]:
            return 1
        x = x[x[2] < val]
    return 0

def query(k):
    global root
    s = 0
    x = root
    y = None
    while 1:
        l = x[0]
        if l:
            if l[4] < k:
                k -= l[4] + 1
                if k == 0:
                    break
                y = x; x[4] -= 1
                d = 1
                x = x[1]
            else:
                y = x; x[4] -= 1
                d = 0
                x = l
        else:
            k -= 1
            if k == 0:
                break
            y = x; x[4] -= 1
            d = 1
            x = x[1]
    v = x[2]
    if y:
        y[d] = __delete(x)
    else:
        root = __delete(x)
    return v


import random
random.seed()
randint = random.randint
Q = int(readline())
ans = []
for i in range(Q):
    t, x = map(int, readline().split())
    if t == 1:
        insert(x, randint(1, 100000))
    else:
        ans.append("%d\n" % query(x))
writelines(ans)

Submission Info

Submission Time
Task C - データ構造
User yaketake08
Language PyPy3 (2.4.0)
Score 100
Code Size 2714 Byte
Status AC
Exec Time 1637 ms
Memory 122728 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 18
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt
Case Name Status Exec Time Memory
sample_01.txt AC 195 ms 41200 KB
sample_02.txt AC 176 ms 39920 KB
subtask1_01.txt AC 177 ms 39920 KB
subtask1_02.txt AC 176 ms 39920 KB
subtask1_03.txt AC 178 ms 39920 KB
subtask1_04.txt AC 472 ms 62552 KB
subtask1_05.txt AC 686 ms 78040 KB
subtask1_06.txt AC 189 ms 40560 KB
subtask1_07.txt AC 1106 ms 98776 KB
subtask1_08.txt AC 1286 ms 100568 KB
subtask1_09.txt AC 1305 ms 104024 KB
subtask1_10.txt AC 1521 ms 117336 KB
subtask1_11.txt AC 1637 ms 121820 KB
subtask1_12.txt AC 1471 ms 109528 KB
subtask1_13.txt AC 1448 ms 113240 KB
subtask1_14.txt AC 1600 ms 122728 KB
subtask1_15.txt AC 829 ms 91096 KB
subtask1_16.txt AC 1094 ms 95832 KB