結果

問題 No.878 Range High-Element Query
ユーザー maspymaspy
提出日時 2020-03-21 03:45:32
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 508 ms / 2,000 ms
コード長 822 bytes
コンパイル時間 95 ms
コンパイル使用メモリ 10,864 KB
実行使用メモリ 33,512 KB
最終ジャッジ日時 2023-08-22 11:10:08
合計ジャッジ時間 5,811 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,772 KB
testcase_01 AC 18 ms
8,300 KB
testcase_02 AC 19 ms
8,364 KB
testcase_03 AC 18 ms
8,208 KB
testcase_04 AC 18 ms
8,304 KB
testcase_05 AC 18 ms
8,344 KB
testcase_06 AC 17 ms
7,776 KB
testcase_07 AC 17 ms
8,200 KB
testcase_08 AC 18 ms
8,408 KB
testcase_09 AC 18 ms
8,468 KB
testcase_10 AC 17 ms
7,856 KB
testcase_11 AC 451 ms
29,868 KB
testcase_12 AC 362 ms
28,352 KB
testcase_13 AC 368 ms
25,644 KB
testcase_14 AC 278 ms
22,308 KB
testcase_15 AC 362 ms
27,556 KB
testcase_16 AC 474 ms
32,448 KB
testcase_17 AC 508 ms
33,480 KB
testcase_18 AC 487 ms
33,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


N, Q = map(int, readline().split())
A = [0] + list(map(int, readline().split())) + [N + 1]
stack = [N + 1]

next_high = [N + 1] * (N + 2)
for n in range(N, -1, -1):
    x = A[n]
    while x > A[stack[-1]]:
        stack.pop()
    next_high[n] = stack[-1]
    stack.append(n)

dp = [None] * 17
dp[0] = next_high
for n in range(1, 17):
    prev = dp[n - 1]
    dp[n] = [prev[prev[i]] for i in range(N + 2)]


def solve():
    _, L, R = map(int, readline().split())
    ret = 1
    for n in range(16, -1, -1):
        m = dp[n][L]
        if m <= R:
            ret += 1 << n
            L = m
    return ret


answers = (solve() for _ in range(Q))
print('\n'.join(map(str, answers)))
0