結果

問題 No.875 Range Mindex Query
ユーザー maspymaspy
提出日時 2020-04-01 17:49:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,853 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 10,856 KB
実行使用メモリ 35,928 KB
最終ジャッジ日時 2023-09-09 08:21:30
合計ジャッジ時間 8,405 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,172 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, Q = map(int, readline().split())
A = list(map(int, readline().split()))
m = map(int, read().split())
query = zip(m, m, m)


class SegTree:
    """ segment tree with point modification and range product. """
    unit = 1 << 30
    f = min

    def __init__(self, N):
        self.N = N
        self.data = [self.unit] * (N + N)

    def build(self, raw_data):
        data = self.data
        f = self.f
        N = self.N
        data[N:] = raw_data[:]
        for i in range(N - 1, 0, -1):
            data[i] = f(data[i << 1], data[i << 1 | 1])

    def set_val(self, i, x):
        data = self.data
        f = self.f
        i += self.N
        data[i] = x
        while i > 1:
            data[i >> 1] = f(data[i], data[i ^ 1])
            i >>= 1

    def fold(self, L, R):
        """ compute for [L, R) """
        vL = vR = self.unit
        data = self.data
        f = self.f
        while L < R:
            if L & 1:
                vL = f(vL, data[L])
                L += 1
            if R & 1:
                R -= 1
                vR = f(data[R], vR)
            L >>= 1
            R >>= 1
        return f(vL, vR)


seg = SegTree(N)
seg.build(A)

num_ind = [0] * (N + 1)
for i, x in enumerate(A, 1):
    num_ind[x] = i


def solve():
    def query_1(L, R):
        x = A[L]
        y = A[R]
        A[L], A[R] = y, x
        seg.set_val(L, y)
        seg.set_val(R, x)
        num_ind[L] = y
        num_ind[R] = x

    def query_2(L, R):
        value = seg.fold(L, R + 1)
        return num_ind[value]

    for t, L, R in query:
        L -= 1
        R -= 1
        if t == 1:
            query_1(L, R)
        else:
            yield query_2(L, R)


print('\n'.join(map(str, solve())))
0