結果

問題 No.833 かっこいい電車
ユーザー kept1994kept1994
提出日時 2022-04-30 22:29:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 831 ms / 2,000 ms
コード長 5,850 bytes
コンパイル時間 711 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 94,664 KB
最終ジャッジ日時 2023-09-12 13:55:55
合計ジャッジ時間 17,590 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 733 ms
85,440 KB
testcase_01 AC 77 ms
71,132 KB
testcase_02 AC 78 ms
71,572 KB
testcase_03 AC 79 ms
71,368 KB
testcase_04 AC 77 ms
71,592 KB
testcase_05 AC 78 ms
71,252 KB
testcase_06 AC 76 ms
71,128 KB
testcase_07 AC 78 ms
71,492 KB
testcase_08 AC 78 ms
71,404 KB
testcase_09 AC 77 ms
71,348 KB
testcase_10 AC 684 ms
86,384 KB
testcase_11 AC 798 ms
91,680 KB
testcase_12 AC 453 ms
83,352 KB
testcase_13 AC 395 ms
82,784 KB
testcase_14 AC 644 ms
93,204 KB
testcase_15 AC 453 ms
85,304 KB
testcase_16 AC 326 ms
88,956 KB
testcase_17 AC 513 ms
81,640 KB
testcase_18 AC 799 ms
86,340 KB
testcase_19 AC 349 ms
86,368 KB
testcase_20 AC 155 ms
79,572 KB
testcase_21 AC 831 ms
85,068 KB
testcase_22 AC 432 ms
93,612 KB
testcase_23 AC 371 ms
87,840 KB
testcase_24 AC 450 ms
93,180 KB
testcase_25 AC 794 ms
85,644 KB
testcase_26 AC 371 ms
89,120 KB
testcase_27 AC 579 ms
86,496 KB
testcase_28 AC 569 ms
83,716 KB
testcase_29 AC 606 ms
84,384 KB
testcase_30 AC 200 ms
94,664 KB
testcase_31 AC 701 ms
85,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import bisect
import sys

MOD = 998244353

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

    """
    初期化
    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, N, add)
    attract = SegTree(0, N, add)
    connect.build([1] * N)
    attract.build(A)


    # 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