結果

問題 No.878 Range High-Element Query
ユーザー rlangevinrlangevin
提出日時 2023-09-06 08:54:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 450 ms / 2,000 ms
コード長 1,428 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 86,736 KB
実行使用メモリ 93,380 KB
最終ジャッジ日時 2023-09-06 08:55:04
合計ジャッジ時間 5,571 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,252 KB
testcase_01 AC 103 ms
75,456 KB
testcase_02 AC 102 ms
75,560 KB
testcase_03 AC 95 ms
75,840 KB
testcase_04 AC 93 ms
75,596 KB
testcase_05 AC 94 ms
75,732 KB
testcase_06 AC 93 ms
75,556 KB
testcase_07 AC 83 ms
75,348 KB
testcase_08 AC 95 ms
75,764 KB
testcase_09 AC 99 ms
75,776 KB
testcase_10 AC 84 ms
75,380 KB
testcase_11 AC 419 ms
90,876 KB
testcase_12 AC 343 ms
91,484 KB
testcase_13 AC 309 ms
87,708 KB
testcase_14 AC 319 ms
86,796 KB
testcase_15 AC 300 ms
90,196 KB
testcase_16 AC 448 ms
92,920 KB
testcase_17 AC 417 ms
93,380 KB
testcase_18 AC 450 ms
93,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from heapq import *

class Fenwick_Tree:
    def __init__(self, n):
        self._n = n
        self.data = [0] * n
 
    def add(self, p, x):
        assert 0 <= p < self._n
        p += 1
        while p <= self._n:
            self.data[p - 1] += x
            p += p & -p
 
    def sum(self, l, r):
        assert 0 <= l <= r <= self._n
        return self._sum(r) - self._sum(l)
 
    def _sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s
    
    def get(self, k):
        k += 1
        x, r = 0, 1
        while r < self._n:
            r <<= 1
        len = r
        while len:
            if x + len - 1 < self._n:
                if self.data[x + len - 1] < k:
                    k -= self.data[x + len - 1]
                    x += len
            len >>= 1
        return x

N, Q = map(int, input().split())
A = list(map(int, input().split()))
query = [[] for i in range(N)]
for i in range(Q):
    _, l, r = map(int, input().split())
    query[l - 1].append((r, i))

ans = [-1] * Q
T = Fenwick_Tree(N)
H = []
for i in range(N - 1, -1, -1):
    while H:
        if H[0][0] < A[i]:
            v, ib = heappop(H)
            T.add(ib, -1)
        else:
            break
    T.add(i, 1)
    heappush(H, (A[i], i))
    for r, iq in query[i]:
        ans[iq] = T.sum(i, r)
    
for a in ans:
    print(a)
0