結果

問題 No.618 labo-index
ユーザー n_knuun_knuu
提出日時 2017-11-26 22:48:29
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 2,926 ms / 6,000 ms
コード長 2,964 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 76,684 KB
実行使用メモリ 115,924 KB
最終ジャッジ日時 2024-11-27 11:50:10
合計ジャッジ時間 35,056 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
75,648 KB
testcase_01 AC 77 ms
75,520 KB
testcase_02 AC 78 ms
75,904 KB
testcase_03 AC 81 ms
75,776 KB
testcase_04 AC 193 ms
79,232 KB
testcase_05 AC 154 ms
80,004 KB
testcase_06 AC 88 ms
78,336 KB
testcase_07 AC 94 ms
77,952 KB
testcase_08 AC 99 ms
78,208 KB
testcase_09 AC 115 ms
79,232 KB
testcase_10 AC 129 ms
80,000 KB
testcase_11 AC 117 ms
79,104 KB
testcase_12 AC 114 ms
78,848 KB
testcase_13 AC 125 ms
79,104 KB
testcase_14 AC 107 ms
78,848 KB
testcase_15 AC 127 ms
79,360 KB
testcase_16 AC 139 ms
79,744 KB
testcase_17 AC 103 ms
78,720 KB
testcase_18 AC 157 ms
80,052 KB
testcase_19 AC 1,044 ms
104,880 KB
testcase_20 AC 1,356 ms
105,380 KB
testcase_21 AC 1,111 ms
104,360 KB
testcase_22 AC 1,163 ms
105,304 KB
testcase_23 AC 1,205 ms
104,916 KB
testcase_24 AC 1,065 ms
105,000 KB
testcase_25 AC 1,261 ms
105,260 KB
testcase_26 AC 1,227 ms
105,084 KB
testcase_27 AC 1,194 ms
105,036 KB
testcase_28 AC 1,036 ms
104,520 KB
testcase_29 AC 1,217 ms
105,760 KB
testcase_30 AC 1,118 ms
105,004 KB
testcase_31 AC 1,185 ms
104,904 KB
testcase_32 AC 1,241 ms
105,220 KB
testcase_33 AC 1,134 ms
104,996 KB
testcase_34 AC 2,663 ms
115,924 KB
testcase_35 AC 2,926 ms
115,100 KB
testcase_36 AC 2,132 ms
109,140 KB
testcase_37 AC 2,300 ms
108,616 KB
testcase_38 AC 74 ms
75,776 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)])
        left, right = 0, in_labo.sum(N) + 1
        while left + 1 < right:
            mid = (left + right) >> 1
            idx = in_labo.count_index(in_labo.sum(N) - 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