結果

問題 No.875 Range Mindex Query
ユーザー UekiUeki
提出日時 2019-11-10 21:58:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 542 ms / 2,000 ms
コード長 2,315 bytes
コンパイル時間 456 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 99,964 KB
最終ジャッジ日時 2023-10-13 07:54:31
合計ジャッジ時間 6,664 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,312 KB
testcase_01 AC 97 ms
76,552 KB
testcase_02 AC 101 ms
77,008 KB
testcase_03 AC 76 ms
75,776 KB
testcase_04 AC 84 ms
75,936 KB
testcase_05 AC 75 ms
75,136 KB
testcase_06 AC 89 ms
76,500 KB
testcase_07 AC 95 ms
76,496 KB
testcase_08 AC 84 ms
75,800 KB
testcase_09 AC 84 ms
76,100 KB
testcase_10 AC 101 ms
77,352 KB
testcase_11 AC 542 ms
98,024 KB
testcase_12 AC 452 ms
92,756 KB
testcase_13 AC 430 ms
99,964 KB
testcase_14 AC 430 ms
98,028 KB
testcase_15 AC 513 ms
99,636 KB
testcase_16 AC 432 ms
99,080 KB
testcase_17 AC 454 ms
99,180 KB
testcase_18 AC 452 ms
99,088 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()))


def f(a, b):
    if a[0] > b[0]:
        return b
    else:
        return a


Seg = SegmentTree(N, f, 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