class matrix{
private:
static const int TOTOP = 0x8;
static const int TOBOTTOM = 0x4;
static const int TOLEFT = 0x2;
static const int TORIGHT = 1;
static const int TOMARK = 0x0f;
static const int INMARK = 0xf0;
static const int INTOP = 0x80;
static const int INBOTTOM = 0x40;
static const int INLEFT = 0x20;
static const int INRIGHT = 0x10;
static const int GRAPEWID = 0x100;
static const int GRAPELEN = 0x200;
int leftX;
int leftY;
int rightX;
int rightY;
public:
matrix(int lx, int ly, int rx, int ry) : leftX(lx), leftY(ly), rightX(rx), rightY(ry) {};
int toAnother(const matrix &m){
int ans = 0;
if(leftY >= m.rightY)
ans |= TOTOP;
if(rightY <= m.leftY)
ans |= TOBOTTOM;
if(rightX <= m.leftX)
ans |= TOLEFT;
if(leftX >= m.rightX)
ans |= TORIGHT;
if(rightY >= m.rightY && leftY <= m.leftY){
ans |= GRAPEWID;
}else{
if(leftY < m.rightY && leftY > m.leftY)
ans |= INTOP;
if(rightY < m.rightY && rightY > m.leftY)
ans |= INBOTTOM;
}
if(rightX >= m.rightX && leftX <= m.leftX){
ans |= GRAPELEN;
}else{
if(rightX < m.rightX && rightX > m.leftX)
ans |= INLEFT;
if(leftX < m.rightX && leftX > m.leftX)
ans |= INRIGHT;
}
return ans;
}
int commonArea(const matrix &m){
int loc = toAnother(m);
if(loc & TOMARK)
return 0;
int len = 0, wid = 0;
if(loc & GRAPEWID)
wid = m.rightY - m.leftY;
else if( loc & INTOP && loc & INBOTTOM)
wid = rightY - leftY;
else if(loc & INTOP)
wid = m.rightY - leftY;
else
wid = rightY - m.leftY;
if(loc & GRAPELEN)
len = m.rightX - m.leftX;
else if(loc & INLEFT && loc & INRIGHT)
len = rightX - leftX;
else if(loc & INLEFT)
len = rightX - m.leftX;
else
len = m.rightX - leftX;
return len * wid;
}
int Area(){
return (rightX - leftX) * (rightY - leftY);
}
};
class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
matrix m1(A,B,C,D);
matrix m2(E,F,G,H);
return m1.Area() + m2.Area() - m1.commonArea(m2);
}
};
223. Rectangle Area
最新推荐文章于 2024-07-13 22:25:18 发布