結果

問題 No.875 Range Mindex Query
ユーザー L_or_R_0228L_or_R_0228
提出日時 2021-12-21 02:49:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 987 ms / 2,000 ms
コード長 2,773 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 87,256 KB
実行使用メモリ 135,948 KB
最終ジャッジ日時 2023-10-13 19:46:34
合計ジャッジ時間 12,843 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 266 ms
91,056 KB
testcase_01 AC 287 ms
91,688 KB
testcase_02 AC 301 ms
92,604 KB
testcase_03 AC 269 ms
91,868 KB
testcase_04 AC 275 ms
91,440 KB
testcase_05 AC 267 ms
91,096 KB
testcase_06 AC 285 ms
92,072 KB
testcase_07 AC 294 ms
92,204 KB
testcase_08 AC 279 ms
92,040 KB
testcase_09 AC 277 ms
91,748 KB
testcase_10 AC 299 ms
92,444 KB
testcase_11 AC 987 ms
128,624 KB
testcase_12 AC 830 ms
110,932 KB
testcase_13 AC 845 ms
134,180 KB
testcase_14 AC 810 ms
134,688 KB
testcase_15 AC 943 ms
135,948 KB
testcase_16 AC 898 ms
129,732 KB
testcase_17 AC 942 ms
130,076 KB
testcase_18 AC 942 ms
129,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def int_sp():
    return map(int, input().split())


def li_int_sp():
    return list(map(int, input().split()))

def trans_li_int_sp():
    return list(map(list, (zip(*[li_int_sp() for _ in range(N)]))))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

def comb(n, k):
    return factorial(n) // (factorial(k) * factorial(n - k))

#####segfunc#####
def segfunc(x, y):
    if x[0] < y[0]:
        return x
    else:
        return y
#################

#####単位元#####
ide_ele = 2**31-1
#################

class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [[ide_ele, ide_ele] for _ in range(2 * self.num)]
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 葉からどんどん上へ構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])


    def update(self, k, x):
        """
        k番目の値をxに更新.
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = [self.ide_ele, self.ide_ele]

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

import pdb
import math
N, Q = int_sp()
A = li_int_sp()
B = []
for i in range(N):
    B.append([A[i], i])
seg = SegTree(B, segfunc, ide_ele)
for _ in range(Q):
    #pdb.set_trace()
    com, l, r = int_sp()
    if com == 1:
        x = seg.tree[seg.num + l-1]
        y = seg.tree[seg.num + r-1]
        seg.update(l-1, [y[0], x[1]])
        seg.update(r-1, [x[0], y[1]])
    else:
        print(seg.query(l-1, r)[1]+1)
0