poj151 Atlantics 扫描线+线段树+离散化

来源:互联网 发布:淘宝客服退款中心 编辑:程序博客网 时间:2024/06/05 20:41

Atlantis
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 18661 Accepted: 7082

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

210 10 20 2015 15 25 25.50

Sample Output

Test case #1Total explored area: 180.00 

Source

Mid-Central European Regional Contest 2000

题意:比较简单,给定若干个和坐标轴平行的矩形,求这些矩形一共占据多大的面积。扫描线的裸题,不过要注意,由于数据比较大,需要用一下离散化。直接map对应即可

扫描线需要线段树维护整个数轴上一共覆盖了多长的长度,由于每个矩形的位置都是固定的,所以不存在区间划分的问题,也就是说线段树维护的区间是固定的。所以直接用标记维护区间即可,不需要向上或者是向下传递标记。这一点比较难想通,如果能够想通,基本上扫描线线段树就算是想明白了。

AC代码如下:

////  main.cpp//  poj1151////  Created by 蘇與軒 on 15/5/6.//  Copyright (c) 2015年 蘇與軒. All rights reserved.//#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <cstdlib>#include <string>#include <map>#include <set>#include <algorithm>#include <functional>#define rep(i,a,b) for (int i=a;i<((b)+1);i++)#define Rep(i,a,b) for (int i=a;i>=b;i--)#define foreach(e,x) for (__typeof(x.begin()) e=x.begin();e!=x.end();e++)#define mid ((l+r)>>1)#define lson (k<<1)#define rson (k<<1|1)#define MEM(a,x) memset(a,x,sizeof a)using namespace std;const int N=950;const long long Mod=1000000007;typedef pair<int, int> pii;typedef long long ll;struct P{    double xl,xr,h;    int l,r,v;    void set(double _xl,double _xr,double _h,int _v){        xl=_xl;xr=_xr;h=_h;v=_v;    }    friend bool operator < (const P &a,const P &b) {        return a.h<b.h;    }}p[250];double _x[240],dat[N];int n,col[N];map<double,int> mp;void push_up(int k,int l,int r) {    if (col[k]>0) dat[k]=_x[r]-_x[l];    else {        if (r-l==1) dat[k]=0;        else dat[k]=dat[lson]+dat[rson];    }}void update(int k,int l,int r,int ll,int rr,int v) {    if (l==r)   return ;    if (ll<=l&&rr>=r) {        col[k]+=v;        push_up(k,l,r);        return ;    }    if (rr<=mid)    update(lson, l, mid, ll, rr, v);    else if (ll>=mid)   update(rson, mid, r, ll, rr, v);    else {        update(lson, l, mid, ll, rr, v);        update(rson, mid, r, ll, rr, v);    }    push_up(k,l,r);}int main(int argc, const char * argv[]) {    int cse=0;    while (~scanf("%d",&n)) {        if (n==0)   break;        MEM(col,0);MEM(dat,0);MEM(_x,0);        mp.clear();        rep(i,1,n) {            double x,y,x1,y1;            scanf("%lf%lf%lf%lf",&x,&y,&x1,&y1);            p[i].set(x, x1, y, 1);            p[i+n].set(x, x1, y1, -1);            _x[i]=x;_x[i+n]=x1;        }        n<<=1;        sort(_x+1,_x+1+n);        sort(p+1,p+1+n);        int m=int(unique(_x+1, _x+1+n)-_x);        rep(i,1,m-1) mp[_x[i]]=i;        rep(i,1,n) {            p[i].l=mp[p[i].xl];            p[i].r=mp[p[i].xr];        }        double ans=0;        rep(i,1,n-1) {            update(1,1,m,p[i].l,p[i].r,p[i].v);            ans+=(p[i+1].h-p[i].h)*dat[1];        }        printf("Test case #%d\n",++cse);        printf("Total explored area: %.2lf\n\n",ans);    }    return 0;}



0 0