結果

問題 No.878 Range High-Element Query
ユーザー neterukunneterukun
提出日時 2019-09-06 23:25:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 651 ms / 2,000 ms
コード長 1,565 bytes
コンパイル時間 1,079 ms
コンパイル使用メモリ 87,020 KB
実行使用メモリ 120,084 KB
最終ジャッジ日時 2023-09-07 02:40:08
合計ジャッジ時間 7,755 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,356 KB
testcase_01 AC 112 ms
78,540 KB
testcase_02 AC 106 ms
76,684 KB
testcase_03 AC 87 ms
76,384 KB
testcase_04 AC 103 ms
76,572 KB
testcase_05 AC 114 ms
76,928 KB
testcase_06 AC 86 ms
76,144 KB
testcase_07 AC 86 ms
76,252 KB
testcase_08 AC 111 ms
77,532 KB
testcase_09 AC 109 ms
77,032 KB
testcase_10 AC 100 ms
76,856 KB
testcase_11 AC 616 ms
110,608 KB
testcase_12 AC 430 ms
114,900 KB
testcase_13 AC 511 ms
103,032 KB
testcase_14 AC 396 ms
99,932 KB
testcase_15 AC 444 ms
111,392 KB
testcase_16 AC 613 ms
120,084 KB
testcase_17 AC 651 ms
118,668 KB
testcase_18 AC 631 ms
118,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SparseTable():
    def __init__(self, array, n):
        '''テーブルを構築する'''
        self.row_size = n.bit_length()

        # log_tableを構築する
        # log_table = [0, 0, 1, 1, 2, 2, 2, 2, ...]
        self.log_table = [0] * (n + 1)
        for i in range(2, n + 1):
            self.log_table[i] = self.log_table[i//2] + 1

        # sparse_tableを構築する
        self.sparse_table = [[0] * n for _ in range(self.row_size)]
        for i in range(n):
            self.sparse_table[0][i] = array[i]
        for row in range(1, self.row_size):
            for i in range(n - (1 << row) + 1):
               self.sparse_table[row][i] = self._merge(self.sparse_table[row - 1][i], \
                                                       self.sparse_table[row - 1][i + (1 << row - 1)])

    def _merge(self, num1, num2):
        '''クエリの内容'''
        return max(num1, num2)

    def query(self, l, r):
        '''区間[l, r)に対するクエリに答える'''
        if r == l:
            return None
        row = self.log_table[r - l]
        return self._merge(self.sparse_table[row][l], self.sparse_table[row][r - (1 << row)])



n, q = map(int, input().split())
a = list(map(int, input().split()))
sp = SparseTable(a, n)

ind_memo = {}
for i in range(n):
    ind_memo[a[i]] = i
    

for _ in range(q):
    _, l, r = map(int, input().split())
    ans = 0
    while True:
        if l-1 == r:
            break
        max_ind = ind_memo[sp.query(l - 1, r)]
        ans += 1
        r = max_ind
    print(ans)
    
0