結果

問題 No.875 Range Mindex Query
ユーザー H3PO4H3PO4
提出日時 2021-03-02 11:27:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,837 ms / 2,000 ms
コード長 1,787 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 20,748 KB
最終ジャッジ日時 2024-04-14 04:30:12
合計ジャッジ時間 14,364 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,880 KB
testcase_01 AC 39 ms
11,008 KB
testcase_02 AC 40 ms
11,008 KB
testcase_03 AC 32 ms
10,880 KB
testcase_04 AC 34 ms
10,752 KB
testcase_05 AC 33 ms
11,136 KB
testcase_06 AC 36 ms
11,008 KB
testcase_07 AC 36 ms
10,880 KB
testcase_08 AC 35 ms
10,880 KB
testcase_09 AC 34 ms
10,880 KB
testcase_10 AC 39 ms
11,008 KB
testcase_11 AC 1,837 ms
19,068 KB
testcase_12 AC 1,459 ms
17,116 KB
testcase_13 AC 1,241 ms
20,232 KB
testcase_14 AC 1,227 ms
19,808 KB
testcase_15 AC 1,723 ms
20,424 KB
testcase_16 AC 1,298 ms
20,428 KB
testcase_17 AC 1,429 ms
20,688 KB
testcase_18 AC 1,362 ms
20,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.buffer.readline


class SegmentTree:
    """
    https://qiita.com/dn6049949/items/afa12d5d079f518de368 から拝借しています。
    """

    def __init__(self, size, f=lambda x, y: min(x, y), default=10 ** 6):
        self.size = 2 ** (size - 1).bit_length()
        self.default = default
        self.dat = [default] * (self.size * 2)
        self.f = f

    def initialize(self, A):
        for i, a in enumerate(A, self.size):
            self.dat[i] = a
        for i in range(self.size - 1, 0, -1):
            self.dat[i] = self.f(self.dat[i * 2], self.dat[i * 2 + 1])

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i * 2], self.dat[i * 2 + 1])

    def query(self, l, r):
        """半開区間[l,r)"""
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1
            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres)
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res


N, Q = map(int, input().split())
A = list(map(int, input().split()))
indices = [0] * (N + 1)
for i, a in enumerate(A):
    indices[a] = i
seg = SegmentTree(N)
seg.initialize(A)
for _ in range(Q):
    q, *lr = map(int, input().split())
    if q == 1:
        l, r = lr
        l -= 1
        r -= 1
        al, ar = A[l], A[r]
        A[r], A[l] = al, ar
        indices[ar], indices[al] = l, r
        seg.update(r, al)
        seg.update(l, ar)
    else:  # q=2
        l, r = lr
        print(indices[seg.query(l - 1, r)] + 1)
0