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";
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章