結果

問題 No.2065 Sum of Min
ユーザー kept1994
提出日時 2022-11-17 01:39:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,291 ms / 2,000 ms
コード長 3,129 bytes
コンパイル時間 206 ms
コンパイル使用メモリ 82,220 KB
実行使用メモリ 174,860 KB
最終ジャッジ日時 2024-09-17 19:53:10
合計ジャッジ時間 23,749 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#!/usr/bin/env python3
class SegTree:
def __init__(self, monoid, bottomList, func, convertLengthToThePowerOf2: bool = False):
# print("index0 使default")
self.monoid = monoid
self.func = func
self.actualLen = len(bottomList)
self.bottomLen = len(bottomList) if not convertLengthToThePowerOf2 else self.getSegLenOfThePowerOf2(len(bottomList))
self.offset = self.bottomLen #
self.segLen = self.bottomLen * 2
self.tree = [monoid] * self.segLen
self.build(bottomList)
"""
O(self.segLen)
"""
def build(self, seq):
#
for i, x in enumerate(seq, self.offset):
self.tree[i] = x
#
for i in range(self.offset - 1, 0, -1):
self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1])
"""
2
"""
def getSegLenOfThePowerOf2(self, ln: int):
if ln <= 0:
return 1
else:
import math
decimalPart, integerPart = math.modf(math.log2(ln))
return 2 ** (int(integerPart) + 1)
"""
O(log(self.bottomLen))
"""
def pointAdd(self, i: int, val: int):
i += self.offset
self.tree[i] = val
while i > 1:
i >>= 1 # 2
self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1]) # 01or
"""
O(log(self.bottomLen))
"""
def getRange(self, l: int, r: int):
l += self.offset
r += self.offset
vL = self.monoid
vR = self.monoid
while l < r:
if l & 1:
vL = self.func(vL, self.tree[l])
l += 1
if r & 1:
r -= 1
vR = self.func(self.tree[r], vR)
l >>= 1
r >>= 1
return self.func(vL, vR)
monoid = [0, 0]
#
def add(A: int, B: int):
return [A[0] + B[0], A[1] + B[1]]
def main():
N, Q = map(int, input().split())
A = list(map(int, input().split()))
queries = [(aa, 100, idx + 1, 0, 0) for idx, aa in enumerate(A)]
seg = SegTree(monoid, [[0, 0] for _ in range(N)], add)
for qi in range(Q):
L, R, X = map(int, input().split())
queries.append((X, 0, L, R, qi))
queries.sort()
ans = [0] * Q
for num, is_query, cidx_l, cidx_r, qi in queries:
if is_query == 100:
# print(cidx_l)
seg.pointAdd(cidx_l - 1, [num, 1])
# print(seg.tree)
else:
# print(seg.tree)
tot, k = seg.getRange(cidx_l - 1, cidx_r)
ideal = cidx_r - cidx_l + 1
res = (ideal - k) * num + tot
ans[qi] = res
# print(ans)
print(*ans, sep="\n")
if __name__ == '__main__':
main()
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0