結果

問題 No.875 Range Mindex Query
ユーザー 👑 timitimi
提出日時 2020-10-08 22:36:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 847 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 328 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 98,308 KB
最終ジャッジ日時 2023-09-27 12:03:52
合計ジャッジ時間 9,250 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
71,300 KB
testcase_01 AC 93 ms
75,956 KB
testcase_02 AC 99 ms
75,960 KB
testcase_03 AC 73 ms
75,788 KB
testcase_04 AC 80 ms
75,836 KB
testcase_05 AC 69 ms
71,012 KB
testcase_06 AC 86 ms
75,948 KB
testcase_07 AC 89 ms
76,120 KB
testcase_08 AC 80 ms
75,720 KB
testcase_09 AC 82 ms
75,800 KB
testcase_10 AC 95 ms
75,920 KB
testcase_11 AC 840 ms
96,436 KB
testcase_12 AC 743 ms
90,732 KB
testcase_13 AC 682 ms
97,648 KB
testcase_14 AC 678 ms
98,308 KB
testcase_15 AC 847 ms
97,628 KB
testcase_16 AC 763 ms
97,804 KB
testcase_17 AC 808 ms
97,768 KB
testcase_18 AC 774 ms
97,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,Q=map(int, input().split())
A=list(map(int, input().split()))
#####segfunc#####
def segfunc(x, y):
    return min(x,y)

#################

#####ide_ele#####
ide_ele =float('inf')
#################

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
B=[[A[i],i] for i in range(N)]
st=SegTree(B,min,[2**31-1,-1])
for q in range(Q):
  com,x,y=map(int, input().split())
  if com==2:
      print(st.query(x-1,y)[1]+1)
  else:
      B[x-1][0],B[y-1][0]=B[y-1][0],B[x-1][0]
      st.update(x-1,B[x-1])
      st.update(y-1,B[y-1])
0