码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1151 Atlantis

时间:2019-03-24 17:36:24      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:nat   tput   sam   read   blank   sar   should   次数   enter   

题意

Language:
Atlantis
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 27764Accepted: 10003

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 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

Sample Output

Test case #1
Total explored area: 180.00 

Source

分析

求矩形面积并,扫描线模板题。n这么小,估计是最初的版本。

线段树不打标记维护,每个节点维护len和cnt,表示该节点代表的区间被矩形覆盖的长度为len,该节点自身被覆盖的次数为cnt。这题的题目性质保证了cnt非负。

时间复杂度\(O(n \log n)\)

代码

#include<iostream>
#include<map>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;

co int N=101;
int n,m,num=0;
struct P{
    double x,y,z;
    int k;
    bool operator<(co P&w)co {return x<w.x;}
}a[N*2];
double raw[N*2];
map<double,int> val;
struct T{
    int l,r,cnt;
    double len;
}t[N*8];
void build(int p,int l,int r){
    t[p].l=l,t[p].r=r;
    t[p].cnt=0,t[p].len=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(p<<1,l,mid),build(p<<1|1,mid+1,r);
}
void change(int p,int l,int r,double k){
    if(l<=t[p].l&&t[p].r<=r) return t[p].len=(t[p].cnt+=k)?raw[t[p].r+1]-raw[t[p].l]:(t[p].l==t[p].r?0:t[p<<1].len+t[p<<1|1].len),void();
    int mid=t[p].l+t[p].r>>1;
    if(l<=mid) change(p<<1,l,r,k);
    if(r>mid) change(p<<1|1,l,r,k);
    t[p].len=t[p].cnt?raw[t[p].r+1]-raw[t[p].l]:t[p<<1].len+t[p<<1|1].len;
}
void Atlantis(){
    for(int i=1;i<=n;++i){
        int k=i<<1;
        double y,z;
        scanf("%lf %lf %lf %lf",&a[k-1].x,&y,&a[k].x,&z);
        raw[k-1]=a[k-1].y=a[k].y=y;
        raw[k]=a[k-1].z=a[k].z=z;
        a[k-1].k=1,a[k].k=-1;
    }
    n<<=1;
    sort(raw+1,raw+n+1);
    int m=unique(raw+1,raw+n+1)-raw-1;
    for(int i=1;i<=m;++i) val[raw[i]]=i;
    sort(a+1,a+n+1);
    build(1,1,m-1);
    double ans=0;
    for(int i=1;i<n;++i){
        int y=val[a[i].y],z=val[a[i].z]-1;
        change(1,y,z,a[i].k);
        ans+=t[1].len*(a[i+1].x-a[i].x);
    }
    printf("Test case #%d\nTotal explored area: %.2f\n\n",++num,ans);
}
int main(){
//  freopen(".in","r",stdin),freopen(".out","w",stdout);
    while(read(n)) Atlantis();
    return 0;
}

POJ1151 Atlantis

标签:nat   tput   sam   read   blank   sar   should   次数   enter   

原文地址:https://www.cnblogs.com/autoint/p/10589026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!