- Choose the aggregate stored for each interval or prefix.
- Build or initialize the structure from the input.
- Apply updates and combine the affected nodes to answer each query.
Code notes
- 46 lines of Python from the credited upstream file maximum-area-rectangle-with-point-constraints-ii.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Count the build once, then multiply the logarithmic update or query path by the number of operations.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 45class Solution(object):6 def maxRectangleArea(self, xCoord, yCoord):7 """8 :type xCoord: List[int]9 :type yCoord: List[int]10 :rtype: int11 """12 class BIT(object): 13 def __init__(self, n):14 self.__bit = [0]*(n+1) 15 16 def add(self, i, val):17 i += 1 18 while i < len(self.__bit):19 self.__bit[i] += val20 i += (i & -i)21 22 def query(self, i):23 i += 1 24 ret = 025 while i > 0:26 ret += self.__bit[i]27 i -= (i & -i)28 return ret29 30 points = sorted((xCoord[i], yCoord[i]) for i in xrange(len(xCoord)))31 y_to_idx = {y:idx for idx, y in enumerate(sorted(set(yCoord)))}32 bit = BIT(len(y_to_idx))33 lookup = {}34 result = -135 for i, (x, y) in enumerate(points):36 y_idx = y_to_idx[y]37 bit.add(y_idx, +1)38 if not (i-1 >= 0 and points[i-1][0] == x):39 continue40 prev_y_idx = y_to_idx[points[i-1][1]]41 curr = bit.query(y_idx)-bit.query(prev_y_idx-1)42 if (prev_y_idx, y_idx) in lookup and lookup[prev_y_idx, y_idx][0] == curr-2:43 result = max(result, (x-lookup[prev_y_idx, y_idx][1])*(y-points[i-1][1]))44 lookup[prev_y_idx, y_idx] = (curr, x)45 return result46