- 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
- 82 lines of Python from the credited upstream file abc344_e.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.
12 3from collections import defaultdict4from itertools import pairwise5from typing import List6 7HEAD, TAIL = 0, -18 9 10class DoublyLinkedList:11 12 def __init__(self, array: List) -> None:13 self.next = defaultdict(int)14 self.prev = defaultdict(int)15 16 for first, second in pairwise([HEAD] + array + [TAIL]):17 self.next[first] = second18 self.prev[second] = first19 20 def insert(self, value_left: int, value_mid: int) -> None:21 assert value_left in self.next.keys() and value_left in self.prev.keys()22 assert not value_mid in self.next.keys() and not value_mid in self.prev.keys()23 24 value_right = self.next[value_left]25 26 self.next[value_left] = value_mid27 self.next[value_mid] = value_right28 29 self.prev[value_right] = value_mid30 self.prev[value_mid] = value_left31 32 def remove(self, value_mid: int) -> None:33 assert value_mid in self.next.keys() and value_mid in self.prev.keys()34 35 value_left, value_right = self.prev[value_mid], self.next[value_mid]36 37 self.next[value_left] = value_right38 self.prev[value_right] = value_left39 40 del self.next[value_mid]41 del self.prev[value_mid]42 43 def fetch_all_values(self) -> List:44 results = list()45 pos = HEAD46 47 while self.next[pos] != TAIL:48 results.append(self.next[pos])49 pos = self.next[pos]50 51 return results52 53 54def main():55 import sys56 57 input = sys.stdin.readline58 59 n = int(input())60 a = list(map(int, input().split()))61 d = DoublyLinkedList(array=a)62 63 q = int(input())64 65 for _ in range(q):66 qi = list(map(int, input().split()))67 68 if qi[0] == 1:69 _, x, y = qi70 d.insert(x, y)71 else:72 _, x = qi73 d.remove(x)74 75 ans = d.fetch_all_values()76 77 print(*ans)78 79 80if __name__ == "__main__":81 main()82