面积重叠问题

5426: Blocked Billboard

时间限制: 1 Sec   内存限制: 128 MB
提交: 23   解决: 18
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

During long milking sessions, Bessie the cow likes to stare out the window of her barn at two huge rectangular billboards across the street advertising "Farmer Alex's Amazingly Appetizing Alfalfa" and "Farmer Greg's Great Grain". Pictures of these two cow feed products on the billboards look much tastier to Bessie than the grass from her farm.
One day, as Bessie is staring out the window, she is alarmed to see a huge rectangular truck parking across the street. The side of the truck has an advertisement for "Farmer Smith's Superb Steaks", which Bessie doesn't quite understand, but she is mostly concerned about the truck potentially blocking the view of her two favorite billboards.

Given the locations of the two billboards and the location of the truck, please calculate the total combined area of both billboards that is still visible. It is possible that the truck obscures neither, both, or only one of the billboards.

输入

The first line of input contains four space-separated integers: x1 y1 x2 y2, where (x1,y1) and (x2,y2) are the coordinates of the lower-left and upper-right corners of the first billboard in Bessie's 2D field of view. The next line contains four more integers, similarly specifying the lower-left and upper-right corners of the second billboard. The third and final line of input contains four integers specifying the lower-left and upper-right corners of the truck. All coordinates are in the range -1000 to +1000. The two billboards are guaranteed not to have any positive area of overlap between themselves.

输出

Please output the total combined area of both billboards that remains visible.

样例输入

1 2 3 5
6 0 10 4
2 1 8 3

样例输出

17

提示

Here, 5 units of area from the first billboard and 12 units of area from the second billboard remain visible.

题意:

两个广告板,现在给你第三个广告板,这个新的广告板可能会覆盖这两个广告板,或者不覆盖,或者覆盖一个。

思路

两个广告板的面积减去重叠部分。重叠部分面积=(max(第一个板的左下角横坐标,第三个板的左下角横坐标)-min(第一个板的右上角横坐标,第三个板子右上角横坐标))*(max(第一个板的左下角纵坐标,第三个板的左下角纵坐标)-min(第一个板的右上角纵坐标,第三个板子右上角纵坐标))

如果两项有一项相减是负的按0处理。


水码如下:

#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<iostream>
#include<map>
#define mes(a,b) memset(a,b,sizeof(a))
#define rep(i,m,n) for(i=m;i<=n;i++)

typedef long long ll;
using namespace std;
int max3(int a,int b,int c){return max(max(a,b),c);}
ll min3(ll a,ll b,ll c){return min(min(a,b),c);}
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
//ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

struct p{
	int x1,y1,x2,y2;
}p[4];
int i;
int f(int x,int y){
	int x1=max(p[x].x1,p[y].x1);
	int x2=min(p[x].x2,p[y].x2);
	int y1=max(p[x].y1,p[y].y1);
	int y2=min(p[x].y2,p[y].y2);
	return max(0,x2-x1)*max(0,y2-y1);
}
int main(){
	rep(i,1,3)
		cin>>p[i].x1>>p[i].y1>>p[i].x2>>p[i].y2;
	int ans=0;
	rep(i,1,3)
		ans+=f(i,i)-f(i,3);
	cout<<ans<<endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值