Block Placement Queries Problem
Description
LeetCode Problem 3161.
There exists an infinite number line, with its origin at 0 and extending towards the positive x-axis.
You are given a 2D array queries, which contains two types of queries:
- For a query of type 1,
queries[i] = [1, x]. Build an obstacle at distancexfrom the origin. It is guaranteed that there is no obstacle at distancexwhen the query is asked. - For a query of type 2,
queries[i] = [2, x, sz]. Check if it is possible to place a block of sizeszanywhere in the range[0, x]on the line, such that the block entirely lies in the range[0, x]. A block cannot be placed if it intersects with any obstacle, but it may touch it. Note that you do not actually place the block. Queries are separate.
Return a boolean array results, where results[i] is true if you can place the block specified in the i^th query of type 2, and false otherwise.
Example 1:
Input: queries = [[1,2],[2,3,3],[2,3,1],[2,2,2]]
Output: [false,true,true]
Explanation:

For query 0, place an obstacle at x = 2. A block of size at most 2 can be placed before x = 3.
Example 2:
Input: queries = [[1,7],[2,7,6],[1,2],[2,7,5],[2,7,6]]
Output: [true,true,false]
Explanation:

- Place an obstacle at x = 7 for query 0. A block of size at most 7 can be placed before x = 7.
- Place an obstacle at x = 2 for query 2. Now, a block of size at most 5 can be placed before x = 7, and a block of size at most 2 before x = 2.
Constraints:
1 <= queries.length <= 15 * 10^42 <= queries[i].length <= 31 <= queries[i][0] <= 21 <= x, sz <= min(5 * 10^4, 3 * queries.length)- The input is generated such that for queries of type 1, no obstacle exists at distance
xwhen the query is asked. - The input is generated such that there is at least one query of type 2.
Solution
Approach: Reverse Processing + Fenwick Tree
Obstacles are only ever added, so we process the queries offline from last to first, which turns "add an obstacle" into "remove an obstacle". Removing an obstacle can only merge two gaps into a larger one, and a Fenwick tree can keep the maximum gap in a prefix.
Start with every obstacle that is ever added, plus sentinels 0 and m + 1 where m is the largest coordinate, kept in sorted order. For each pair of neighboring obstacles a < b, store the gap b - a at position b of the Fenwick tree (as a prefix maximum). Then scan the queries backwards:
- Type 2
[2, x, sz]: find the last obstaclepre <= x. A block fits if some gap entirely inside[0, pre]has length at leastsz, or if the free stretch(pre, x]has lengthx - pre >= sz. - Type 1
[1, x]: removexfrom the sorted obstacles; its neighborsprevandnextnow form one gap, so update positionnextwithnext - prev.
Complexity: O(q log m) time and O(m) extra space, where q is the number of queries and m is the largest coordinate. (The Python and Go versions delete from a sorted array, which is fast enough here.)
from bisect import bisect_left, bisect_right
class BinaryIndexedTree:
def __init__(self, n: int):
self.n = n
self.c = [0] * (n + 1)
def update(self, x: int, v: int):
while x <= self.n:
self.c[x] = max(self.c[x], v)
x += x & -x
def query(self, x: int) -> int:
mx = 0
while x:
mx = max(mx, self.c[x])
x -= x & -x
return mx
class Solution:
def getResults(self, queries: List[List[int]]) -> List[bool]:
m = max(q[1] for q in queries)
a = sorted([0, m + 1] + [q[1] for q in queries if q[0] == 1])
tree = BinaryIndexedTree(m + 1)
for i in range(1, len(a)):
tree.update(a[i], a[i] - a[i - 1])
ans = []
for q in reversed(queries):
x = q[1]
if q[0] == 1:
i = bisect_left(a, x)
tree.update(a[i + 1], a[i + 1] - a[i - 1])
a.pop(i)
else:
i = bisect_right(a, x)
pre = a[i - 1]
ans.append(tree.query(pre) >= q[2] or x - pre >= q[2])
return ans[::-1]