Codeforces1216C White Sheet【矩形面积交】【计算几何】
题意:
给你三个矩形的左下角和右上角的下标
请问第二个矩形和第三个矩形是否能完全覆盖第一个矩形,能输出"NO",否则输出"YES"
思路:
计算几何模板题
通过面积之间的关系可以得出第二个矩形和第三个矩形一共覆盖了第一个矩形多少面积
进行判断
面积 = 第一个矩形与第二个矩形的交 + 第一个矩形与第三个矩形的交 + 三个矩形的交
我的代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
void check(ll &a) {
if(a < 0) a = 0;
}
int main() {
ll x1, x2,x3, x4,x5, x6;
ll y1,y2,y3, y4,y5, y6;
cin>>x1>>y1>>x2>>y2;
cin>>x3>>y3>>x4>>y4;
cin>>x5>>y5>>x6>>y6;
ll ans1 = (min(min(x4, x6), x2) - max(max(x3, x5),x1)) * (min(min(y4, y6),y2) - max(max(y3, y5),y1));
ll ans2 = (min(x2, x6) - max(x1, x5)) * (min(y2, y6) - max(y1, y5));
ll ans3 = (min(x4, x2) - max(x3, x1)) * (min(y4, y2) - max(y3, y1));
ll ans = (x2 - x1) * (y2 - y1);
check(ans1);
check(ans2);
check(ans3);
if(ans == ans2 + ans3 - ans1){
puts("NO");
}else{
puts("YES");
}
}
dalao的代码:
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
struct Rect {
ll x1, y1, x2, y2;
ll area() const {
if (x2 < x1 || y2 < y1) return 0;
else return -
}
};
Rect its(Rect a, Rect b) {
Rect res;
res.x1 = max(a.x1, b.x1);
res.x2 = min(a.x2, b.x2);
res.y1 = max(a.y1, b.y1);
res.y2 = min(a.y2, b.y2);
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
Rect w, b1, b2;
cin >> w.x1 >> w.y1 >> w.x2 >> w.y2;
cin >> b1.x1 >> b1.y1 >> b1.x2 >> b1.y2;
cin >> b2.x1 >> b2.y1 >> b2.x2 >> b2.y2;
ll res = w.area() - its(w, b1).area() - its(w, b2).area() + its(w, its(b1, b2)).area();
if (res > 0) cout << "YES\n";
else cout << "NO\n";
}