Problem solution · Python

ABC355 D — Intersecting Intervals

ABC355 D — Intersecting Intervals: 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
88 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 ABC355 D — Intersecting Intervals, 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

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

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 codeABC355 D — Intersecting Intervals · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  class SegmentTree:    def __init__(self, size):        self.size = 2 ** ((size - 1).bit_length())        self.tree = [0] * (2 * self.size)     def add(self, i, x):        i += self.size        self.tree[i] += x         while i > 1:            self.tree[i >> 1] = self.tree[i] + self.tree[i ^ 1]            i >>= 1     def query(self, l, r):        l += self.size        r += self.size        s = 0         while l < r:            if l & 1:                s += self.tree[l]                l += 1            if r & 1:                r -= 1                s += self.tree[r]             l >>= 1            r >>= 1         return s  from bisect import bisect_rightfrom typing import List  def bisect_le(sorted_array: List[int], value: int):    """Find the largest element <= x and its index, or None if it doesn't exist."""     if sorted_array[0] <= value:        index: int = bisect_right(sorted_array, value) - 1         return index, sorted_array[index]     return None, None  def main():    import sys     input = sys.stdin.readline     n = int(input())    lr = [tuple(map(int, input().split())) for _ in range(n)]    lr = sorted(lr)    l = [li for li, _ in lr]    # 座標圧縮    compressed = {x: i for i, x in enumerate(sorted(set(l)))}    st = SegmentTree(len(compressed))     for i in range(n):        li, _ = lr[i]        st.add(compressed[li], 1)     ans = 0     for j in range(n - 1):        lj, rj = lr[j]        st.add(compressed[lj], -1)         # li <= rjとなる個数        _, value = bisect_le(l, rj)         if value is None:            continue         count = st.query(0, compressed[value] + 1)        ans += count     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 ↗