結果

問題 No.618 labo-index
ユーザー n_knuun_knuu
提出日時 2017-11-26 22:55:01
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 3,230 ms / 6,000 ms
コード長 2,995 bytes
コンパイル時間 640 ms
コンパイル使用メモリ 76,928 KB
実行使用メモリ 115,480 KB
最終ジャッジ日時 2024-11-27 11:51:14
合計ジャッジ時間 35,956 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
75,392 KB
testcase_01 AC 84 ms
75,520 KB
testcase_02 AC 83 ms
75,264 KB
testcase_03 AC 85 ms
75,264 KB
testcase_04 AC 162 ms
78,720 KB
testcase_05 AC 147 ms
79,232 KB
testcase_06 AC 99 ms
77,568 KB
testcase_07 AC 106 ms
78,080 KB
testcase_08 AC 113 ms
78,208 KB
testcase_09 AC 137 ms
79,104 KB
testcase_10 AC 151 ms
79,104 KB
testcase_11 AC 132 ms
79,360 KB
testcase_12 AC 107 ms
78,080 KB
testcase_13 AC 118 ms
78,848 KB
testcase_14 AC 114 ms
78,464 KB
testcase_15 AC 137 ms
78,848 KB
testcase_16 AC 142 ms
79,744 KB
testcase_17 AC 104 ms
78,720 KB
testcase_18 AC 154 ms
80,296 KB
testcase_19 AC 1,074 ms
103,844 KB
testcase_20 AC 1,756 ms
104,616 KB
testcase_21 AC 1,402 ms
104,476 KB
testcase_22 AC 1,141 ms
104,412 KB
testcase_23 AC 1,414 ms
104,704 KB
testcase_24 AC 1,174 ms
104,748 KB
testcase_25 AC 1,413 ms
104,632 KB
testcase_26 AC 1,384 ms
105,088 KB
testcase_27 AC 1,389 ms
104,612 KB
testcase_28 AC 1,058 ms
103,704 KB
testcase_29 AC 1,272 ms
104,748 KB
testcase_30 AC 1,068 ms
104,876 KB
testcase_31 AC 1,342 ms
104,400 KB
testcase_32 AC 1,598 ms
104,620 KB
testcase_33 AC 1,331 ms
103,852 KB
testcase_34 AC 2,921 ms
115,480 KB
testcase_35 AC 3,230 ms
114,736 KB
testcase_36 AC 2,218 ms
108,140 KB
testcase_37 AC 2,435 ms
107,860 KB
testcase_38 AC 80 ms
75,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python
import sys
import math
if sys.version_info[0] == 2:
    range, input = xrange, raw_input


class FenwickTree:
    def __init__(self, a_list):
        # 0-indexed
        self.N = len(a_list)
        self.bit = a_list[:]
        for _ in range(self.N, 1 << int(math.ceil(math.log(self.N, 2)))):
            self.bit.append(0)
        for i in range(self.N-1):
            self.bit[i | (i+1)] += self.bit[i]

    def add(self, i, val):
        while i < self.N:
            self.bit[i] += val
            i |= i + 1

    def sum(self, n):
        """[0, n)"""
        ret = 0
        n -= 1
        while n >= 0:
            ret += self.bit[n]
            n = (n & (n + 1)) - 1
        return ret

    def index(self, k):
        return self.sum(k + 1) - self.sum(k)

    def count_index(self, k):
        left, right = -1, self.N - 1
        while left + 1 < right:
            mid = (left + right) >> 1
            if self.sum(mid + 1) >= k:
                right = mid
            else:
                left = mid
        return right


def solve(Q, events):
    up_events = [0] * (Q + 1)
    for i, (x, y) in enumerate(events):
        if x == 3:
            up_events[i + 1] = y
        up_events[i + 1] += up_events[i]

    final_powers = []
    for i, (x, y) in enumerate(events):
        if x == 1:
            final_power = y + up_events[Q] - up_events[i]
            final_powers.append((final_power, i))
    final_powers.sort()

    N = len(final_powers)
    # print(final_powers)

    idx2fwt = [-1] * Q
    for i, (_, idx) in enumerate(final_powers):
        idx2fwt[idx] = i

    get_into_labo = []
    in_labo = FenwickTree([0] * N)

    for i, (x, y) in enumerate(events):
        if x == 1:
            # assert(-10 ** 9 <= y <= 10 ** 9)
            # assert(idx2fwt[i] != -1)
            # assert(in_labo.index(idx2fwt[i]) == 0)
            in_labo.add(idx2fwt[i], 1)
            get_into_labo.append(i)
        elif x == 2:
            # assert(1 <= y < i + 1)
            # assert(in_labo.index(idx2fwt[get_into_labo[y - 1]]) == 1)
            in_labo.add(idx2fwt[get_into_labo[y - 1]], -1)
        elif x == 3:
            # assert(-10 ** 9 <= y <= 10 ** 9)
            pass
        else:
            assert(False)
            pass
        # print([in_labo.index(i) for i in range(N)])
        num_members = in_labo.sum(N)
        left, right = 0, num_members + 1
        while left + 1 < right:
            mid = (left + right) >> 1
            idx = in_labo.count_index(num_members - mid + 1)
            orig = final_powers[idx][1]
            now_power = events[orig][1] + up_events[i + 1] - up_events[orig]
            # print(left, mid, right, idx)
            if now_power >= mid:
                left = mid
            else:
                right = mid
        print(left)


if __name__ == '__main__':
    Q = int(input())
    # assert(1 <= Q <= 2 * 10 ** 5)
    events = [tuple(map(int, input().split())) for _ in range(Q)]
    solve(Q, events)
0