K - Atlantis (线段树+扫描线)

自闭症网瘾萝莉.ら 提交于 2019-12-02 17:06:49
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.

InputThe 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.OutputFor 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 
#define lson i<<1,l,m
#define rson i<<1|1,m+1,r
const int maxn=222;
double x[maxn];
struct node
{
    double l,r,h;//左右坐标,高度
    int d;//标记上位边还是下位边
    node() {}
    node(double l,double r,double h,int d):l(l),r(r),h(h),d(d) {}
    bool operator < (const node &a)const
    {
        return h<a.h;
    }
} line[maxn];
int cnt[maxn<<2];
double sum[maxn<<2];
void pushup(int i,int l,int r)
{
    if(cnt[i])
        sum[i]=x[r+1]-x[l];
    else
        sum[i]=sum[i<<1]+sum[i<<1|1];
}
//       update(l,r,line[i].d,1,1,k-1);
void update(int ql,int qr,int v,int i,int l,int r)
{
    if(ql<=l && qr>=r)
    {
        cnt[i]+=v;
        pushup(i,l,r);
        return ;
    }
    int m=(l+r)>>1;
    if(ql<=m) update(ql,qr,v,lson);
    if(qr>m)  update(ql,qr,v,rson);
    pushup(i,l,r);
}
int main()
{
    int q;
    int kase=0;
    while(cin>>q&&q)
    {
        memset(cnt,0,sizeof(cnt));    //
        memset(sum,0,sizeof(sum));    //存储线段
        int n=0,m=0;
        for(int i=1; i<=q; i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
            x[++n]=x1;
            x[++n]=x2;
            line[++m] = node(x1,x2,y1,1);
            line[++m] = node(x1,x2,y2,-1);
        }
        sort(x+1,x+1+n);
        sort(line+1,line+1+m);
        int k=1;
        k=unique(x+1,x+n+1)-x-1;
        double ans=0.0;
        for(int i=1; i<m; i++)
        {

            int l=lower_bound(x+1,x+k+1,line[i].l)-x;
            int r=lower_bound(x+1,x+k+1,line[i].r)-x;
            r--;
            if(l<=r) update(l,r,line[i].d,1,1,k-1);
            ans+=sum[1]*(line[i+1].h-line[i].h);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",++kase,ans);
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!