結果

問題 No.875 Range Mindex Query
ユーザー Coki628Coki628
提出日時 2020-08-07 23:26:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 351 ms / 2,000 ms
コード長 3,336 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 82,084 KB
実行使用メモリ 94,856 KB
最終ジャッジ日時 2023-10-25 04:58:42
合計ジャッジ時間 5,655 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,648 KB
testcase_01 AC 59 ms
70,328 KB
testcase_02 AC 70 ms
73,408 KB
testcase_03 AC 44 ms
61,856 KB
testcase_04 AC 53 ms
66,216 KB
testcase_05 AC 42 ms
61,172 KB
testcase_06 AC 59 ms
70,348 KB
testcase_07 AC 61 ms
70,360 KB
testcase_08 AC 51 ms
66,188 KB
testcase_09 AC 54 ms
66,232 KB
testcase_10 AC 68 ms
72,976 KB
testcase_11 AC 351 ms
90,916 KB
testcase_12 AC 322 ms
85,820 KB
testcase_13 AC 320 ms
94,256 KB
testcase_14 AC 311 ms
92,540 KB
testcase_15 AC 351 ms
94,252 KB
testcase_16 AC 305 ms
94,268 KB
testcase_17 AC 320 ms
94,856 KB
testcase_18 AC 327 ms
94,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** -10

class SegTreeIndex:
    """
    セグメント木(index取得対応版)
    1.update: i番目の値をxに更新する
    2.query: 区間[l, r)の値とindex(同値があった場合は一番左)を得る
    """

    def __init__(self, n, func, intv, A=[]):
        """
        :param n: 要素数(0-indexed)
        :param func: 値の操作に使う関数(min, max)
        :param intv: 要素の初期値(単位元)
        :param A: 初期化に使うリスト(オプション)
        """
        self.n = n
        self.func = func
        self.intv = (intv, -1)
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [self.intv] * (n2 << 1)
        if A:
            for i in range(n):
                self.tree[n2+i] = (A[i], i)
            for i in range(n2-1, -1, -1):
                self.tree[i] = self.compare(self.tree[i*2], self.tree[i*2+1])

    def compare(self, a, b):
        if a[0] == b[0]:
            # 同値はindexが小さい方優先
            if a[1] <= b[1]:
                return a
            else:
                return b
        elif self.func(a[0], b[0]) == a[0]:
            return a
        else:
            return b

    def update(self, i, x):
        """
        i番目の値をxに更新
        :param i: index(0-indexed)
        :param x: update value
        """
        i += self.n2
        self.tree[i] = (x, i-self.n2)
        while i > 0:
            i >>= 1
            self.tree[i] = self.compare(self.tree[i*2], self.tree[i*2+1])

    def add(self, i, x):
        self.update(i, self.get(i) + x)

    def query(self, a, b):
        """
        [a, b)の値を得る
        :param a: index(0-indexed)
        :param b: index(0-indexed)
        """
        l = a + self.n2
        r = b + self.n2
        s = self.intv
        while l < r:
            if r & 1:
                r -= 1
                s = self.compare(s, self.tree[r])
            if l & 1:
                s = self.compare(s, self.tree[l])
                l += 1
            l >>= 1
            r >>= 1
        return s

    def get(self, i):
        """ 一点取得 """
        return self.tree[i+self.n2][0]

    def all(self):
        """ 全区間[0, n)の取得 """
        return self.tree[1]

    def print(self):
        for i in range(self.n):
            print(self.get(i), end=' ')
        print()

N, Q = MAP()
A = LIST()

seg = SegTreeIndex(N, min, INF, A)

for _ in range(Q):
    x, l, r = MAP()
    l -= 1; r-= 1
    if x == 1:
        tmp = seg.get(l)
        seg.update(l, seg.get(r))
        seg.update(r, tmp)
    else:
        ans = seg.query(l, r+1)[1] + 1
        print(ans)
0