- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 57 lines of Python from the credited upstream file abc371_d.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4from bisect import bisect_left, bisect_right5from typing import List6 7 8def bisect_lt(sorted_array: List[int], value: int):9 """Find the largest element < x and its index, or None if it doesn't exist."""10 11 if sorted_array[0] < value:12 index: int = bisect_left(sorted_array, value) - 113 14 return index, sorted_array[index]15 16 return None, None17 18 19def bisect_le(sorted_array: List[int], value: int):20 """Find the largest element <= x and its index, or None if it doesn't exist."""21 22 if sorted_array[0] <= value:23 index: int = bisect_right(sorted_array, value) - 124 25 return index, sorted_array[index]26 27 return None, None28 29 30def main():31 import sys32 from itertools import accumulate33 34 input = sys.stdin.readline35 36 n = int(input())37 x = list(map(int, input().split()))38 p = list(map(int, input().split()))39 40 inf = 10**1241 x = [-inf] + x + [inf]42 p = [0] + p + [0]43 acc_p = list(accumulate(p))44 45 q = int(input())46 47 for _ in range(q):48 li, ri = map(int, input().split())49 50 j, _ = bisect_le(x, ri) 51 i, _ = bisect_lt(x, li) 52 print(acc_p[j] - acc_p[i])53 54 55if __name__ == "__main__":56 main()57