0223.cpp (574B)
1 class Solution { 2 int calc_area(int x1, int y1, int x2, int y2) { return abs(x1 - x2) * abs(y1 - y2); } 3 4 public: 5 int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { 6 int area = calc_area(ax1, ay1, ax2, ay2) + calc_area(bx1, by1, bx2, by2); 7 int x1, x2, y1, y2; 8 x1 = max(ax1, bx1); 9 x2 = min(ax2, bx2); 10 y1 = max(ay1, by1); 11 y2 = min(ay2, by2); 12 if (x2 - x1 > 0 && y2 - y1 > 0) 13 return area - calc_area(x1, y1, x2, y2); 14 else 15 return area; 16 } 17 };