結果

問題 No.878 Range High-Element Query
ユーザー neterukunneterukun
提出日時 2019-09-06 23:16:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 690 ms / 2,000 ms
コード長 1,864 bytes
コンパイル時間 376 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 119,952 KB
最終ジャッジ日時 2023-09-07 02:31:09
合計ジャッジ時間 7,768 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,188 KB
testcase_01 AC 114 ms
77,660 KB
testcase_02 AC 109 ms
76,940 KB
testcase_03 AC 88 ms
76,088 KB
testcase_04 AC 103 ms
76,452 KB
testcase_05 AC 117 ms
77,968 KB
testcase_06 AC 89 ms
76,304 KB
testcase_07 AC 92 ms
76,264 KB
testcase_08 AC 132 ms
77,912 KB
testcase_09 AC 117 ms
77,920 KB
testcase_10 AC 95 ms
76,280 KB
testcase_11 AC 633 ms
111,568 KB
testcase_12 AC 458 ms
115,704 KB
testcase_13 AC 550 ms
103,384 KB
testcase_14 AC 432 ms
99,680 KB
testcase_15 AC 476 ms
111,740 KB
testcase_16 AC 675 ms
119,952 KB
testcase_17 AC 690 ms
118,360 KB
testcase_18 AC 687 ms
118,164 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
    
ruiseki = [0]*(n+1)
for i in range(n):
    if i == 0:
        ruiseki[i+1] = 1
    elif a[i-1] < a[i]:
        ruiseki[i+1] = ruiseki[i] + 1
    else:
        ruiseki[i+1] = ruiseki[i]
#print(ruiseki)

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