51Nod-2488 矩形并的面积 题解
题目概述
给两个矩形,每条边分别和坐标轴平行,求两个矩形覆盖的总面积
解题思路
用左下和右上两个点来表示矩形
如果两个矩形分别为和,相交部分的矩形表示为
则
如果且,说明两矩形确实有相交部分
求两个矩形面积之和然后减去相交的小矩形的面积即可
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long llong;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
int n, m;
llong a, b, c, d;
llong e, f, g, h;
int main() {
#ifdef LOCAL
freopen("D:/Code/ACM/in.in", "r", stdin);
freopen("D:/Code/ACM/out.out", "w", stdout);
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> a >> b >> c >> d;
cin >> e >> f >> g >> h;
llong ans = 0;
ans += (c - a) * (d - b);
ans += (g - e) * (h - f);
llong x1 = max(a, e);
llong y1 = max(b, f);
llong x2 = min(c, g);
llong y2 = min(d, h);
if (x1 < x2 && y1 < y2) {
ans -= (x2 - x1) * (y2 - y1);
}
cout << ans << "\n";
return 0;
}