結果

問題 No.875 Range Mindex Query
ユーザー AEnAEn
提出日時 2022-06-16 23:13:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 570 ms / 2,000 ms
コード長 2,198 bytes
コンパイル時間 241 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 116,096 KB
最終ジャッジ日時 2024-04-16 12:56:19
合計ジャッジ時間 6,182 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,144 KB
testcase_01 AC 68 ms
68,608 KB
testcase_02 AC 76 ms
72,320 KB
testcase_03 AC 49 ms
61,440 KB
testcase_04 AC 56 ms
63,744 KB
testcase_05 AC 44 ms
55,552 KB
testcase_06 AC 63 ms
67,072 KB
testcase_07 AC 75 ms
70,016 KB
testcase_08 AC 56 ms
64,640 KB
testcase_09 AC 58 ms
64,768 KB
testcase_10 AC 74 ms
72,576 KB
testcase_11 AC 537 ms
109,664 KB
testcase_12 AC 487 ms
96,980 KB
testcase_13 AC 453 ms
115,840 KB
testcase_14 AC 450 ms
112,512 KB
testcase_15 AC 521 ms
116,064 KB
testcase_16 AC 520 ms
115,860 KB
testcase_17 AC 570 ms
116,096 KB
testcase_18 AC 550 ms
116,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
class SegmentTree():
    """UnitXは単位元、fは区間で行いたい操作、initは自然数あるいは配列"""
    def __init__(self, init, unitX, f):
        self.f = f # (X, X) -> X
        self.unitX = unitX
        self.f = f
        if type(init) == int:
            self.n = init
            self.n = 1 << (self.n - 1).bit_length()
            self.X = [unitX] * (self.n * 2)
        else:
            self.n = len(init)
            self.n = 1 << (self.n - 1).bit_length()
            # len(init)が2の累乗ではない時UnitXで埋める
            self.X = [unitX] * self.n + init + [unitX] * (self.n - len(init))
            # 配列のindex1まで埋める
            for i in range(self.n-1, 0, -1):
                self.X[i] = self.f(self.X[i*2], self.X[i*2|1])
    
    # 0-indexedのi番目の値をxで置換
    def update(self, i, x):
        # 最下段に移動
        i += self.n
        self.X[i] = x
        # 上向に更新
        i >>= 1
        while i:
            self.X[i] = self.f(self.X[i*2], self.X[i*2|1])
            i >>= 1
    
    # 元の配列のindexの値を見る
    def getvalue(self, i):
        return self.X[i + self.n]
    
    # 区間[l, r)でのfを行った値
    def getrange(self, l, r):
        l += self.n
        r += self.n
        al = self.unitX
        ar = self.unitX
        while l < r:
            # 左端が右子ノードであれば
            if l & 1:
                al = self.f(al, self.X[l])
                l += 1
            # 右端が右子ノードであれば
            if r & 1:
                r -= 1
                ar = self.f(self.X[r], ar)
            l >>= 1
            r >>= 1
        return self.f(al, ar)
    
 
N, Q = map(int, input().split())
a = list(map(int, input().split()))
d = defaultdict(int)
for i in range(N):
    d[a[i]] = i+1
st = SegmentTree(a, float('inf'), min)
for i in range(Q):
    p, l, r = map(int, input().split())
    if p == 1:
        x, y = st.getvalue(l-1),st.getvalue(r-1)
        d[x], d[y] = d[y], d[x]
        st.update(l-1, y)
        st.update(r-1, x)
    else:
        res = st.getrange(l-1, r)
        print(d[res])
0