# 223. Rectangular area

Give you two rectangles composed of straight lines on the two-dimensional plane. Please calculate and return the total area covered by the two rectangles.

Each rectangle is represented by its lower left vertex and upper right vertex coordinates:

The first rectangle is defined by its lower left vertex (ax1, ay1) and upper right vertex (ax2, ay2).

The second rectangle is defined by its lower left vertex (bx1, by1) and upper right vertex (bx2, by2).

Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2

Output: 45

Source: LeetCode

Link: https://leetcode-cn.com/problems/rectangle-area

## Problem solution

Subtract the coverage area from the sum of the two rectangular areas

The coverage area is calculated through the coordinate interval of the coverage

class Solution: def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int: s = (ax1-ax2)*(ay1-ay2)+(bx1-bx2)*(by1-by2) if ax2<=bx1 or ax1>=bx2 or ay1>=by2 or ay2<=by1: return s xs = [ax1,ax2,bx1,bx2] ys = [ay1,ay2,by1,by2] xs.sort() ys.sort() return s-((xs[1]-xs[2])*(ys[1]-ys[2]))

# 850. Rectangular area II

We give a (axis aligned) list of rectangles. For rectangle[i] = [x1, y1, x2, y2], where (x1, y1) is the coordinate of the lower left corner of rectangle I and (x2, y2) is the coordinate of the upper right corner of the rectangle.

Find the total area covered by all rectangles in the plane. Since the answer may be too large, please return its modulus of 10 ^ 9 + 7.

Example 1:

Input: [[0,0,2,2], [1,0,2,3], [1,0,3,1]]

Output: 6

Explanation: as shown in the figure

Source: LeetCode

Link: https://leetcode-cn.com/problems/rectangle-area-ii

## thinking

- Take all the x's and arrange them in order, corresponding to the coordinate axis
- Find the effective interval coverage of y-axis corresponding to each x, as shown in the debugging code below
- We pack the ordinates Y1 and Y2 of a rectangle into a list and put them into the [x1,x2) interval
- If the [x1,x2) interval has three coordinates x1, X3 and x4, use these three coordinates as keys to store the y interval they cover
- Then x1:[y1,y2],x3:[y1,y2],x4:[y1,y2]
- Calculate the interval covered by all rectangles
- The key corresponding intervals are fused to find the sum of the effective interval length

Three coordinates are keys to store the y interval they cover

## Problem solving - coordinate projection - line segment coverage - effective interval calculation

class Solution: def rectangleArea(self, rectangles) -> int: ans = 0 ''' Mapped rectangular x Coordinate to axis ''' rectangles.sort() xs=[] for x1, y1, x2, y2 in rectangles: xs.append(x1) xs.append(x2) xs = list(set(xs)) xs.sort() ''' look for x Corresponding y section ''' cy = {} for x1, y1, x2, y2 in rectangles: for i in range(len(xs)-1): if i not in cy: cy[i] = [] if x1<=xs[i]: if x2>xs[i]: cy[i].append([y1,y2]) else: break ''' Find the effective interval length ''' def mergeqj(xs): if len(xs) == 0:return 0 ys = 0 xs.sort() l,r = xs[0] for ll,rr in xs[1:]: if ll > r: ys+=r-l l = ll r = rr else: r = max(r,rr) ys += r-l return ys ''' Find area ''' for i in range(0,len(xs)-1): y = mergeqj(cy[i]) ans += (xs[i+1]-xs[i])*y return int(ans%(10**9+7))

# pow index calculation fails in a wide range

from math import pow

int(ans%(pow(10,9)+7)) and int(ans%(10**9+7)) get different answers, and the former answer is wrong

output

862275785

Expected results

862275791

In a small range, pow is efficient

On a large scale, pow is wrong