結果

問題 No.833 かっこいい電車
ユーザー kept1994kept1994
提出日時 2022-04-30 22:32:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 848 ms / 2,000 ms
コード長 5,832 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 87,208 KB
実行使用メモリ 94,628 KB
最終ジャッジ日時 2023-09-12 13:57:09
合計ジャッジ時間 16,238 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 737 ms
85,388 KB
testcase_01 AC 75 ms
71,468 KB
testcase_02 AC 76 ms
71,264 KB
testcase_03 AC 77 ms
71,592 KB
testcase_04 AC 76 ms
71,132 KB
testcase_05 AC 75 ms
71,368 KB
testcase_06 AC 76 ms
71,324 KB
testcase_07 AC 80 ms
71,240 KB
testcase_08 AC 77 ms
71,564 KB
testcase_09 AC 74 ms
71,556 KB
testcase_10 AC 688 ms
86,392 KB
testcase_11 AC 807 ms
91,600 KB
testcase_12 AC 450 ms
83,196 KB
testcase_13 AC 403 ms
83,136 KB
testcase_14 AC 642 ms
92,888 KB
testcase_15 AC 453 ms
85,488 KB
testcase_16 AC 326 ms
89,096 KB
testcase_17 AC 511 ms
81,500 KB
testcase_18 AC 795 ms
86,628 KB
testcase_19 AC 348 ms
86,640 KB
testcase_20 AC 149 ms
79,732 KB
testcase_21 AC 848 ms
84,536 KB
testcase_22 AC 429 ms
93,692 KB
testcase_23 AC 367 ms
87,684 KB
testcase_24 AC 449 ms
93,340 KB
testcase_25 AC 787 ms
86,676 KB
testcase_26 AC 367 ms
89,268 KB
testcase_27 AC 576 ms
86,568 KB
testcase_28 AC 573 ms
83,924 KB
testcase_29 AC 606 ms
84,552 KB
testcase_30 AC 196 ms
94,628 KB
testcase_31 AC 688 ms
85,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys

MOD = 998244353

class SegTree:
    def __init__(self, monoid, bottomList, func):
        self.monoid = monoid
        self.func = func
        self.bottomLen = len(bottomList)
        self.offset = self.bottomLen        # セグ木の最下層の最初のインデックスに合わせるためのオフセット
        self.segLen = self.bottomLen * 2
        self.tree = [monoid] * self.segLen
        self.build(bottomList)

    """
    初期化
    O(self.segLen)
    """
    def build(self, seq):
        # 最下段の初期化
        for i, x in enumerate(seq, self.offset):
            self.tree[i] = x
        # ビルド
        for i in range(self.offset - 1, 0, -1):
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1])

    """
    一点加算 他演算
    O(log(self.bottomLen))
    """
    def pointAdd(self, i: int, val: int):
        i += self.offset
        self.tree[i] += val
        # self.tree[i] = self.func(self.tree[i], val) <- こっちの方が都度の修正は発生しない。再帰が遅くないか次第。
        while i > 1:
            i >>= 1 # 2で割って頂点に達するまで下層から遡上
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1]) # 必ず末尾0と1がペアになるのでor演算子

    """
    一点更新
    O(log(self.bottomLen))
    """
    def pointUpdate(self, i: int, val: int):
        i += self.offset
        self.tree[i] = val
        while i > 1:
            i >>= 1 # 2で割って頂点に達するまで下層から遡上
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1]) # 必ず末尾0と1がペアになるのでor演算子
    
    """
    区間更新
    O(log(self.bottomLen))
    """
    def rangeAdd(self, l: int, r: int, val: int):
        l += self.offset
        r += self.offset
        while l < r:
            if l & 1:
                self.tree[l] = self.func(self.tree[l], val) 
                l += 1
            if r & 1:
                r -= 1
                self.tree[r] = self.func(self.tree[r], val) 
            l >>= 1
            r >>= 1
        return

    """ 区間取得
    O(log(self.bottomLen))
    """
    def getRange(self, l: int, r: int):
        l += self.offset
        r += self.offset
        vL = self.monoid
        vR = self.monoid
        while l < r:
            if l & 1:
                vL = self.func(vL, self.tree[l])
                l += 1
            if r & 1:
                r -= 1
                vR = self.func(self.tree[r], vR)
            l >>= 1
            r >>= 1
        return self.func(vL, vR)

    """ 一点取得
    O(log(self.bottomLen))
    """
    def getPoint(self, i: int):
        i += self.offset
        return self.tree[i]

def main():
    N, Q = map(int, input().split())
    A = list(map(int, input().split()))
    def add(x: int, y: int):
        return x + y
    connect = SegTree(0, [1] * N, add)
    attract = SegTree(0, A, add)

    # True ------ ok | ng ---- False
    def is_ok_l(k: int, threshold: int):
        return connect.getRange(0, k + 1) < threshold   # 条件式

    def binSearch_l(ok: int, ng: int, threshold: int):
        # print(ok, ng)              # はじめの2値の状態
        while abs(ok - ng) > 1:     # 終了条件(差が1となり境界を見つけた時)
            mid = (ok + ng) // 2
            # print("target > ", mid)
            result = is_ok_l(mid, threshold)
            # print(result)
            if result:
                ok = mid            # midが条件を満たすならmidまではokなのでokの方を真ん中まで持っていく
            else:
                ng = mid            # midが条件を満たさないならmidまではngなのでngの方を真ん中まで持っていく
            # print(ok, ng)          # 半分に切り分ける毎の2値の状態
        return ok    # 関数呼び出し時の引数のngは絶対評価されないのでngに書く値が答えになりうるならその数マイナス1を指定する。

    # True ------ ok | ng ---- False
    def is_ok_r(k: int, threshold: int):
        return connect.getRange(0, k + 1) <= threshold   # 条件式

    def binSearch_r(ok: int, ng: int, threshold: int):
        # print(ok, ng)              # はじめの2値の状態
        while abs(ok - ng) > 1:     # 終了条件(差が1となり境界を見つけた時)
            mid = (ok + ng) // 2
            # print("target > ", mid)
            result = is_ok_r(mid, threshold)
            # print(result)
            if result:
                ok = mid            # midが条件を満たすならmidまではokなのでokの方を真ん中まで持っていく
            else:
                ng = mid            # midが条件を満たさないならmidまではngなのでngの方を真ん中まで持っていく
            # print(ok, ng)          # 半分に切り分ける毎の2値の状態
        return ok    # 関数呼び出し時の引数のngは絶対評価されないのでngに書く値が答えになりうるならその数マイナス1を指定する。

    for i in range(Q):
        q, x = list(map(int, input().split()))
        x -= 1
        if q == 1:
            connect.pointUpdate(x + 1, 0)
        elif q == 2:
            connect.pointUpdate(x + 1, 1)
        elif q == 3:
            attract.pointAdd(x, 1)
        else:
            # xの属するグループ num
            num = connect.getRange(0, x + 1)
            # print(num)
            # numの両端
            st = binSearch_l(-1, N, num) + 1
            ed = binSearch_r(-1, N, num)
            # print(st, ed)
            res = attract.getRange(st, ed + 1)
            print(res)
    return

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