結果

問題 No.878 Range High-Element Query
ユーザー stngstng
提出日時 2022-08-21 09:23:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 788 ms / 2,000 ms
コード長 1,527 bytes
コンパイル時間 362 ms
コンパイル使用メモリ 82,376 KB
実行使用メモリ 98,856 KB
最終ジャッジ日時 2024-04-18 08:59:50
合計ジャッジ時間 7,743 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
54,104 KB
testcase_01 AC 72 ms
72,544 KB
testcase_02 AC 89 ms
76,124 KB
testcase_03 AC 72 ms
73,808 KB
testcase_04 AC 69 ms
72,364 KB
testcase_05 AC 73 ms
74,360 KB
testcase_06 AC 54 ms
63,732 KB
testcase_07 AC 53 ms
63,884 KB
testcase_08 AC 83 ms
75,836 KB
testcase_09 AC 85 ms
76,536 KB
testcase_10 AC 56 ms
64,732 KB
testcase_11 AC 654 ms
97,932 KB
testcase_12 AC 577 ms
90,852 KB
testcase_13 AC 594 ms
94,284 KB
testcase_14 AC 487 ms
89,388 KB
testcase_15 AC 545 ms
91,596 KB
testcase_16 AC 788 ms
98,492 KB
testcase_17 AC 729 ms
98,856 KB
testcase_18 AC 759 ms
98,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

n,q = map(int,input().split())
a = [int(i) for i in input().split()]
qry = [[int(i) for i in input().split()]+[j] for j in range(q)]

ru = Bit(n+1)

#print(qry)

qry.sort(key=lambda x:-x[1])
_,l,r,IDX = qry[0]
l -= 1
r -= 1
hp = []
heapq.heapify(hp)
ans = [0]*(q)
for i in range(n-1,l-1,-1):
    ru.add(i+1,1)
    val = a[i]
    while len(hp):
        tmp,idx = heapq.heappop(hp)
        if val > tmp:
            ru.add(idx,-1)
        else:
            heapq.heappush(hp,[tmp,idx])
            break

    heapq.heappush(hp,[val,i+1])
ans[IDX] = ru.sum(r+1)-ru.sum(l)
#print(ans)
#exit()
L = l
for i in range(1,q):
    _,l,r,IDX = qry[i]
    #print(qry[i],"i")
    l -= 1
    r -= 1
    if L > l:
        for j in range(L-1,l-1,-1):
            ru.add(j+1,1)
            val = a[j]
            while len(hp):
                tmp,idx = heapq.heappop(hp)
                if val > tmp:
                    ru.add(idx,-1)
                else:
                    heapq.heappush(hp,[tmp,idx])
                    break
            heapq.heappush(hp,[val,j+1])
    L = l
    #print(ru.sum(r+1),i,"ii")
    ans[IDX] = ru.sum(r+1)-ru.sum(l)
    #print(idx,"idx")

print(*ans,sep="\n")
0