【codevs 3044】【HDU 1542】【poj 1151】矩形面积并 线段树+‘扫描线’

E - Atlantis
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Submit

Status
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

题意: 给你N个矩形,求并起来的总面积!!
Sample Output
Test case #1
Total explored area: 180.00

题意: 给你N个矩形,求并起来的总面积!!
[面积并的扫描区间]!
【codevs 3044】【HDU 1542】【poj 1151】矩形面积并 线段树+‘扫描线’_第1张图片

思路:
1.面积并——————》》扫描线!!;
2.扫描线处理————》》排序+计算(y[i]-y[i-1])*tree[1].len;
3.离散化——————》》map+数组!!
*建树 1.线段建树 2.len 递归求和
*细节 见代码

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
#include<map>
#include<string.h>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
struct Line{
    double y,x1,x2;
    int id;
};
int n;
double ans=0;
struct nod{
    int flag;   //flag与lazy同步(detail太!),也可不用lazy,到点更新
    double len;
}tree[1003];
vector<double> xx;
vector<Line> lin;
map<double,int> H;
int cmp(Line x,Line y)
{
    return x.y<y.y;
}
int cc(double x,double y)
{
    return x<y;
}
double HH[250];
void push_up(int id)
{
    tree[id].len=tree[lson].len+tree[rson].len;
}
void suan(int id,int l,int r)
{
    if(tree[id].flag!=0)
    {
        tree[id].len=HH[r]-HH[l];
    }
    else    tree[id].len=0;
}
void push_down(int id,int l,int mid,int r)
{
    if(tree[id].flag==0) return ;
    tree[rson].flag+=tree[id].flag;
    tree[lson].flag+=tree[id].flag;
    suan(rson,mid,r);
    suan(lson,l,mid);
    tree[id].flag=0;
}
void add(int id,int l,int r,int L,int R,int V)
{   
    if(r<=L||l>=R)  return ;
    if(l>=L && r<=R && tree[id].flag+V>=0)  //flag<0 break不然push_down无果
    {

        tree[id].flag+=V;
        suan(id,l,r);
        if(tree[id].len==0)
        push_up(id);
        return ;
    }   
    int mid=(r+l)>>1;
    push_down(id,l,mid,r);
    add(lson,l,mid,L,R,V);
    add(rson,mid,r,L,R,V);
    push_up(id);    
}
int main()
{
    int cas=0;
    while(scanf("%d",&n))
    {
        if(n==0) return 0;
        H.clear();
        lin.clear();
        xx.clear();
        memset(HH,0,sizeof(HH));
        memset(tree,0,sizeof(tree));
        double x1,x2,y1,y2;
        ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            Line p;
            p.y=y1;
            p.x1=x1;
            p.x2=x2;
            p.id=1;
            lin.push_back(p);
            p.y=y2;
            p.id=-1;
            lin.push_back(p);
            xx.push_back(x1); xx.push_back(x2);
        }   
        sort(lin.begin(),lin.end(),cmp);
        sort(xx.begin(),xx.end());
        xx.erase(unique(xx.begin(),xx.end()),xx.end()); 
        for(int i=0;i<xx.size();i++)
        {
            H[xx[i]]=i+1;
            HH[i+1]=xx[i];
        }
        for(int i=0;i<lin. size();i++)
        {   
            if(i!=0)            ans+=double(lin[i].y-lin[i-1].y)*tree[1].len;   
add(1,1,2*n,H[lin[i].x1],H[lin[i].x2],lin[i].id);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",++cas,ans);
    }
}


吐槽 :poj过不了

你可能感兴趣的:(tree)