- Choose the aggregate stored for each interval or prefix.
- Build or initialize the structure from the input.
- Apply updates and combine the affected nodes to answer each query.
Code notes
- 81 lines of Python from the credited upstream file abc442_d.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Count the build once, then multiply the logarithmic update or query path by the number of operations.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4from typing import Any5 6 7class BIT:8 """Binary Indexed Tree (Fenwick Tree)9 10 See:11 https:atcoder.jp/contests/tessoku-book/submissions/3491243412 """13 14 def __init__(self, size: int) -> None:15 self.size = size16 self.size0 = 1 << (size.bit_length() - 1)17 self.tree = [0] * (size + 1)18 19 def add(self, index: int, value: Any) -> None:20 assert 0 <= index < self.size21 22 index += 123 24 while index <= self.size:25 self.tree[index] += value26 index += index & -index27 28 def get(self, index: int) -> Any:29 return self.sum(index) - self.sum(index - 1)30 31 def range_sum(self, left: int, right: int) -> Any:32 assert 0 <= left <= right <= self.size33 34 return self.sum(right - 1) - self.sum(left - 1)35 36 def sum(self, index: int) -> Any:37 index += 138 summed = 039 40 assert 0 <= index <= self.size41 42 while index > 0:43 summed += self.tree[index]44 index -= index & -index45 46 return summed47 48 49def main():50 import sys51 52 input = sys.stdin.readline53 54 n, q = map(int, input().split())55 a = list(map(int, input().split()))56 size = 2 * 10**5 + 1057 bit = BIT(size)58 59 for i, ai in enumerate(a):60 bit.add(i, ai)61 62 for _ in range(q):63 query, *args = map(int, input().split())64 65 if query == 1:66 x = args[0] - 167 68 first, second = bit.get(x), bit.get(x + 1)69 bit.add(x, -first + second)70 bit.add(x + 1, -second + first)71 else:72 l, r = args73 l -= 174 75 ans = bit.range_sum(l, r)76 print(ans)77 78 79if __name__ == "__main__":80 main()81