Cod sursa(job #75487)

Utilizator MarcvsHdrMihai Leonte MarcvsHdr Data 2 august 2007 13:14:30
Problema Reuniune Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.78 kb
# include<stdio.h>

long long int p1,p2,s,s2;
typedef struct MUCHIE {long long int x,y1,y2,type;};
typedef struct DREPTUNGHI {long long int x1,y1,x2,y2;};
DREPTUNGHI drept[4];
MUCHIE muc[50];
long int lenmuc;
typedef struct SEGMENT {long long int a,b,count;};
SEGMENT segment[100];
typedef struct INTERVAL {long long int a,b;};

void citire()
{
FILE *f=fopen("reuniune.in","r");
int i;
for (i=1;i<=3;i++)
fscanf(f,"%lld%ld%lld%lld",&drept[i].x1,&drept[i].y1,&drept[i].x2,&drept[i].y2);
fclose(f);
}

void sort()
{
long int l=lenmuc,i,j;MUCHIE aux;
for (i=1;i<=l-1;i++)
	for (j=i+1;j<=l;j++)
		if (muc[i].x>muc[j].x||(muc[i].x==muc[j].x&&muc[i].type<muc[j].type))
			{aux=muc[i];muc[i]=muc[j];muc[j]=aux;}
}

void pune_muchiile(long int &l)
{
muc[1].x=drept[1].x1;muc[1].y1=drept[1].y1;muc[1].y2=drept[1].y2;
muc[2].x=drept[2].x1;muc[2].y1=drept[2].y1;muc[2].y2=drept[2].y2;
muc[3].x=drept[3].x1;muc[3].y1=drept[3].y1;muc[3].y2=drept[3].y2;
l=3;
sort();
}

void pune_muchiile_2(long int &l)
{
muc[1].x=drept[1].y2;muc[1].y1=drept[1].x1;muc[1].y2=drept[1].x2;
muc[2].x=drept[2].y2;muc[2].y1=drept[2].x1;muc[2].y2=drept[2].x2;
muc[3].x=drept[3].y2;muc[3].y1=drept[3].x1;muc[3].y2=drept[3].x2;
l=3;
sort();
}

void intersecteaza(INTERVAL a, INTERVAL b, INTERVAL &c)
{
if (a.a>b.a) c.a=a.a; else c.a=b.a;
if (a.b<b.b) c.b=a.b; else c.b=b.b;
if (c.a>=c.b) c.a=c.b=0;
}

long long int length(INTERVAL a) {return a.b-a.a;}
long long int aria(DREPTUNGHI a) {return (a.x2-a.x1)*(a.y2-a.y1);}

void baleiaza(long long int &p)
{
INTERVAL a,b,c,iab,iac,ibc,iabc;
a.a=muc[1].y1;a.b=muc[1].y2;
b.a=muc[2].y1;b.b=muc[2].y2;
c.a=muc[3].y1;c.b=muc[3].y2;
intersecteaza(a,b,iab);
intersecteaza(a,c,iac);
intersecteaza(b,c,ibc);
intersecteaza(iab,c,iabc);
p=length(a)+length(b)-length(iab)+length(c)-length(iac)-length(ibc)+length(iabc);
p*=2;
}

long long int max(long long int a, long long int b) {if (a>b) return a; return b;}
long long int min(long long int a, long long int b) {if (a>b) return b; return a;}

void intersecteaza_d(DREPTUNGHI a, DREPTUNGHI b, DREPTUNGHI &c)
{
c.x1=max(a.x1,b.x1);
c.y1=max(a.y1,b.y1);
c.x2=min(a.x2,b.x2);
c.y2=min(a.y2,b.y2);
if (c.x1>=c.x2||c.y1>=c.y2) c.x1=c.x2=c.y1=c.y2=0;
}

void calculeaza_aria()
{
DREPTUNGHI a,b,c,iab,iac,ibc,iabc;
a=drept[1];
b=drept[2];
c=drept[3];
intersecteaza_d(a,b,iab);
intersecteaza_d(a,c,iac);
intersecteaza_d(b,c,ibc);
intersecteaza_d(iab,c,iabc);
s=aria(a)+aria(b)+aria(c)-aria(iab)-aria(iac)-aria(ibc)+aria(iabc);
}

void scrie()
{
FILE *g=fopen("reuniune.out","w");
fprintf(g,"%lld %lld\n",s,p1+p2);
fcloseall();
}

int main()
{
citire();
lenmuc=0;
pune_muchiile(lenmuc);
baleiaza(p1);
lenmuc=0;
pune_muchiile_2(lenmuc);
baleiaza(p2);
calculeaza_aria();
scrie();
return 0;
}