Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13446 Accepted Submission(s): 5588
Problem Description
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
Input
The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don’t process it.
The input file is terminated by a line containing a single 0. Don’t process it.
Output
For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
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
Source
给你n个矩形,让你算出总面积,重叠的当然不能算。
POINT:
离散化不说了。
扫描线法,具体看了很多大神的博客,可以去搜搜。
下面这两段要好好理解一下。
if(cnt[x])sum[x]=hah[r+1]-hah[l];
R=lower_bound(hah+1,hah+1+m,len[i].r)-hah-1
可以想象成每个点都是一条线段,区间(1,1)代表端点1到2的长度,区间(1,2)代表端点1到3的长度。
为什么这样呢,走过弯路调试一下就知道了呀!直接听懂不是很无趣吗?(你只是不会讲而已别说了
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
#define lt 2*x
#define rt 2*x+1
#define LL long long
const int N = 210*10;
struct node
{
double l,r;
int flag;
double h;
}len[N/10];
int cnt[N];
double sum[N];
double hah[N/10];
bool cmd(node a,node b)
{
return a.h<b.h;
}
void init()
{
memset(hah,0,sizeof hah);
memset(len,0,sizeof len);
memset(sum,0,sizeof sum);
memset(cnt,0,sizeof cnt);
}
void pushup(int x,int l,int r)
{
if(cnt[x]) sum[x]=hah[r+1]-hah[l];
else if(l==r) sum[x]=0;
else sum[x]=sum[rt]+sum[lt];
}
void add(int x,int l,int r,int ll,int rr,int flag)
{
if(ll<=l&&rr>=r)
{
cnt[x]+=flag;
}
else
{
int mid=(l+r)>>1;
if(ll<=mid) add(lt,l,mid,ll,rr,flag);
if(mid<rr) add(rt,mid+1,r,ll,rr,flag);
}
pushup(x,l,r);
}
int main()
{
int n;
int p=0;
while(~scanf("%d",&n)&&n)
{
init();
int num=0;
for(int i=1;i<=n;i++)
{
double x1,y1,x2,y2;
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
len[++num].h=y1,len[num].l=x1,len[num].r=x2;
hah[num]=x1;
len[num].flag=1;
len[++num].h=y2,len[num].l=x1,len[num].r=x2;
hah[num]=x2;
len[num].flag=-1;
}
sort(len+1,len+1+num,cmd);
sort(hah+1,hah+1+num);
int m=1;
for(int i=2;i<=num;i++)
if(hah[i]!=hah[i-1]) hah[++m]=hah[i];
double ans=0;
for(int i=1;i<=num;i++)
{
int L=lower_bound(hah+1,hah+1+m,len[i].l)-hah;
int R=lower_bound(hah+1,hah+1+m,len[i].r)-hah-1;
add(1,1,m,L,R,len[i].flag);
ans+=sum[1]*(len[i+1].h-len[i].h);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",++p,ans);
}
return 0;
}