POJ_1389
这个题目和POJ_1151基本是一样的,一些具体的思路可以参考我的那篇题解:http://www.cnblogs.com/staginner/archive/2012/02/20/2359396.html。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXD 2010
#define zero 1e-8
struct square
{
double x1, y1, x2, y2;
}s[MAXD];
int N, M, X;
double tx[MAXD], left[MAXD], right[MAXD];
int cmp1(const void *_p, const void *_q)
{
square *p = (square *)_p, *q = (square *)_q;
return p->y1 < q->y1 ? -1 : 1;
}
int cmp2(const void *_p, const void *_q)
{
double *p = (double *)_p, *q = (double *)_q;
return *p < *q ? -1 : 1;
}
double fabs(double x)
{
return x < 0 ? -x : x;
}
int dcmp(double x)
{
return fabs(x) < zero ? 0 : (x < 0 ? -1 : 1);
}
void readin()
{
scanf("%lf%lf%lf%lf", &s[N].x1, &s[N].y1, &s[N].x2, &s[N].y2);
tx[X ++] = s[N].x1;
tx[X ++] = s[N].x2;
}
void prepare()
{
int i, j, k;
qsort(s, N, sizeof(s[0]), cmp1);
qsort(tx, X, sizeof(tx[0]), cmp2);
M = 0;
for(i = 1; i < X; i ++)
if(dcmp(tx[i] - tx[i - 1]) != 0)
{
left[M] = tx[i - 1], right[M] = tx[i];
++ M;
}
}
void solve()
{
int i, j, k;
double ans = 0, up, down;
prepare();
for(i = 0; i < M; i ++)
{
up = down = -1;
for(j = 0; j < N; j ++)
if(dcmp(left[i] - s[j].x1) >= 0 && dcmp(right[i] - s[j].x2) <= 0)
{
if(dcmp(s[j].y1 - up) > 0)
{
ans += (up - down) * (right[i] - left[i]);
down = s[j].y1, up = s[j].y2;
}
else if(dcmp(s[j].y2 - up) > 0)
up = s[j].y2;
}
ans += (up - down) * (right[i] - left[i]);
}
printf("%.0lf\n", ans);
}
int main()
{
for(;;)
{
N = X = 0;
readin();
if(dcmp(s[N].x1 + 1) == 0)
break;
++ N;
for(;;)
{
readin();
if(dcmp(s[N].x1 + 1) == 0)
break;
++ N;
}
solve();
}
return 0;
}