結果

問題 No.878 Range High-Element Query
ユーザー tktk_snsn
提出日時 2022-02-19 16:07:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 735 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 82,348 KB
実行使用メモリ 101,912 KB
最終ジャッジ日時 2024-06-29 10:26:50
合計ジャッジ時間 3,224 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


N, Q = map(int, input().split())
D = (N + 1).bit_length()
P = list(map(lambda x: int(x) - 1, input().split()))

par = [[N] * (N + 1) for _ in range(D)]
stack = []
for i, p in enumerate(P):
    while stack and P[stack[-1]] < p:
        j = stack.pop()
        par[0][j] = i
    stack.append(i)
while stack:
    j = stack.pop()
    par[0][j] = N

for d in range(1, D):
    for i in range(N):
        par[d][i] = par[d-1][par[d-1][i]]

for _ in range(Q):
    _, L, R = map(int, input().split())
    x = L - 1
    ans = 1
    for d in reversed(range(D)):
        if par[d][x] >= R:
            continue
        ans += 1 << d
        x = par[d][x]
    print(ans)
0