一样的题:HDU 1542
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18148 | Accepted: 6902 |
Description
Input
The input file is terminated by a line containing a single 0. Don't process it.
Output
Output a blank line after each test case.
Sample Input
2
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00 求矩形并、线段树+扫描线
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 210 int n;
int tot;
double sum;
double y[N];
double yl[N<<];
double yr[N<<];
int cover[N<<];
double height[N<<];
struct X
{
int pos;
double x,y1,y2;
bool operator <(const X &t)const
{
return x<t.x;
}
}x[N]; void pushup(int l,int r,int rt)
{
if(cover[rt]>) height[rt]=yr[rt]-yl[rt];
else if(l+==r) height[rt]=;
else height[rt]=height[rt<<]+height[rt<<|];
}
void build(int l,int r,int rt)
{
cover[rt]=;
yl[rt]=y[l];
yr[rt]=y[r];
if(l+==r) return;
int m=(l+r)>>;
build(l,m,rt<<);
build(m,r,rt<<|);
}
void update(int l,int r,int rt,double L,double R,int c)
{
if(yl[rt]==L && yr[rt]==R)
{
cover[rt]+=c;
pushup(l,r,rt);
return;
}
int m=(l+r)>>;
if(R<=y[m]) update(l,m,rt<<,L,R,c);
else if(L>=y[m]) update(m,r,rt<<|,L,R,c);
else
{
update(l,m,rt<<,L,y[m],c);
update(m,r,rt<<|,y[m],R,c);
}
pushup(l,r,rt);
}
int main()
{
int i,iCase=;
while(scanf("%d",&n),n)
{
tot=;
sum=;
memset(height,,sizeof(height));
for(i=;i<n;i++)
{
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
y[tot]=y1;
x[tot].x=x1;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=;
y[tot]=y2;
x[tot].x=x2;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=-;
}
sort(x,x+tot);
sort(y,y+tot);
build(,tot-,);
for(i=;i<tot;i++)
{
update(,tot-,,x[i-].y1,x[i-].y2,x[i-].pos);
sum+=height[]*(x[i].x-x[i-].x);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",iCase++,sum);
}
return ;
}
一样的题:POJ 1389
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3193 | Accepted: 1627 |
Description
Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a 2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon. A polygon is simple if there is no pair of nonconsecutive edges sharing a point.
Example: Consider the following three rectangles:
rectangle 1: < (0, 0) (4, 4) >,
rectangle 2: < (1, 1) (5, 2) >,
rectangle 3: < (1, 1) (2, 5) >.
The total area of all simple polygons constructed by these rectangles is 18.
Input
Output
Sample Input
0 0 4 4
1 1 5 2
1 1 2 5
-1 -1 -1 -1
0 0 2 2
1 1 3 3
2 2 4 4
-1 -1 -1 -1
-1 -1 -1 -1
Sample Output
18
10
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 210 int n;
int tot;
double sum;
double y[N];
double yl[N<<];
double yr[N<<];
int cover[N<<];
double height[N<<];
struct X
{
int pos;
double x,y1,y2;
bool operator <(const X &t)const
{
return x<t.x;
}
}x[N]; void pushup(int l,int r,int rt)
{
if(cover[rt]>) height[rt]=yr[rt]-yl[rt];
else if(l+==r) height[rt]=;
else height[rt]=height[rt<<]+height[rt<<|];
}
void build(int l,int r,int rt)
{
cover[rt]=;
yl[rt]=y[l];
yr[rt]=y[r];
if(l+==r) return;
int m=(l+r)>>;
build(l,m,rt<<);
build(m,r,rt<<|);
}
void update(int l,int r,int rt,double L,double R,int c)
{
if(yl[rt]==L && yr[rt]==R)
{
cover[rt]+=c;
pushup(l,r,rt);
return;
}
int m=(l+r)>>;
if(R<=y[m]) update(l,m,rt<<,L,R,c);
else if(L>=y[m]) update(m,r,rt<<|,L,R,c);
else
{
update(l,m,rt<<,L,y[m],c);
update(m,r,rt<<|,y[m],R,c);
}
pushup(l,r,rt);
}
int main()
{
int i;
while()
{
tot=;
sum=;
memset(height,,sizeof(height));
n=;
double x1,y1,x2,y2;
while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2) && x1+x2+y1+y2!=-)
{
y[tot]=y1;
x[tot].x=x1;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=;
y[tot]=y2;
x[tot].x=x2;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=-;
n++;
}
if(n==) break;
sort(x,x+tot);
sort(y,y+tot);
build(,tot-,);
for(i=;i<tot;i++)
{
update(,tot-,,x[i-].y1,x[i-].y2,x[i-].pos);
sum+=height[]*(x[i].x-x[i-].x);
}
printf("%.0f\n",sum);
}
return ;
}