結果
問題 |
No.3078 Difference Sum Query
|
ユーザー |
👑 |
提出日時 | 2025-02-24 19:47:22 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,092 ms / 2,000 ms |
コード長 | 2,166 bytes |
コンパイル時間 | 413 ms |
コンパイル使用メモリ | 82,916 KB |
実行使用メモリ | 162,532 KB |
最終ジャッジ日時 | 2025-02-24 22:55:01 |
合計ジャッジ時間 | 15,273 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 19 |
ソースコード
# Convert the following C++ submission into pypy3 # https://yukicoder.me/submissions/1045839 import sys from collections import defaultdict # ACL for python # https://github.com/shakayami/ACL-for-python/blob/master/fenwicktree.py class FenwickTree: def __init__(self, N): self.n = N self.data = [0] * N def add(self, p, x): assert 0 <= p < self.n, f"0 <= p < n, p={p}, n={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, f"0 <= l <= r <= n, l={l}, r={r}, n={self.n}" return self._sum0(r) - self._sum0(l) def _sum0(self, r): s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s def main(): # 入力高速化 input = sys.stdin.read data = input().split() index = 0 n, q = int(data[index]), int(data[index + 1]) index += 2 a = list(map(int, data[index:index + n])) index += n # イベントの準備 event = defaultdict(list) for i in range(n): event[a[i]].append((1, i)) queries = [] for i in range(q): l, r, x = map(int, data[index:index + 3]) index += 3 l -= 1 r -= 1 queries.append((l, r, x)) event[x].append((2, i)) # フェニック木の準備 fenwick1 = FenwickTree(n) fenwick2 = FenwickTree(n) # 初期値をセット for i in range(n): fenwick1.add(i, a[i]) fenwick2.add(i, 1) ans = [0] * q # クエリ処理 for _, vec in sorted(event.items()): for type_, i in vec: if type_ == 1: # 要素を削除 (負の値を加算) fenwick1.add(i, -2 * a[i]) fenwick2.add(i, -2) elif type_ == 2: # クエリを処理 l, r, x = queries[i] val = fenwick1.sum(l, r + 1) - fenwick2.sum(l, r + 1) * x ans[i] = val # 結果を一括出力 (高速化) sys.stdout.write("\n".join(map(str, ans)) + "\n") if __name__ == "__main__": main()