- Choose the aggregate stored for each interval or prefix.
- Build or initialize the structure from the input.
- Apply updates and combine the affected nodes to answer each query.
Code notes
- 63 lines of Python from the credited upstream file abc343_f.py.
- The implementation visibly relies on sequence storage, hash lookup, 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.
Use this to learn the idea, then write your own version.
12 3 45def main():6 import sys7 from collections import defaultdict8 9 from atcoder.segtree import SegTree10 11 input = sys.stdin.readline12 13 n, q = map(int, input().split())14 a = list(map(int, input().split()))15 inf = 10**1816 17 18 def op(x, y):19 20 x_1st, x_2nd = (x[0], x[1]), (x[2], x[3])21 y_1st, y_2nd = (y[0], y[1]), (y[2], y[3])22 23 24 value_count = defaultdict(int)25 26 for value, count in [x_1st, x_2nd, y_1st, y_2nd]:27 if value in value_count:28 value_count[value] += count29 else:30 value_count[value] = count31 32 33 value_count = sorted(value_count.items(), reverse=True)34 35 first_value, first_count = value_count[0]36 second_value, second_count = (37 value_count[1] if len(value_count) > 1 else (-inf, 0)38 )39 40 return first_value, first_count, second_value, second_count41 42 e = (-inf, 0, -inf + 1, 0)43 seg = SegTree(op, e, [(ai, 1, -inf, 0) for ai in a])44 45 for _ in range(q):46 qi, *args = map(int, input().split())47 48 if qi == 1:49 p, x = args50 p -= 151 52 seg.set(p, (x, 1, -inf, 0))53 else:54 l, r = args55 l -= 156 57 _, _, _, second_count = seg.prod(l, r)58 print(second_count)59 60 61if __name__ == "__main__":62 main()63