Problem solution · Python

ABC221 E — LEQ

ABC221 E — LEQ: 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
107 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 ABC221 E — LEQ, 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

  • 107 lines of Python from the credited upstream file abc221_e.py.
  • The implementation visibly relies on sequence storage, hash lookup, 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 codeABC221 E — LEQ · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*- import typing  class FenwickTree:    """Reference: https://en.wikipedia.org/wiki/Fenwick_tree"""     def __init__(self, n: int = 0, mod=None) -> None:        self._n = n        self.data = [0] * n        self.mod = mod     def add(self, p: int, x: typing.Any) -> None:        assert 0 <= p < self._n         p += 1         while p <= self._n:            self.data[p - 1] += x             if self.mod is not None:                self.data[p - 1] %= self.mod             p += p & -p     def sum(self, left: int, right: int) -> typing.Any:        """[left, right)"""        assert 0 <= left <= right <= self._n         return self._sum(right) - self._sum(left)     def _sum(self, r: int) -> typing.Any:        s = 0         while r > 0:            s += self.data[r - 1]             if self.mod is not None:                s %= self.mod             r -= r & -r         return s  def compress_coordinate(elements: list) -> dict:    """Means that reduce the numerical value while maintaining the magnitude        relationship.     Args:        elements: list of integer numbers (greater than -1).     Returns:        A dictionary's items ((original number, compressed number) pairs).     Landau notation: O(n log n)    """     # See:    # https://atcoder.jp/contests/abc036/submissions/5707999?lang=ja    compressed_list = sorted(set(elements))    return {element: index for index, element in enumerate(compressed_list)}  def main():    import sys     input = sys.stdin.readline     n = int(input())    a = list(map(int, input().split()))     # Aiの左右の端部l, rを固定すると[l + 1, r)は自由に決められるので、2 ** (r - l - 1)通り     # O(n ** 2)の高速化:    # ・変数を分離すると、2 ** (r - 1) * ((1 / 2) ** l)    # ・rを固定して、a[l] <= a[r]となる((1 / 2) ** l)を答えに加算    # ・点加算と区間和を高速に求められるFenwick Treeを使用    # ・各Aiに対して、((1 / 2) ** l)を加算    # ・Aiが大きくなる可能性がある + 値そのもの以上に大小関係が重要なので、座標圧縮    c = compress_coordinate(a)    size = len(c.keys())    mod = 998244353    ft = FenwickTree(size + 1, mod)    inv = pow(2, mod - 2, mod)  # 1 / p    two, inv_two = 1, 1    ans = 0     for ai in a:        d = c[ai]        ans += two * ft.sum(0, d + 1)  # [l, r)        ans %= mod         two *= 2        two %= mod        inv_two *= inv        inv_two %= mod         ft.add(d, inv_two)     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 ↗