- Define the priority key and whether the smallest or largest item should lead.
- Push each candidate when it becomes eligible.
- Discard stale entries when necessary and process the best live candidate.
Code notes
- 126 lines of Python from the credited upstream file abc306_e.py.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup, work queue.
- No explicit loop blocks detected.
Complexity
Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3from collections import defaultdict4from heapq import heappop, heappush5 6 7class SumOfTopKth:8 """Sum of the k-th number from the smallest (largest) to the k-th.9 10 See:11 https:atcoder.jp/contests/abc306/submissions/4233937512 """13 14 __slots__ = (15 "_summed",16 "_k",17 "_in",18 "_out",19 "_d_in",20 "_d_out",21 "_freq",22 "_ascending_order",23 )24 25 def __init__(self, k: int, ascending_order=True) -> None:26 self._k = k27 self._summed = 028 self._in = []29 self._out = []30 self._d_in = []31 self._d_out = []32 self._ascending_order = ascending_order33 self._freq = defaultdict(int)34 35 def query(self) -> int:36 return self._summed if self._ascending_order else -self._summed37 38 def add(self, x: int) -> None:39 if not self._ascending_order:40 x = -x41 42 self._freq[x] += 143 heappush(self._in, -x)44 self._summed += x45 self._modify()46 47 def discard(self, x: int) -> None:48 if not self._ascending_order:49 x = -x50 if self._freq[x] == 0:51 return52 53 self._freq[x] -= 154 55 if self._in and -self._in[0] == x:56 self._summed -= x57 heappop(self._in)58 elif self._in and -self._in[0] > x:59 self._summed -= x60 heappush(self._d_in, -x)61 else:62 heappush(self._d_out, x)63 64 self._modify()65 66 def set_k(self, k: int) -> None:67 self._k = k68 self._modify()69 70 def get_k(self) -> int:71 return self._k72 73 def _modify(self) -> None:74 while self._out and (len(self._in) - len(self._d_in) < self._k):75 p = heappop(self._out)76 77 if self._d_out and p == self._d_out[0]:78 heappop(self._d_out)79 else:80 self._summed += p81 heappush(self._in, -p)82 83 while len(self._in) - len(self._d_in) > self._k:84 p = -heappop(self._in)85 86 if self._d_in and p == -self._d_in[0]:87 heappop(self._d_in)88 else:89 self._summed -= p90 heappush(self._out, p)91 92 while self._d_in and self._in[0] == self._d_in[0]:93 heappop(self._in)94 heappop(self._d_in)95 96 def __len__(self) -> int:97 return len(self._in) + len(self._out) - len(self._d_in) - len(self._d_out)98 99 def __contains__(self, x: int) -> bool:100 if not self._ascending_order:101 x = -x102 return self._freq[x] > 0103 104 105def main():106 import sys107 108 input = sys.stdin.readline109 110 n, k, q = map(int, input().split())111 a = [0] * n112 s = SumOfTopKth(k, ascending_order=False)113 114 for _ in range(q):115 xi, yi = map(int, input().split())116 xi -= 1117 118 s.discard(a[xi])119 s.add(yi)120 print(s.query())121 a[xi] = yi122 123 124if __name__ == "__main__":125 main()126