Determine Available Range on Number Line
Heaps, Sorting & SearchAuto-graded · 21 test casesLast updated
Evaluate dynamic range-query operations on 1D coordinates to test whether continuous subsegments contain obstacles.
You are given a sequence of operations performed on a 1D grid of infinite length, where all integer positions start out unoccupied.
There are two types of operations:
[1, x]: Place an obstacle at positionx. If positionxalready contains an obstacle, this operation has no effect.[2, x, k]: Query whether a contiguous block of lengthkstarting atx(i.e., covering positionsx, x + 1, ..., x + k - 1) is completely free of obstacles. Append'1'to the result string if all positions in the interval are unoccupied, or'0'if at least one obstacle is present in that interval.
Execute all operations in the given order and return a single string containing the results of all type 2 query operations concatenated together.
Constraints
1 <= len(operations) <= 10^5- Each element in
operationsis either[1, x]or[2, x, k]. 1 <= x <= 10^91 <= k <= 10^9
Examples
Example 1
Input: process_operations([[1, 2], [1, 5], [2, 3, 2], [2, 3, 3], [2, 1, 1], [2, 1, 2]])
Output: '1010'
Example 2
Input: process_operations([[2, 10, 5], [1, 12], [1, 12], [2, 10, 2], [2, 10, 3], [2, 13, 100]])
Output: '1101'
Example 3
Input: process_operations([[1, 1]])
Output: ''
Hints
Hint 1
Since coordinate positions reach up to 10^9, you cannot maintain an array indexed directly by position.
Hint 2
Consider extracting all unique obstacle positions that appear in build operations to perform coordinate compression.
Hint 3
Alternatively, query whether the smallest obstacle position that is >= x falls inside the range [x, x + k - 1].
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires us to process two types of operations online: placing an obstacle at a given coordinate, and checking if an interval [x, x + k - 1] is completely clear of obstacles.
Because coordinates can be as large as $10^9$, using an array of size equal to the maximum coordinate is impossible. However, the number of operations $N$ is at most $10^5$. We can collect all positions $x$ where an obstacle placement occurs, sort them, and map them to compressed indices in $0 \dots M-1$ (where $M \le N$).
To execute operations efficiently:
- Coordinate Compression: Collect every candidate obstacle position from type 1 operations, sort and remove duplicates.
- Fenwick Tree / Binary Indexed Tree (BIT): Initialize a BIT over the $M$ compressed indices.
- For a type 1 operation
[1, x]: Binary searchxin the sorted array of candidate positions. If an obstacle has not been placed atxyet, mark it as placed and update the BIT with $+1$. - For a type 2 query
[2, x, k]: Use binary search to find the range of compressed indices corresponding to integer positions in $[x, x + k - 1]$. The left index islo = bisect_left(coords, x)and the right index ishi = bisect_right(coords, x + k - 1). Query the BIT to count how many obstacles currently exist in the compressed range[lo, hi). If the sum is0, record'1'; otherwise, record'0'.
- For a type 1 operation
Complexity
- Time Complexity:
- Coordinate extraction and sorting takes $\mathcal{O}(M \log M)$ time, where $M \le N$.
- Each build or query operation performs binary searches and BIT updates/queries, taking $\mathcal{O}(\log M)$ per operation.
- Overall time complexity is $\mathcal{O}(N \log N)$.
- Space Complexity: $\mathcal{O}(N)$ to store compressed coordinates, the BIT, and output characters.
Solution
from bisect import bisect_left, bisect_right
from typing import List
def process_operations(operations: List[List[int]]) -> str:
# Gather and sort all unique positions where obstacles could be placed
coords = sorted({op[1] for op in operations if op[0] == 1})
m = len(coords)
tree = [0] * (m + 1)
built = [False] * m
out = []
for op in operations:
if op[0] == 1:
x = op[1]
idx = bisect_left(coords, x)
if not built[idx]:
built[idx] = True
i = idx + 1
while i <= m:
tree[i] += 1
i += i & -i
else:
x, k = op[1], op[2]
right_bound = x + k - 1
lo = bisect_left(coords, x)
hi = bisect_right(coords, right_bound)
if lo == hi:
out.append("1")
continue
s = 0
i = hi
while i > 0:
s += tree[i]
i -= i & -i
i = lo
while i > 0:
s -= tree[i]
i -= i & -i
out.append("0" if s > 0 else "1")
return "".join(out)