- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 56 lines of Python from the credited upstream file abc449_c.py.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
- No explicit loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1from bisect import bisect_right2from typing import List3 4 5def bisect_le(sorted_array: List[int], value: int):6 """Find the largest element <= x and its index, or None if it doesn't exist."""7 8 if sorted_array[0] <= value:9 index: int = bisect_right(sorted_array, value) - 110 11 return index, sorted_array[index]12 13 return None, None14 15 16def main():17 import sys18 from collections import defaultdict19 from string import ascii_lowercase20 21 input = sys.stdin.readline22 23 n, l, r = map(int, input().split())24 l -= 125 s = input().rstrip()26 inf = 10**927 d = defaultdict(list)28 29 for i, si in enumerate(s):30 if si not in d:31 d[si] = [-inf]32 33 d[si].append(i)34 35 for alpha in ascii_lowercase:36 if alpha in d:37 d[alpha].append(inf)38 39 ans = 040 41 for values in d.values():42 for value in values:43 if value == -inf or value == inf:44 continue45 46 j, _ = bisect_le(values, value + r)47 i, _ = bisect_le(values, value + l)48 49 ans += j - i50 51 print(ans)52 53 54if __name__ == "__main__":55 main()56