Problem solution · Python

ABC157 E — Simple String Queries

ABC157 E — Simple String Queries: 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
82 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 ABC157 E — Simple String Queries, 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

  • 82 lines of Python from the credited upstream file abc157_e.py.
  • The implementation visibly relies on sequence storage.
  • 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 codeABC157 E — Simple String Queries · 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():    import sys     input = sys.stdin.readline     n = int(input())    s = list(input().rstrip())    q = int(input())    bits = [BIT(n) for _ in range(26)]     for index, si in enumerate(s, 1):        c = ord(si) - ord("a")        bits[c].add(index, 1)     for i in range(q):        query = list(input().split())         if query[0] == "1":            iq = int(query[1])            cq = ord(query[2]) - ord("a")            si = ord(s[iq - 1]) - ord("a")             bits[si].add(iq, -1)            s[iq - 1] = query[2]            bits[cq].add(iq, 1)        else:            lq, rq = int(query[1]), int(query[2])            ans = 0             for bit in bits:                if bit.sum(rq) - bit.sum(lq - 1) > 0:                    ans += 1             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 ↗