Cod sursa(job #262802)

Utilizator savimSerban Andrei Stan savim Data 19 februarie 2009 17:45:31
Problema Reuniune Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.12 kb
#include <stdio.h>

struct dr {
   long x1;
   long y1;
   long x2;
   long y2;
} A, B, C, D;

long long sup, per;
long left, right, up, down, vid;

long long suprafata(dr A) {
     return 1LL * (A.x2 - A.x1) * (A.y2 - A.y1);
}

long long perimetru(dr A) {
     return 1LL * 2 * (A.x2 - A.x1 + A.y2 - A.y1);
}

void calc(dr X) {
     int ox , oy, stanga, dreapta, sus, jos;
     ox = oy = 0;
     if (left <= X.x1 && X.x1 <= right || left <= X.x2 && X.x2 <= right) ox = 1;
     if (down <= X.y1 && X.y1 <= up || down <= X.y2 && X.y2 <= up) oy = 1;
     
     if (ox && oy) {
        if (left <= X.x1 && X.x1 <= right) stanga = X.x1;
        else stanga = left;
        if (left <= X.x2 && X.x2 <= right) dreapta = X.x2;
        else dreapta = right;
        
        if (down <= X.y1 && X.y1 <= up) jos = X.y1;
        else jos = down;
        if (down <= X.y2 && X.y2 <= up) sus = X.y2;
        else sus = up;
        
        left = stanga; right = dreapta; up = sus; down = jos;
        vid++;
     }
}

void inter(dr A, dr B, dr C) {
     left = A.x1; right = A.x2; up = A.y2; down = A.y1;
     vid = 0;
     
     //intersectez A cu B
     calc(B);
     
     //intersectez A,B cu C
     calc(C);
     
     if (vid == 2) {
        D.x1 = left; D.y1 = down; D.x2 = right; D.y2 = up;
     }
     else D.x1 = D.y1 = D.x2 = D.y2 = 0;
}

int main() {

    freopen("reuniune.in", "r", stdin);
    freopen("reuniune.out", "w", stdout);
    
    scanf("%ld %ld %ld %ld", &A.x1, &A.y1, &A.x2, &A.y2);
    scanf("%ld %ld %ld %ld", &B.x1, &B.y1, &B.x2, &B.y2);    
    scanf("%ld %ld %ld %ld", &C.x1, &C.y1, &C.x2, &C.y2);        
    
    sup = suprafata(A) + suprafata(B) + suprafata(C);
    per = perimetru(A) + perimetru(B) + perimetru(C);
    
    inter(A, B, A);
    sup -= suprafata(D);
    per -= perimetru(D);

    inter(A, C, A);
    sup -= suprafata(D);
    per -= perimetru(D);

    inter(B, C, B);
    sup -= suprafata(D);
    per -= perimetru(D);

    inter(A, B, C);
    sup += suprafata(D);
    per += perimetru(D);

    printf("%lld %lld\n", sup, per);

    return 0;
}