結果

問題 No.875 Range Mindex Query
ユーザー hase_0o0
提出日時 2019-10-25 11:41:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 1,284 bytes
コンパイル時間 1,362 ms
コンパイル使用メモリ 81,616 KB
実行使用メモリ 91,196 KB
最終ジャッジ日時 2024-07-19 07:59:57
合計ジャッジ時間 4,998 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline

class SegmentTree:
    def __init__(self, n): #a1, a2, ..., an
        n_ = 1
        while n_ < n:
            n_ *= 2

        self.n = n_
        self.arr = [10**18] * (2*self.n-1)

    def update(self, k, a): #0_indexed
        k += self.n - 1
        self.arr[k] = a

        while k > 0:
            k = (k-1) // 2
            self.arr[k] = min(self.arr[2*k+1], self.arr[2*k+2])

    def query(self, l, r): #min(a[l:r))
        L, R = l+self.n, r+self.n
        ide_ele = float("inf")
        res = ide_ele

        while L < R:
            if R & 1:
                R -= 1
                res = min(res, self.arr[R-1])

            if L & 1:
                res = min(res, self.arr[L-1])
                L += 1

            L >>= 1
            R >>= 1

        return res

from collections import defaultdict
n,q=map(int,input().split())
a=list(map(int,input().split()))
st=SegmentTree(n)
idx=defaultdict(int)

for i in range(n):
    st.update(i,a[i])
    idx[a[i]]=i

for _ in range(q):
    i,l,r=map(int,input().split())
    if i==1:
        st.update(l-1,a[r-1])
        st.update(r-1,a[l-1])
        idx[a[r-1]]=l-1
        idx[a[l-1]]=r-1
        a[l-1],a[r-1]=a[r-1],a[l-1]
    else:
        x=st.query(l-1,r)
        print(idx[x]+1)
0