結果

問題 No.875 Range Mindex Query
ユーザー mlihua09mlihua09
提出日時 2020-08-31 17:28:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 552 ms / 2,000 ms
コード長 1,643 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 93,104 KB
最終ジャッジ日時 2024-04-28 01:05:17
合計ジャッジ時間 5,878 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,492 KB
testcase_01 AC 64 ms
68,068 KB
testcase_02 AC 73 ms
70,724 KB
testcase_03 AC 49 ms
60,800 KB
testcase_04 AC 57 ms
64,480 KB
testcase_05 AC 53 ms
64,796 KB
testcase_06 AC 62 ms
67,284 KB
testcase_07 AC 70 ms
69,108 KB
testcase_08 AC 57 ms
65,640 KB
testcase_09 AC 59 ms
64,780 KB
testcase_10 AC 72 ms
71,712 KB
testcase_11 AC 444 ms
89,480 KB
testcase_12 AC 399 ms
84,160 KB
testcase_13 AC 372 ms
93,040 KB
testcase_14 AC 373 ms
91,156 KB
testcase_15 AC 445 ms
93,104 KB
testcase_16 AC 515 ms
92,604 KB
testcase_17 AC 552 ms
93,024 KB
testcase_18 AC 538 ms
92,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


class SegmentTree:
    
    def __init__(self, N, A):
        
        self.n = 2 ** (N.bit_length())
        
        self.A = A + [N + 1]
        
        self.Tree = [N] * (self.n * 2 - 1)
        
        for i in range(N):
            
            self.Tree[self.n + i - 1] = i
        
            self.update(i)
    
    
    def update(self, i):
        
        i = (self.n + i) // 2 - 1
        
        while i >= 0:
            
            
            if self.A[self.Tree[i * 2 + 1]] < self.A[self.Tree[i * 2 + 2]]:
                
                self.Tree[i] = self.Tree[i * 2 + 1]
                
            else:
                
                self.Tree[i] = self.Tree[i * 2 + 2]
                
            i -= 1
            i //= 2
            
    def query(self, l, r):
        
        l -= 1
        
        i = l + self.n - 1
        
        ans = self.Tree[i]
        x = 2
        while l < r:
            if l + x <= r and i % 2:
                i = (i - 1) // 2
                x *= 2
            
            else:
                l = l + x // 2
                if self.A[ans] > self.A[self.Tree[i]]:
                    ans = self.Tree[i]
                
                i = l + self.n - 1
                x = 2
                
        return ans + 1

        
N, Q = map(int, input().split())

A = list(map(int, input().split()))

X = SegmentTree(N, A)

for i in range(Q):
    
    q, l, r = map(int, input().split())
    
    if q == 1:
        
        X.A[l - 1], X.A[r - 1] = X.A[r - 1], X.A[l - 1]
        X.update(l - 1)
        X.update(r - 1)
    else:
        
        print(X.query(l, r))
0