好久没写过博客了,这学期不是很有热情去写博客,写过的题也懒得写题解。现在来水一水博客,写一下若干年前的题目的题解。
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21978 Accepted Submission(s): 8714
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.
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.
Sample Input
2
10 10 20 20
15 15 25 25.5
0
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00
Total explored area: 180.00
Source
题目就是求矩形面积并。扫描线。
一开始不知道扫描线是个什么东西的时候,感觉很厉害的东西,知道是什么之后,就。。。
就是区间更新的问题,因为在这里线段树维护的是线段,不是点,所以每个点代表的其实是一条线段,所以会有什么+1,-1的操作。
具体的看代码。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e4+;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int cnt[maxn<<];
double sum[maxn<<],f[maxn]; void init()
{
memset(cnt,,sizeof cnt);
memset(sum,,sizeof sum);
} struct node{
double h,l,r;
int s; node(){} node(double a,double b,double c,int d):l(a),r(b),h(c),s(d){} bool operator<(const node&cmp){
return h<cmp.h;
} }line[maxn]; void pushup(int rt,int l,int r)
{
if(cnt[rt]){//解决线段重复问题,如果当前区间有标记,那么就用f数组进行更新,不是左右儿子更新
sum[rt]=f[r+]-f[l];
}
else if(l==r){
sum[rt]=;
}
else{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R){
cnt[rt]+=c;
pushup(rt,l,r);
return ;
} int m=(l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
pushup(rt,l,r);
} int main()
{
int n,cas=;
while(~scanf("%d",&n)&&n){
int h=;
for(int i=;i<=n;i++){//从下往上扫
double a,b,c,d;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
f[h]=a;
line[h++]=node(a,c,b,);//标记入边
f[h]=c;
line[h++]=node(a,c,d,-);//标记出边
}
sort(f,f+h);
sort(line,line+h);
int d=unique(f,f+h)-f;//离散化
init();
double ret=;
for(int i=;i<h-;i++){//因为每一个点代表的是线段,0代表0-1这一段,所以是查询的时候-1
int l=lower_bound(f,f+d,line[i].l)-f;//直接用下标进行操作
int r=lower_bound(f,f+d,line[i].r)-f;
r--;//因为线段树上点代表线段,从i到i+1这一段,所以查询的时候,右边要-1
if(l<=r) update(l,r,line[i].s,,d-,);
ret+=sum[]*(line[i+].h-line[i].h);
}
printf("Test case #%d\n",cas++);
printf("Total explored area: %.2f\n\n",ret);
}
return ;
}
开溜。