- 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
- 65 lines of Python from the credited upstream file abc430_c.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_left5 6 7def bisect_lt(sorted_array: list[int], value: int):8 """Find the largest element < x and its index, or None if it doesn't exist."""9 10 if sorted_array[0] < value:11 index: int = bisect_left(sorted_array, value) - 112 13 return index, sorted_array[index]14 15 return None, None16 17 18def bisect_ge(sorted_array: list[int], value: int):19 """Find the smallest element >= x and its index, or None if it doesn't exist."""20 21 if sorted_array[-1] >= value:22 index: int = bisect_left(sorted_array, value)23 24 return index, sorted_array[index]25 26 return None, None27 28 29def main():30 import sys31 from itertools import accumulate32 33 input = sys.stdin.readline34 35 n, a, b = map(int, input().split())36 s = input().rstrip()37 a_count, b_count = list(), list()38 39 for si in s:40 if si == "a":41 a_count.append(1)42 b_count.append(0)43 else:44 a_count.append(0)45 b_count.append(1)46 47 inf = 10**1848 a_count.append(inf)49 b_count.append(inf)50 51 acc_a = list(accumulate(a_count, initial=0))52 acc_b = list(accumulate(b_count, initial=0))53 ans = 054 55 for k in range(n + 1):56 i, _ = bisect_ge(acc_a, acc_a[k] + a)57 j, _ = bisect_lt(acc_b, acc_b[k] + b)58 ans += max(0, j - i + 1)59 60 print(ans)61 62 63if __name__ == "__main__":64 main()65