목차

모독

ps
링크acmicpc.net/…
출처BOJ
문제 번호16221
문제명모독
레벨플래티넘 3
분류

구간 쿼리

시간복잡도O(n+qlogn)
인풋사이즈n<=1,000,000, q<=1,000,000
사용한 언어PyPy
제출기록223956KB / 2796ms
최고기록2796ms
해결날짜2021/04/13

풀이

코드

"""Solution code for "BOJ 16221. 모독".

- Problem link: https://www.acmicpc.net/problem/16221
- Solution link: http://www.teferi.net/ps/problems/boj/16221
"""

import heapq
import sys
from teflib import fenwicktree

MAX_K = 1_000_000


def main():
    Q = int(sys.stdin.readline())

    fenwick = fenwicktree.FenwickTree(MAX_K + 2)
    zeros = list(range(1, MAX_K + 2))
    heapq.heapify(zeros)
    for _ in range(Q):
        T, K = [int(x) for x in sys.stdin.readline().split()]
        if T == 1:
            fenwick.update(K, 1)
            if fenwick.get(K) == 1 and K == zeros[0]:
                while fenwick.get(zeros[0]) > 0:
                    heapq.heappop(zeros)
        else:
            fenwick.update(K, -1)
            if fenwick.get(K) == 0:
                heapq.heappush(zeros, K)
        print(fenwick.query(1, zeros[0]))


if __name__ == '__main__':
    main()