- Choose the invariant that makes a window valid or useful.
- Advance the right boundary and add the new element.
- Move the left boundary only as needed while maintaining the invariant and updating the answer.
Code notes
- 66 lines of Python from the credited upstream file abc435_e.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3from bisect import bisect_left4 5 6def bisect_ge(sorted_array: list[int], value: int):7 """Find the smallest element >= x and its index, or None if it doesn't exist."""8 9 if sorted_array[-1] >= value:10 index: int = bisect_left(sorted_array, value)11 12 return index, sorted_array[index]13 14 return None, None15 16 17def main():18 import sys19 from itertools import pairwise20 from atcoder.lazysegtree import LazySegTree 21 22 input = sys.stdin.readline23 24 n, q = map(int, input().split())25 lr = [tuple(map(int, input().split())) for _ in range(q)]26 compressed = set([0, n])27 28 for li, ri in lr:29 compressed.add(li - 1)30 compressed.add(ri)31 32 compressed = sorted(compressed)33 34 counts = [(second - first, 1) for first, second in pairwise(compressed)]35 36 def op(a, b):37 return (a[0] + b[0], a[1] + b[1])38 39 e = (0, 0)40 41 pending = -142 43 def mapping(f, a):44 return a if f == pending else (f * a[1], a[1])45 46 def composition(f, g):47 return g if f == pending else f48 49 id = pending50 51 st = LazySegTree(op, e, mapping, composition, id, counts)52 53 for li, ri in lr:54 li -= 155 56 left, _ = bisect_ge(compressed, li)57 right, _ = bisect_ge(compressed, ri)58 st.apply(left, right, 0)59 60 ans = st.all_prod()[0]61 print(ans)62 63 64if __name__ == "__main__":65 main()66