Submission #3410116


Source Code Expand

import random
random.seed()
randint = random.randint

def merge(l, r):
    if not l or not r:
        return l or r

    #st = []; dr = []
    z = y = [None]; d = 0
    while l and r:
        lc = l[3]; rc = r[3]
        if randint(1, lc+rc) <= lc:
            y[d] = l
            l[3] = lc+rc
            y = l; d = 1
            l = l[1]
        else:
            y[d] = r
            r[3] = lc+rc
            y = r; d = 0
            r = r[0]
        #st.append(y)
        #dr.append(d)
    y[d] = l or r
    #while st:
    #    x = st.pop(); d = dr.pop()
    return z[0]

def __split(st, dr):
    l = None; r = None
    st.reverse(); dr.reverse()
    for x, d in zip(st, dr):
        if d:
            x[1] = q = l
            p = x[0]
            l = x
        else:
            x[0] = p = r
            q = x[1]
            r = x
        x[3] = (p[3] if p else 0) + (q[3] if q else 0) + 1
    return l, r

def split(t, k):
    if not t:
        return None, None
    x = t
    st = []; dr = []
    while x:
        l = x[0]
        c = (l[3] if l else 0) + 1

        st.append(x)
        if c <= k:
            k -= c
            x = x[1]
            d = 1
        else:
            x = x[0]
            d = 0
        dr.append(d)
    return __split(st, dr)

def insert(t, val):
    x = t
    st = []; dr = []
    while x:
        st.append(x)
        if x[2] == val:
            return t
        d = (x[2] < val)
        dr.append(d)
        x = x[d]

    l, r = __split(st, dr)

    # [<left>, <right>, <key>, <count>]
    nd = [None, None, val, 1]

    # merge tree l and node nd
    x = l; y = None
    while x:
        if not randint(0, x[3]):
            if y:
                y[1] = nd
            else:
                l = nd
            nd[0] = x
            nd[3] = x[3]+1
            break
        y = x
        x[3] += 1
        x = x[1]
    else:
        if y:
            y[1] = nd
        else:
            l = nd
    return merge(l, r)

def delete(t, val):
    x = t
    st = []; dr = []
    y = None
    while x:
        st.append(x)
        d = x[2] <= val
        dr.append(d)
        x = x[d]
    l, r = __split(st, dr)

    st = []; dr = []
    x = l
    while x:
        st.append(x)
        d = (x[2] < val)
        dr.append(d)
        x = x[d]
    l, m = __split(st, dr)

    #assert m[2] == val

    return merge(l, r)

root = None
def query(k):
    global root
    l, r = split(root, k)
    l, m = split(l, k-1)
    root = merge(l, r)
    return m[2]

import sys
readline = sys.stdin.readline
writelines = sys.stdout.writelines

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

Submission Info

Submission Time
Task C - データ構造
User yaketake08
Language PyPy3 (2.4.0)
Score 0
Code Size 2908 Byte
Status TLE
Exec Time 2110 ms
Memory 114396 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 2
AC × 9
TLE × 9
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 181 ms 39920 KB
sample_02.txt AC 183 ms 39920 KB
subtask1_01.txt AC 184 ms 39920 KB
subtask1_02.txt AC 193 ms 39920 KB
subtask1_03.txt AC 190 ms 39920 KB
subtask1_04.txt AC 721 ms 77532 KB
subtask1_05.txt AC 913 ms 84060 KB
subtask1_06.txt AC 238 ms 44780 KB
subtask1_07.txt TLE 2109 ms 99036 KB
subtask1_08.txt TLE 2110 ms 112856 KB
subtask1_09.txt TLE 2110 ms 114396 KB
subtask1_10.txt TLE 2109 ms 100856 KB
subtask1_11.txt TLE 2109 ms 100700 KB
subtask1_12.txt TLE 2109 ms 91228 KB
subtask1_13.txt TLE 2110 ms 101208 KB
subtask1_14.txt TLE 2108 ms 97712 KB
subtask1_15.txt AC 1584 ms 95708 KB
subtask1_16.txt TLE 2109 ms 104076 KB