Problem solution · Python

ABC153 F — Silver Fox vs Monster

ABC153 F — Silver Fox vs Monster: a Python solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Segment tree or range structure
Source
KATO-Hiro AtCoder Solutions
Length
77 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Segment tree or range structure

For ABC153 F — Silver Fox vs Monster, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 77 lines of Python from the credited upstream file abc153_f.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.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC153 F — Silver Fox vs Monster · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  class BIT:    """Binary Indexed Tree (Fenwick Tree)    Args:        size: List size (greater than 0).    """     def __init__(self, size: int):        self.size = size        self._bit = [0 for _ in range(self.size + 1)]     def add(self, index: int, value: int) -> None:        """Adds value to list with index i.        Args:            index: The number of index (1-indexed).            value: A value        Landau notation: O(log n)        """         while index <= self.size:            self._bit[index] += value            index += index & -index     def sum(self, index) -> int:        """Calculate the sum of elements from 1 to index.        Args:            index: The number of index (1-indexed).        Returns:            The sum of elements from 1 to index.        Landau notation: O(log n)        """         summed = 0         while index > 0:            summed += self._bit[index]            index -= index & -index         return summed  def main():    from math import ceil    from bisect import bisect_right    import sys     input = sys.stdin.readline     n, d, a = map(int, input().split())    p = sorted([list(map(int, input().split())) for _ in range(n)])    xs = [xi for xi, _ in p]     bit = BIT(n + 1)    ans = 0     for index, (xi, hi) in enumerate(p, 1):        hi -= bit.sum(index)         if hi <= 0:            continue         bomb_count = ceil(hi / a)        ans += bomb_count        damage = a * bomb_count        bit.add(index, damage)        # pos = bisect_right(xs, min(xi + 2 * d, 10 ** 9 + 7))        pos = bisect_right(xs, xi + 2 * d)        bit.add(pos + 1, -damage)     print(ans)  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗