提出 #14360320


ソースコード 拡げる

import sys
from heapq import heappop, heappush
import bisect

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

class BinaryIndexedTree():
    def __init__(self, seq):
        self.size = len(seq)
        self.depth = self.size.bit_length()
        self.build(seq)

    def build(self, seq):
        data = seq
        size = self.size
        for i, x in enumerate(data):
            j = i + (i & (-i))
            if j < size:
                data[j] += data[i]
        self.data = data

    def __repr__(self):
        return self.data.__repr__()

    def get_sum(self, i):
        data = self.data
        s = 0
        while i:
            s += data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        data = self.data
        size = self.size
        while i < size:
            data[i] += x
            i += i & -i

    def find_kth_element(self, k):
        data = self.data
        size = self.size
        x, sx = 0, 0
        dx = 1 << (self.depth)
        for i in range(self.depth - 1, -1, -1):
            dx = (1 << i)
            if x + dx >= size:
                continue
            y = x + dx
            sy = sx + data[y]
            if sy < k:
                x, sx = y, sy
        return x + 1

N, Q = map(int, readline().split())
m = map(int, read().split())
ABCD = tuple(zip(m, m))
AB = ABCD[:N]
CD = ABCD[N:]

# 座圧
A, B = zip(*AB)
Asort = [0] + sorted(A)
A = tuple(bisect.bisect_left(Asort, x) for x in A)

C = 2 * 10 ** 5
INF = 2 * 10**5 + 10
enji_rate = [0] * (N + 1)
enji_cls = [0] * (N + 1)
cls_rate = [[(0, 0)] for _ in range(C + 1)]
for i, (a, b) in enumerate(zip(A,B), 1):
    enji_rate[i] = a
    enji_cls[i] = b
    heappush(cls_rate[b], (-a, i))
cls_rate_max = [-INF] * (C + 1)
tmp = [0] * INF
for i, lst in enumerate(cls_rate):
    x, _ = lst[0]
    cls_rate_max[i] = -x
    tmp[-x] += 1

tmp[0] = 0
bit = BinaryIndexedTree(tmp)

def find_max(c):
    A = cls_rate[c]
    while True:
        r, enj = A[0]
        if enj == 0:
            return 0
        if enji_cls[enj] == c:
            return -r
        heappop(A)

for c, after_cls in CD:
    before_cls = enji_cls[c]
    x1 = find_max(before_cls)
    y1 = find_max(after_cls)
    enji_cls[c] = after_cls
    heappush(cls_rate[after_cls], (-enji_rate[c], c))
    x2 = find_max(before_cls)
    y2 = find_max(after_cls)
    if x1:
        bit.add(x1, -1)
    if y1:
        bit.add(y1, -1)
    if x2:
        bit.add(x2, 1)
    if y2:
        bit.add(y2, 1)
    x = bit.find_kth_element(1)
    print(Asort[x])

提出情報

提出日時
問題 E - Smart Infants
ユーザ maspy
言語 PyPy3 (7.3.0)
得点 500
コード長 2700 Byte
結果 AC
実行時間 1584 ms
メモリ 175780 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果 AC
AC × 11
セット名 テストケース
Sample
All handmade02, handmade03, handmade04, handmade05, handmade06, handmade07, handmade08, handmade09, random10, sample00, sample01
ケース名 結果 実行時間 メモリ
handmade02 AC 109 ms 95128 KiB
handmade03 AC 89 ms 93448 KiB
handmade04 AC 93 ms 93548 KiB
handmade05 AC 533 ms 138732 KiB
handmade06 AC 823 ms 174188 KiB
handmade07 AC 906 ms 174264 KiB
handmade08 AC 1152 ms 173360 KiB
handmade09 AC 1095 ms 173576 KiB
random10 AC 1584 ms 175780 KiB
sample00 AC 106 ms 95264 KiB
sample01 AC 92 ms 95400 KiB