SRM546 Div2 Medium(500) TwoRectangles

TwoRectangles

X軸とY軸で分けるとわかりやすい。

#include <string>
#include <vector>
using namespace std;

int intersect( int a1, int a2, int b1, int b2 )
{
    if ( a2<b1 || b2<a1 )
        return 0;
    if ( a2==b1 || b2==a1 )
        return 1;
    return 2;
}

class TwoRectangles{public:
string describeIntersection( vector <int> A, vector <int> B )
{
    int x = intersect(A[0],A[2],B[0],B[2]);
    int y = intersect(A[1],A[3],B[1],B[3]);

    if ( x==2 && y==2 )
        return "rectangle";
    if ( x==2 && y==1 ||
         x==1 && y==2 )
        return "segment";
    if ( x==1 && y==1 )
        return "point";
    return "none";
}};