poj1151 Atlantis——扫描线+线段树

题目:http://poj.org/problem?id=1151

经典的扫描线问题;

可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改;

每个格子记录两个值:c(矩形存在情况),sum(对当前答案作出贡献的长度);

将y离散化作为建树的依据;

一开始没想到线段树上的点应该是横向的格子,写了个乱七八糟:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t,n,xt;
double s[805],ans,tr[805],mx,eps=0.36;
struct P{
    double x,y1,y2;
    int tp;
    P(int x=0,int y1=0,int y2=0,int t=0):x(x),y1(y1),y2(y2),tp(t) {}
}p[805];
void pushup(int x)
{
    tr[x]=tr[x<<1]+tr[x<<1|1];
}
void add(int x,double l,double r,double L,double R,int w)
{
//    cout<<x<<endl;
//    printf("l=%lf r=%lf eps=%lf\n",l,r,eps);
    if(r-l<eps)
    {
        s[x]+=w;
        if(s[x])tr[x]=1;
        else tr[x]=0;
//        printf("s[%d]=%d\n",x,s[x]);
        return;
    }
    double mid=(l+r)/2;
    if(mid>=L)add(x<<1,l,mid,L,R,w);
    if(mid<R)add(x<<1|1,mid+1,r,L,R,w);
    pushup(x);
}
bool cmp(P x,P y){return x.x<y.x;}
int main()
{
    while(scanf("%d",&n)==1)
    {
        t++;xt=0;mx=0;ans=0;
        memset(tr,0,sizeof tr);
        memset(s,0,sizeof s);
        if(!n)return 0;
        for(int i=1;i<=n;i++)
        {
            xt++;scanf("%lf%lf",&p[xt].x,&p[xt].y1);
            xt++;scanf("%lf%lf",&p[xt].x,&p[xt].y1);
            p[xt-1].y2=p[xt].y1;p[xt].y2=p[xt-1].y1;
            p[xt-1].tp=1;p[xt].tp=-1;
            mx=max(mx,max(p[xt].y1,p[xt].y2));
        }
        sort(p+1,p+xt+1,cmp);
        double lst=-1;
        for(int i=1;i<=xt;i++)
        {
            if(p[i].y1>p[i].y2)swap(p[i].y1,p[i].y2);
            add(1,0,mx,p[i].y1,p[i].y2,p[i].tp);
            if(lst!=-1)ans+=(p[i].x-lst)*tr[1];
            lst=p[i].x;
        }
        printf("Test case #%d\nTotal explored area: %.2lf \n",t,ans);
    }
    return 0;
}

然后参考别人的博客,努力理解了半天,模仿着打了出来,终于A了...

学到了add()函数里面那种二分的方法。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,ct,t;
double ans,y[205];
struct P{
    int l,r,c;//c为上下矩形抵消数
    double ly,ry,sum;//sum为此时计入计算的长度
}tr[805];
struct E{
    int tp;
    double x,ly,ry;
}ed[205];
bool cmp(E x,E y){return x.x<y.x;}
void build(int x,int l,int r)
{
    tr[x].l=l;tr[x].r=r;
    tr[x].ly=y[l];tr[x].ry=y[r];
    tr[x].c=0;tr[x].sum=0;
    if(l==r-1)return;
    int mid=(l+r)>>1;
    build(x<<1,l,mid);
    build(x<<1|1,mid,r);//不是mid+1,因为l,r表示此块上下两边
}
void pushup(int x)
{
    if(tr[x].c>0)//全选
        tr[x].sum=tr[x].ry-tr[x].ly;
    else if(tr[x].l==tr[x].r-1)//仅有一格且被退出
         tr[x].sum=0;
    else//可能有其他边覆盖
        tr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum;
}
void add(int x,E e)
{
    if(tr[x].ly==e.ly&&tr[x].ry==e.ry)
    {
        tr[x].c+=e.tp;
        pushup(x);
        return;
    }
    if(tr[x<<1].ry>=e.ry)add(x<<1,e);
    else if(tr[x<<1|1].ly<=e.ly)add(x<<1|1,e);
    else
    {
        E tmp=e;
        tmp.ry=tr[x<<1].ry;
        add(x<<1,tmp);
        tmp=e;
        tmp.ly=tr[x<<1|1].ly;
        add(x<<1|1,tmp);
    }
    pushup(x);
}
int main()
{
    while(scanf("%d",&n)==1)
    {
        t++;
        if(!n)return 0;
        ct=0;ans=0;
        for(int i=1;i<=n;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            y[++ct]=y1;ed[ct].tp=1;
            ed[ct].x=x1;ed[ct].ly=y1;ed[ct].ry=y2;
            y[++ct]=y2;ed[ct].tp=-1;
            ed[ct].x=x2;ed[ct].ly=y1;ed[ct].ry=y2;
        }
        sort(ed+1,ed+ct+1,cmp);
        sort(y+1,y+ct+1);
        build(1,1,ct);
        add(1,ed[1]);
        for(int i=2;i<=ct;i++)
        {
            ans+=tr[1].sum*(ed[i].x-ed[i-1].x);
            add(1,ed[i]);//上下勿反
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",t,ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Zinn/p/8969820.html

时间: 2024-08-28 11:00:43

poj1151 Atlantis——扫描线+线段树的相关文章

POJ 1151 Atlantis 扫描线+线段树

点击打开链接 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17252   Accepted: 6567 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of pa

【POJ1151】Atlantis(线段树,扫描线)

[POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) 这样只需要求出每次和\(x\)轴覆盖的长度 就可以两两相乘,求出面积 最后累计和就行啦 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cma

poj 1151 Atlantis (离散化 + 扫描线 + 线段树)

题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 #define LL __int64 8 #define lson l, mid, 2*rt

hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积

题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> #include <algorithm> #define MAXN 110 #define LL ((rt<<1)+1) #define RR ((rt<<1)+2) using namespace std; int n; struct segment{ double l

POJ 1151 Atlantis(线段树 + 扫描线)

转载请注明原文:http://www.cnblogs.com/burning-flame/p/5934653.html 题目链接:http://poj.org/problem?id=1151 题意: 给你 n 个矩形的对角线坐标,求 n 个矩形并集的面积. 做法: 扫描线 + 线段树. 因为作线段树的题遇到了扫描线,只是粗浅的了解了一下. 就像字面上的:线性扫描一遍,对于每个单元,因为某些事件的发生会有一些变化. 举个例子: 现有长度为 n 的数组a,同时有 n 个区间覆盖[li, ri],对于

HDU1542_Atlantis(扫描线/线段树+离散)

解题报告 题目传送门 题意: 求矩形并面积. 思路: 离散+线段树+扫描线. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; struct Seg { int v; double h,lx,rx; friend bool operator < (Seg a,Seg b) { return a.h<b

POJ训练计划1177_Picture(扫描线/线段树+离散)

解题报告 题意: 求矩形周长和. 思路: 左扫上扫,扫过了. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; struct Seg { int lx,rx,ly,ry,h,v; friend bool operator < (Seg a,Seg b) { re

HDU3265_Posters(扫描线/线段树)

解题报告 题意: 给定的矩形里面有镂空的矩阵,求矩阵面积并. 思路: 直接把一个图形拆成4个矩形,进行面积并. 扫描线+线段树 #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #define LL __int64 using namespace std; struct Seg { int lx,rx,h,v; friend bool operator

HDU1377_Counting Squares(扫描线/线段树)

解题报告 题意: 矩形面积并. 思路: 扫描线+线段树 #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; struct Seg { int lx,rx,h,v; friend bool operator < (Seg a,Seg b) { return a.h<b.h; } } seg[500000]