Cod sursa(job #563145)

Utilizator chibicitiberiuChibici Tiberiu chibicitiberiu Data 24 martie 2011 15:58:41
Problema Reuniune Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.28 kb
#include <fstream>
#include <algorithm>

using namespace std;

class Point
{
public:
    long long X, Y;
    Point () { X = Y = 0; }
    Point (long long x, long long y) { X = x; Y = y; }

};

class Rectangle
{
public:
    long long Left, Right;
    long long Top, Bottom;

    Rectangle () { Left = Right = Top = Bottom = 0; }
    Rectangle (Point bottom_left, Point top_right)
    {
        Left = bottom_left.X;
        Top = top_right.Y;
        Right = top_right.X;
        Bottom = bottom_left.Y;
    }

    Rectangle (long long left, long long top, long long right, long long bottom)
    {
        Left = left; Top = top; Right = right; Bottom = bottom;
    }

    Rectangle operator& (Rectangle b)
    {
        long long top, left, right, bottom;
        bottom = max(Bottom, b.Bottom);
        top = min(Top, b.Top);
        left = max(Left, b.Left);
        right = min(Right, b.Right);

        if (bottom < top && left < right) return Rectangle(left,top,right,bottom);
        return Rectangle();
    }

    long long Area()
    {
        return (Right-Left)*(Top-Bottom);
    }

    long long Perimeter()
    {
        if (Right - Left == 0 || Top - Bottom == 0) return 0;
        return 2*(Right - Left + Top - Bottom);
    }

    Rectangle* operator= (const Rectangle* b)
    {
        Left = b->Left;
        Top = b->Top;
        Right = b->Right;
        Bottom = b->Bottom;

        return this;
    }
};


int main()
{
    ifstream in ("reuniune.in");
    ofstream out ("reuniune.out");

    Point Rect1BotL, Rect1TopR;
    Point Rect2BotL, Rect2TopR;
    Point Rect3BotL, Rect3TopR;

    in>>Rect1BotL.X>>Rect1BotL.Y>>Rect1TopR.X>>Rect1TopR.Y;
    in>>Rect2BotL.X>>Rect2BotL.Y>>Rect2TopR.X>>Rect2TopR.Y;
    in>>Rect3BotL.X>>Rect3BotL.Y>>Rect3TopR.X>>Rect3TopR.Y;

    Rectangle A(Rect1BotL, Rect1TopR),
              B(Rect2BotL, Rect2TopR),
              C(Rect3BotL, Rect3TopR);

    long long area = A.Area() +B.Area() +C.Area()
                       -(A&B).Area() -(A&C).Area() -(B&C).Area()
                       +(A&B&C).Area();

    long long perimeter = A.Perimeter() +B.Perimeter() +C.Perimeter()
                       -(A&B).Perimeter() -(A&C).Perimeter() -(B&C).Perimeter()
                       +(A&B&C).Perimeter();

    out<<area<<" "<<perimeter;

    in.close();
    out.close();
    return 0;
}