結果
問題 |
No.2065 Sum of Min
|
ユーザー |
![]() |
提出日時 | 2023-04-03 12:48:11 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 917 ms / 2,000 ms |
コード長 | 1,570 bytes |
コンパイル時間 | 292 ms |
コンパイル使用メモリ | 82,076 KB |
実行使用メモリ | 110,592 KB |
最終ジャッジ日時 | 2024-09-25 00:46:19 |
合計ジャッジ時間 | 18,297 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
import sys readline = sys.stdin.readline 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, readline().split()) A = list(map(int, readline().split())) D = [] for i in range(N): D.append((A[i], -1, -1, i)) for i in range(Q): L, R, X = map(int, readline().split()) L -= 1 D.append((X, L, R, i)) D.sort() BIT_X = Fenwick_Tree(N) BIT_A = Fenwick_Tree(N) BIT_AA = Fenwick_Tree(N) ans = [0] * Q for i in range(N): BIT_X.add(i, 1) BIT_A.add(i, -A[i]) BIT_AA.add(i, A[i]) for X, L, R, i in D: if L == -1: BIT_X.add(i, -1) BIT_A.add(i, A[i]) else: ans[i] = BIT_AA.sum(L, R) + BIT_A.sum(L, R) + BIT_X.sum(L, R) * X # print("test", BIT_A.sum(L, R), BIT_X.sum(L, R), X, L, R) for a in ans: print(a)