結果

問題 No.875 Range Mindex Query
ユーザー kawap23kawap23
提出日時 2019-09-07 13:41:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,375 bytes
コンパイル時間 76 ms
コンパイル使用メモリ 10,828 KB
実行使用メモリ 39,412 KB
最終ジャッジ日時 2023-09-08 16:16:09
合計ジャッジ時間 20,043 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
23,648 KB
testcase_01 AC 45 ms
23,672 KB
testcase_02 AC 50 ms
23,924 KB
testcase_03 AC 36 ms
23,804 KB
testcase_04 AC 41 ms
23,640 KB
testcase_05 AC 37 ms
24,248 KB
testcase_06 AC 48 ms
23,736 KB
testcase_07 AC 44 ms
23,728 KB
testcase_08 AC 41 ms
23,648 KB
testcase_09 AC 41 ms
23,632 KB
testcase_10 AC 49 ms
23,972 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 1,634 ms
39,336 KB
testcase_14 AC 1,628 ms
35,540 KB
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    #セグメント木
    #次の2つの処理をO(log N)で実施することができる
    # s, tが与えられた時、区間[s, t)の最小値を求める
    # i, xが与えられた時、a_iの値をxに変更する

    INIT = 10 ** 18 #初期値 答えに関係ないもの
    MAX_N = 10 ** 6 #与えられる要素数の最大

    data = [INIT] * (2 * MAX_N - 1) #MAX_Nで初期化

    #ここに機能をもたせる
    def func(vl, vr):
        return min(vl, vr)

    def init(n_):
        #簡単のため要素数を2のべき乗に
        n = 1
        while n < n_:
            n *= 2
        return n

    N, Q = map(int ,input().split()) #配列の数
    n = init(N)

    def update(k, a): #k番目の値をaに変更 0-index
        B[a] = k
        k += n - 1
        data[k] = a
        while k > 0: #登りながら更新
            k = (k - 1)//2
            data[k] = func(data[k * 2 + 1], data[k * 2 + 2])

    def query(a, b, k = 0, l = 0, r = n):
        if r <= a or b <= l:
            return INIT
        if a <= l and r <= b:
            return data[k]
        else:
            vl = query(a, b, k * 2 + 1, l, (l + r)//2)
            vr = query(a, b, k * 2 + 2, (l + r)//2, r)
            return func(vl, vr)

    def initialize(A): #リストAを与えて初期化する
        for i, a in enumerate(A):
            data[i + n - 1] = a
            B[a] = i
        for i in range((2 * n - 3)//2, -1, -1):
            data[i] = func(data[i * 2 + 1], data[i * 2 + 2])

    def index(a): #要素aがもとのどこに保存されているかを返す関数
        return B[a]

    def value(index): #indexが与えられた時、index番目の値を得る
        return data[index + n - 1]

    #初期値の入力
    A = list(map(int, input().split()))
    B = dict() #同じ要素がないのが前提 0-index管理
    initialize(A)
    # print (data[:16])
    #クエリの数
    # Q = int(input())
    for _ in range(Q):
        #区間[a, b)の値
        t, a, b = map(int, input().split())
        a -= 1
        b -= 1
        if t == 1:
            update(a, A[b])
            update(b, A[a])
            A[a], A[b] = A[b], A[a]
        else:
            # print (query(a, b + 1))
            print (index(query(a, b + 1)) + 1)

    # print (data[:16])
    # print (B)

if __name__ == '__main__':
    main()
0