HDOJ1542 线段树+离散+扫描线

来源:互联网 发布:js array splice无参数 编辑:程序博客网 时间:2024/06/07 10:30

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14860    Accepted Submission(s): 6124


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
210 10 20 2015 15 25 25.50
 

Sample Output
Test case #1Total explored area: 180.00
 

Source
Mid-Central European Regional Contest 2000
 

Recommend
linle   |   We have carefully selected several similar problems for you:  1828 1255 1540 1823 2871 

分析:坐标范围非常大,要离散化坐标。

扫描线思路如下图所述:




代码如下:
#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn = 200+5;double y[maxn];struct Line{    double down,up;    double x;    int cover;}line[maxn];struct node{   int l,r,cover;   double len;}tree[2000];int n,tot;int len;double total_area;bool cmp(Line a, Line b){return a.x<b.x;}void init(){     tot = 0;     double x1,y1,x2,y2;     for (int i=0; i<n; i++){         scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);         y[tot] = y1;         line[tot].cover = 1; //为1表示矩形的左边         line[tot].x = x1;         line[tot].down = y1;         line[tot].up = y2;         tot++;         y[tot] = y2;         line[tot].cover = -1; //为-1表示矩形的右边         line[tot].x = x2;         line[tot].down = y1;         line[tot].up = y2;         tot++;     }}int Find(double x){    int high = len, low = 0, mid;    while (low<=high){        mid = (low+high)>>1;        if (y[mid]==x) return mid;        if (y[mid]<x) low = mid+1;        else high = mid-1;    }    return low;}void build(int rt , int l, int r){    tree[rt].l = l;    tree[rt].r = r;    tree[rt].cover = 0;    tree[rt].len = 0;    if (l+1==r) return; //左闭右开的区间    int mid = (l+r)>>1;    build(rt<<1,l,mid);    build(rt<<1|1,mid,r);}void func(int rt){    if (tree[rt].cover) tree[rt].len = y[tree[rt].r] - y[tree[rt].l];    else if (tree[rt].l+1==tree[rt].r) tree[rt].len = 0;    else tree[rt].len = tree[rt<<1].len + tree[rt<<1|1].len;}void update(int rt, int L, int R, int cover){    if (tree[rt].l>R || tree[rt].r<L) return;    if (tree[rt].l>=L && tree[rt].r<=R) {        tree[rt].cover += cover;        func(rt);        return;    }    update(rt<<1,L,R,cover);    update(rt<<1|1,L,R,cover);    func(rt);}int kase = 0;int main(){    int f1,f2;    while (scanf("%d",&n) && n){        init();        sort(y,y+tot);        len = unique(y,y+tot)-y;        len--;        build(1,0,len);        sort(line,line+tot,cmp);        total_area = 0;        for (int i=0; i<tot-1; i++) {           f1 = Find(line[i].down);           f2 = Find(line[i].up);           update(1,f1,f2,line[i].cover);           total_area += tree[1].len*(line[i+1].x-line[i].x);        }        printf("Test case #%d\n",++kase);        printf("Total explored area: %.2lf\n\n",total_area);    }    return 0;}