結果

問題 No.875 Range Mindex Query
ユーザー UekiUeki
提出日時 2019-11-10 22:01:07
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 529 ms / 2,000 ms
コード長 2,290 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 100,072 KB
最終ジャッジ日時 2023-10-13 07:55:06
合計ジャッジ時間 6,226 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,336 KB
testcase_01 AC 95 ms
76,920 KB
testcase_02 AC 102 ms
77,348 KB
testcase_03 AC 79 ms
75,836 KB
testcase_04 AC 83 ms
76,160 KB
testcase_05 AC 78 ms
75,292 KB
testcase_06 AC 91 ms
76,332 KB
testcase_07 AC 95 ms
76,872 KB
testcase_08 AC 82 ms
76,072 KB
testcase_09 AC 83 ms
76,180 KB
testcase_10 AC 102 ms
77,432 KB
testcase_11 AC 529 ms
98,000 KB
testcase_12 AC 456 ms
93,076 KB
testcase_13 AC 442 ms
100,072 KB
testcase_14 AC 432 ms
98,128 KB
testcase_15 AC 513 ms
99,780 KB
testcase_16 AC 427 ms
99,204 KB
testcase_17 AC 454 ms
99,224 KB
testcase_18 AC 448 ms
99,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class SegmentTree():
    '''
    非再帰
    segment tree
    '''

    def __init__(self, n, func, init=float('inf')):
        '''
        n->配列の長さ
        func:func(a,b)->val, func=minだとRMQになる
        木の高さhとすると,
        n:h-1までのノード数。h段目のノードにアクセスするために使う。
        data:ノード。
        parent:k->child k*2+1とk*2+2
        '''
        self.n = 2**(n-1).bit_length()
        self.init = init
        self.data = [init]*(2*self.n)
        self.func = func

    def set(self, k, v):
        '''
        あたいの初期化
        '''
        self.data[k+self.n-1] = v

    def swap(self, x, y):
        x_val, x_ind = self.data[x+self.n-1]
        y_val, y_ind = self.data[y+self.n-1]
        self.update(x, [y_val, x])
        self.update(y, [x_val, y])

    def build(self):
        '''
        setの後に一斉更新
        '''
        for k in reversed(range(self.n-1)):
            self.data[k] = self.func(self.data[k*2+1], self.data[k*2+2])

    def update(self, k, a):
        '''
        list[k]=aに更新する。
        更新ぶんをrootまで更新
        '''
        k += self.n-1
        self.data[k] = a

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

    def query(self, l, r):
        '''
        [l,r)のfuncを求める
        '''
        L = l+self.n
        R = r+self.n
        ret = self.init
        while L < R:
            if R & 1:
                R -= 1
                ret = self.func(ret, self.data[R-1])
            if L & 1:
                ret = self.func(ret, self.data[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return ret


N, Q = map(int, input().split())
L = list(map(int, input().split()))


Seg = SegmentTree(N, lambda a, b: b if a[0] > b[0] else a, init=[
                  float('inf'), -1])
for i, l in enumerate(L):
    Seg.set(i, [l, i])
Seg.build()

for _ in range(Q):
    q, l, r = map(lambda x: int(x)-1, input().split())
    if q == 0:
        Seg.swap(l, r)
    else:
        val, ind = Seg.query(l, r+1)
        print(ind+1)
0