結果

問題 No.878 Range High-Element Query
ユーザー maspymaspy
提出日時 2020-03-21 03:45:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 664 ms / 2,000 ms
コード長 822 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 34,736 KB
最終ジャッジ日時 2024-05-09 16:49:55
合計ジャッジ時間 7,158 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,624 KB
testcase_01 AC 33 ms
10,880 KB
testcase_02 AC 35 ms
11,136 KB
testcase_03 AC 35 ms
11,136 KB
testcase_04 AC 33 ms
10,880 KB
testcase_05 AC 34 ms
10,880 KB
testcase_06 AC 32 ms
10,624 KB
testcase_07 AC 32 ms
10,752 KB
testcase_08 AC 35 ms
11,008 KB
testcase_09 AC 35 ms
10,880 KB
testcase_10 AC 32 ms
10,624 KB
testcase_11 AC 626 ms
30,848 KB
testcase_12 AC 474 ms
30,004 KB
testcase_13 AC 514 ms
27,096 KB
testcase_14 AC 400 ms
23,988 KB
testcase_15 AC 478 ms
29,104 KB
testcase_16 AC 644 ms
33,760 KB
testcase_17 AC 662 ms
34,736 KB
testcase_18 AC 664 ms
34,624 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