結果

問題 No.875 Range Mindex Query
ユーザー c-yanc-yan
提出日時 2020-10-20 00:10:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,253 ms / 2,000 ms
コード長 1,948 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 11,148 KB
実行使用メモリ 27,172 KB
最終ジャッジ日時 2023-09-28 13:31:09
合計ジャッジ時間 9,551 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,068 KB
testcase_01 AC 21 ms
8,104 KB
testcase_02 AC 22 ms
8,056 KB
testcase_03 AC 18 ms
8,196 KB
testcase_04 AC 19 ms
8,040 KB
testcase_05 AC 18 ms
8,464 KB
testcase_06 AC 20 ms
8,624 KB
testcase_07 AC 21 ms
8,072 KB
testcase_08 AC 19 ms
8,104 KB
testcase_09 AC 19 ms
8,176 KB
testcase_10 AC 23 ms
8,576 KB
testcase_11 AC 1,253 ms
22,328 KB
testcase_12 AC 996 ms
18,284 KB
testcase_13 AC 847 ms
24,464 KB
testcase_14 AC 848 ms
23,736 KB
testcase_15 AC 1,207 ms
24,784 KB
testcase_16 AC 891 ms
25,748 KB
testcase_17 AC 966 ms
27,172 KB
testcase_18 AC 936 ms
26,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin


class SegmentTree:
    def __init__(self, size, op, e):
        self._op = op
        self._e = e
        self._size = size
        t = 1
        while t < size:
            t *= 2
        self._offset = t - 1
        self._data = [e] * (t * 2 - 1)

    def __getitem__(self, key):
        return self._data[self._offset + key]

    def __setitem__(self, key, value):
        op = self._op
        data = self._data
        i = self._offset + key
        data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            data[i] = op(data[i * 2 + 1], data[i * 2 + 2])

    def build(self, iterable):
        op = self._op
        data = self._data
        data[self._offset:self._offset + self._size] = iterable
        for i in range(self._offset - 1, -1, -1):
            data[i] = op(data[i * 2 + 1], data[i * 2 + 2])

    def query(self, start, stop):
        def iter_segments(data, l, r):
            while l < r:
                if l & 1 == 0:
                    yield data[l]
                if r & 1 == 0:
                    yield data[r - 1]
                l = l // 2
                r = (r - 1) // 2
        op = self._op
        it = iter_segments(self._data, start + self._offset,
                           stop + self._offset)
        result = self._e
        for v in it:
            result = op(result, v)
        return result


def f(a, b):
    if a[1] < b[1]:
        return a
    else:
        return b


readline = stdin.readline

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

st = SegmentTree(N, f, (N + 1, N + 1))
st.build((i + 1, a[i]) for i in range(N))

result = []
for _ in range(Q):
    t, l, r = map(int, readline().split())
    l, r = l - 1, r - 1
    if t == 1:
        lv = st[l][1]
        rv = st[r][1]
        st[l] = (l + 1, rv)
        st[r] = (r + 1, lv)
    elif t == 2:
        result.append(st.query(l, r + 1)[0])
print(*result, sep='\n')
0