結果

問題 No.875 Range Mindex Query
ユーザー kept1994kept1994
提出日時 2022-01-05 23:58:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,987 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 124,284 KB
最終ジャッジ日時 2024-04-24 04:40:27
合計ジャッジ時間 9,218 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
105,472 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
INF = 10 ** 6
class SegmentTree:
    def __init__(self, monoid: int, bottomLen: int, operation: "function"):
        self.monoid = monoid
        self.bottomLen = bottomLen
        self.offset = self.bottomLen        # セグ木の最下層の最初のインデックスに合わせるためのオフセット
        self.segLen = self.bottomLen * 2
        self.tree = [monoid] * self.bottomLen + [[monoid, i]for i in range(self.bottomLen)]
        self.operation = operation
        return
    
    """ 1点取得
    """
    def getPoint(self, index: int):
        segIndex = index + self.offset
        return self.tree[segIndex]
    
    """ 1点更新
    O(1)
    """
    def pointUpdateWithoutRebuild(self, index: int, val: int):
        segIndex = index + self.offset
        self.tree[segIndex] = [val, index]          # 各マスの更新方法
        return
    
    """ 全区間更新
    O(bottomLen) # =セグ木の配列長
    """
    def allBuild(self):
        for segIndex in reversed(range(self.offset)):
            if segIndex == 0:
                return
            self.tree[segIndex] = self.operation(self.tree[segIndex * 2], self.tree[segIndex * 2 + 1])
        return

    """ 1点更新 + リビルド
    O(log(bottomLen))
    """
    def pointUpdate(self, index: int, val: int):
        segIndex = index + self.offset
        self.tree[segIndex] = [val, index]          # 各マスの更新方法
        while True:
            segIndex //= 2
            if segIndex == 0:
                break
            self.tree[segIndex] = self.operation(self.tree[segIndex * 2], self.tree[segIndex * 2 + 1])
        return

    def rangeQuery(self, l: int, r: int):
        l += self.offset
        r += self.offset
        res = [self.monoid, 0]                   # クエリの初期値
        while l < r:
            if l % 2 == 1:
                res = self.operation(res, self.tree[l])
                l += 1
            l //= 2
            if r % 2 == 1:
                res = self.operation(res, self.tree[r - 1])
                r -= 1
            r //= 2
        return res

def minWithIndex(a: "list[val, index]", b: "list[val, index]"):
    # print("minWithIndex", a, b)
    return a if a[0] < b[0] else b

def main():
    N, Q = map(int, input().split())
    A = list(map(int, input().split()))
    tr = SegmentTree(monoid=INF, bottomLen=2**18, operation=minWithIndex)
    for i in range(N):
        tr.pointUpdateWithoutRebuild(i, A[i])
    tr.allBuild()
    # print(tr.tree)
    
    for _ in range(Q):
        t, l, r = map(int, input().split())
        if t == 1:
            al, ar = tr.getPoint(l - 1), tr.getPoint(r - 1)
            tr.pointUpdate(l - 1, ar[0])
            tr.pointUpdate(r - 1, al[0])
            # print(tr.tree)
        else:
            _, i = tr.rangeQuery(l, r + 1)
            print(i + 1)
            # print(tr.tree)

    

    
    return
            

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