Hdu 1542 Atlantis 线段树 求矩形面积并

来源:互联网 发布:成都蜀美网络 编辑:程序博客网 时间:2024/05/22 15:20

Atlantis

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


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
 

-------------

转题解:

题意:矩形面积并
思路:浮点数先要离散化;然后把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用cnt表示该区间下边比上边多几个,sum代表该区间内被覆盖的线段的长度总和
这里线段树的一个结点并非是线段的一个端点,而是该端点和下一个端点间的线段,所以题目中r+1,r-1的地方可以自己好好的琢磨一下
线段树操作:update:区间增减 query:直接取根节点的值
-------------


-------------

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int N=22222;struct Seg{    double l,r,h;    int s;    Seg(){}    Seg(double a,double b,double c,int d):l(a),r(b),h(c),s(d){}    bool operator < (const Seg& cmp) const    {        return h<cmp.h;    }}a[N];double X[N];struct TREE{    int l;    int r;    int cnt;    double sum;}tree[N*4];void build(int root,int l,int r){    tree[root].l=l;    tree[root].r=r;    tree[root].cnt=0;    tree[root].sum=0;    if (l==r)    {        return;    }    int mid=(l+r)/2;    build(root<<1,l,mid);    build(root<<1|1,mid+1,r);}void push_up(int root){    if (tree[root].cnt)    {        tree[root].sum=X[tree[root].r+1]-X[tree[root].l];    }    else if (tree[root].l==tree[root].r)    {        tree[root].sum=0;    }    else    {        tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;    }}void update(int root,int L,int R,int val){    if (L<=tree[root].l&&R>=tree[root].r)    {        tree[root].cnt+=val;        push_up(root);        return;    }    int mid=(tree[root].l+tree[root].r)/2;    if (L<=mid) update(root<<1,L,R,val);    if (R>mid) update(root<<1|1,L,R,val);    push_up(root);}int main(){    int n,m,k;    double x1,y1,x2,y2,ret;    int cnt=1;    while (~scanf("%d",&n))    {        if (n==0) break;        m=0;        for (int i=0;i<n;i++)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            X[m]=x1;            a[m++]=Seg(x1,x2,y1,1);            X[m]=x2;            a[m++]=Seg(x1,x2,y2,-1);        }        sort(X,X+m);        sort(a,a+m);        k=1;        for (int i=1;i<m;i++)        {            if (X[i]!=X[i-1]) X[k++]=X[i];        }        build(1,0,k-1);        ret=0;        for (int i=0;i<m-1;i++)        {            int l=lower_bound(X,X+k,a[i].l)-X;            int r=lower_bound(X,X+k,a[i].r)-X-1;            if (l<=r) update(1,l,r,a[i].s);            ret+=tree[1].sum*(a[i+1].h-a[i].h);        }        printf("Test case #%d\nTotal explored area: %0.2lf\n\n",cnt++,ret);    }    return 0;}








原创粉丝点击