- 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
- 67 lines of Python from the credited upstream file maximum-area-rectangle-with-point-constraints-i.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, points):7 """8 :type points: List[List[int]]9 :rtype: int10 """11 class BIT(object): 12 def __init__(self, n):13 self.__bit = [0]*(n+1) 14 15 def add(self, i, val):16 i += 1 17 while i < len(self.__bit):18 self.__bit[i] += val19 i += (i & -i)20 21 def query(self, i):22 i += 1 23 ret = 024 while i > 0:25 ret += self.__bit[i]26 i -= (i & -i)27 return ret28 29 points.sort()30 y_to_idx = {y:idx for idx, y in enumerate(sorted(set(y for _, y in points)))}31 bit = BIT(len(y_to_idx))32 lookup = {}33 result = -134 for i, (x, y) in enumerate(points):35 y_idx = y_to_idx[y]36 bit.add(y_idx, +1)37 if not (i-1 >= 0 and points[i-1][0] == x):38 continue39 prev_y_idx = y_to_idx[points[i-1][1]]40 curr = bit.query(y_idx)-bit.query(prev_y_idx-1)41 if (prev_y_idx, y_idx) in lookup and lookup[prev_y_idx, y_idx][0] == curr-2:42 result = max(result, (x-lookup[prev_y_idx, y_idx][1])*(y-points[i-1][1]))43 lookup[prev_y_idx, y_idx] = (curr, x)44 return result45 46 47484950class Solution2(object):51 def maxRectangleArea(self, points):52 """53 :type points: List[List[int]]54 :rtype: int55 """56 result = -157 points.sort()58 for i in xrange(len(points)-3):59 if points[i][0] != points[i+1][0]:60 continue61 j = next((j for j in xrange(i+2, len(points)-1) if points[i][1] <= points[j][1] <= points[i+1][1]), len(points)-1)62 if j == len(points)-1 or not (points[j][0] == points[j+1][0] and points[i][1] == points[j][1] and points[i+1][1] == points[j+1][1]):63 continue64 result = max(result, (points[i+1][1]-points[i][1])*(points[j][0]-points[i][0]))65 return result66 67