Problem solution · Python

ABC343 F — Second Largest Query

ABC343 F — Second Largest Query: a Python solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Segment tree or range structure
Source
KATO-Hiro AtCoder Solutions
Length
63 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Segment tree or range structure

For ABC343 F — Second Largest Query, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. 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.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC343 F — Second Largest Query · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  # PyPy3でもTLEdef main():    import sys    from collections import defaultdict     from atcoder.segtree import SegTree     input = sys.stdin.readline     n, q = map(int, input().split())    a = list(map(int, input().split()))    inf = 10**18     # (1st value, 1st count, 2nd value, 2nd count)    def op(x, y):        # value, countのペアに変換        x_1st, x_2nd = (x[0], x[1]), (x[2], x[3])        y_1st, y_2nd = (y[0], y[1]), (y[2], y[3])         # 同じvalueのときは、countを合算        value_count = defaultdict(int)         for value, count in [x_1st, x_2nd, y_1st, y_2nd]:            if value in value_count:                value_count[value] += count            else:                value_count[value] = count         # x, yにおける1st, 2ndのvalueとcountを取得        value_count = sorted(value_count.items(), reverse=True)         first_value, first_count = value_count[0]        second_value, second_count = (            value_count[1] if len(value_count) > 1 else (-inf, 0)        )         return first_value, first_count, second_value, second_count     e = (-inf, 0, -inf + 1, 0)    seg = SegTree(op, e, [(ai, 1, -inf, 0) for ai in a])     for _ in range(q):        qi, *args = map(int, input().split())         if qi == 1:            p, x = args            p -= 1             seg.set(p, (x, 1, -inf, 0))        else:            l, r = args            l -= 1             _, _, _, second_count = seg.prod(l, r)            print(second_count)  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗