結果

問題 No.875 Range Mindex Query
ユーザー aqua_tenhouaqua_tenhou
提出日時 2021-07-11 22:36:09
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 401 ms / 2,000 ms
コード長 2,374 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 94,176 KB
最終ジャッジ日時 2023-09-14 20:25:12
合計ジャッジ時間 6,603 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,468 KB
testcase_01 AC 101 ms
76,628 KB
testcase_02 AC 111 ms
77,460 KB
testcase_03 AC 80 ms
75,704 KB
testcase_04 AC 88 ms
76,696 KB
testcase_05 AC 80 ms
75,784 KB
testcase_06 AC 94 ms
76,608 KB
testcase_07 AC 103 ms
76,636 KB
testcase_08 AC 90 ms
76,700 KB
testcase_09 AC 91 ms
76,648 KB
testcase_10 AC 110 ms
77,596 KB
testcase_11 AC 401 ms
94,040 KB
testcase_12 AC 368 ms
91,148 KB
testcase_13 AC 340 ms
93,376 KB
testcase_14 AC 339 ms
91,800 KB
testcase_15 AC 397 ms
93,680 KB
testcase_16 AC 379 ms
93,540 KB
testcase_17 AC 394 ms
94,016 KB
testcase_18 AC 385 ms
94,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#####segfunc#####
def segfunc(x, y):
    return min(x,y)
#################
#####ide_ele#####
ide_ele = 10**9
#################
class SegTree:
    '''
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    '''
    def __init__(self, init_val, segfunc, ide_ele):
        '''
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        '''
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])
    def update(self, k, x):
        '''
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        '''
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1
    def query(self, l, r):
        '''
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        '''
        res = self.ide_ele
        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res
#aは配列
#seg = SegTree(a, segfunc, ide_ele)

n,q = map(int,input().split())
a = [0] + list(map(int,input().split())) + [0]*10
z = [list(map(int,input().split())) for i in range(q)]

d = [0 for i in range(n+1)]
for i,x in enumerate(a):
    d[x] = i

seg = SegTree(a, segfunc, ide_ele)

for t,l,r in z:
    if t == 1:
        d[a[l]], d[a[r]] = d[a[r]], d[a[l]]
        a[l], a[r] = a[r], a[l]
        seg.update(l, a[l])
        seg.update(r, a[r])
    else:
        p = seg.query(l,r+1)
        print(d[p])
0