結果

問題 No.945 YKC饅頭
ユーザー tamato
提出日時 2020-04-01 10:54:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,270 ms / 2,000 ms
コード長 1,575 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 135,188 KB
最終ジャッジ日時 2024-06-26 06:38:38
合計ジャッジ時間 31,097 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 74
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.readline

    class Bit:
        def __init__(self, n):
            self.size = n
            self.tree = [0] * (n + 1)

        def sum(self, i):
            s = 0
            while i > 0:
                s += self.tree[i]
                i -= i & -i
            return s

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

        def lower_bound(self, w):
            if w <= 0:
                return 0
            x = 0
            k = 1 << (self.size.bit_length() - 1)
            while k:
                if x + k <= self.size and self.tree[x + k] < w:
                    w -= self.tree[x + k]
                    x += k
                k >>= 1
            return x+1

    N, M = map(int, input().split())
    q = []
    qy = [0] * (M+1)
    ans = [0] * 3
    bit = Bit(M)
    for i in range(1, M+1):
        s, t, y = input().split()
        s = int(s)
        t = int(t)
        if y == 'K':
            qy[i] = 1
        elif y == 'C':
            qy[i] = 2
        q.append((s, 1, i))
        if t+1 <= N:
            q.append((t+1, -1, i))

    q.sort(key=lambda x: x[0])
    i = 1
    for t, flg, j in q:
        while i < t:
            k = bit.lower_bound(1)
            if k != M + 1:
                ans[qy[k]] += 1
            i += 1
        bit.add(j, flg)

    while i < N+1:
        k = bit.lower_bound(1)
        if k != M + 1:
            ans[qy[k]] += 1
        i += 1

    print(*ans)


if __name__ == '__main__':
    main()
0