- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 57 lines of Python from the credited upstream file 3161.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1from sortedcontainers import SortedList2 3 4class FenwickTree:5 def __init__(self, n: int):6 self.vals = [0] * (n + 1)7 8 def maximize(self, i: int, val: int) -> None:9 while i < len(self.vals):10 self.vals[i] = max(self.vals[i], val)11 i += FenwickTree.lowtree(i)12 13 def get(self, i: int) -> int:14 res = 015 while i > 0:16 res = max(res, self.vals[i])17 i -= FenwickTree.lowtree(i)18 return res19 20 @staticmethod21 def lowtree(i: int) -> int:22 return i & -i23 24 25class Solution:26 def getResults(self, queries: list[list[int]]) -> list[bool]:27 n = min(50000, len(queries) * 3)28 ans = []29 tree = FenwickTree(n + 1)30 obstacles = SortedList([0, n]) 31 32 for query in queries:33 type = query[0]34 if type == 1:35 x = query[1]36 obstacles.add(x)37 38 for x1, x2 in itertools.pairwise(obstacles):39 tree.maximize(x2, x2 - x1)40 41 for query in reversed(queries):42 type = query[0]43 x = query[1]44 if type == 1:45 i = obstacles.index(x)46 next = obstacles[i + 1]47 prev = obstacles[i - 1]48 obstacles.remove(x)49 tree.maximize(next, next - prev)50 else:51 sz = query[2]52 i = obstacles.bisect_right(x)53 prev = obstacles[i - 1]54 ans.append(tree.get(prev) >= sz or x - prev >= sz)55 56 return ans[::-1]57