https://www.luogu.com.cn/problem/P1884
我想先将各个矩形面积加起来 然后减去他们重合的部分,怎么实现?
#include <iostream>
using namespace std;
typedef long long ll;
struct area{
int x1,y1,x2,y2;
}a[1010];
int n,cnt;
ll ans;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin>>n;
for(int i=1; i<=n; i++){
cin>>a[i].x1>>a[i].y1>>a[i].x2>>a[i].y2;
ans+=(a[i].x2-a[i].x1)*(a[i].y2-a[i].y1);
for(int j=1; j<i; j++){
if((a[j].x1>=a[i].x1 and a[j].x1<=a[i].x2)
or (a[j].x2>=a[i].x1 and a[j].x1<=a[i].x2)
or (a[j].y1>=a[i].y1 and a[j].y1<=a[i].y2)
or (a[j].y2>=a[i].y1 and a[j].y2<=a[i].y2)){
int x1=max(a[i].x1,a[j].x1);
int y1=max(a[i].y1,a[j].y1);
int x2=min(a[i].x2,a[j].x2);
int y2=min(a[i].y2,a[j].y2);
ll Area = (x2-x1)*(y2-y1);
ans-=Area;
}
}
}
cout<<ans;
return 0;
}