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

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

题面

Vjudge

题解

学一学扫描线
其实很简单啦
这道题目要求的就是若干矩形的面积和

把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫)
这样只需要求出每次和\(x\)轴覆盖的长度
就可以两两相乘,求出面积
最后累计和就行啦

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 500
#define lson (now<<1)
#define rson (now<<1|1)
struct Node{double x1,x2,y,w;}p[MAX];
bool operator<(Node a,Node b){return a.y<b.y;}
double S[MAX];
int top;
int n,tot;
struct SegmentTreeNode
{
    double ss;
    int ly;
}t[MAX<<3];
void pushup(int now,int l,int r)
{
    if(t[now].ly)t[now].ss=S[r+1]-S[l];
    else if(l==r)t[now].ss=0;
    else t[now].ss=t[lson].ss+t[rson].ss;
}
void Modify(int now,int l,int r,int L,int R,int w)
{
    if(L<=l&&r<=R)
    {
        t[now].ly+=w;
        pushup(now,l,r);
        return;
    }
    int mid=(l+r)>>1;
    if(L<=mid)Modify(lson,l,mid,L,R,w);
    if(R>mid)Modify(rson,mid+1,r,L,R,w);
    pushup(now,l,r);
}
int main()
{
    int TT=0;
    while(scanf("%d",&n))
    {
        ++TT;
        if(!n)break;
        tot=top=0;
        double x1,x2,y1,y2;
        for(int i=1;i<=n;++i)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            p[++tot]=(Node){x1,x2,y1,1};
            p[++tot]=(Node){x1,x2,y2,-1};
            S[++top]=x1,S[++top]=x2;
        }
        sort(&S[1],&S[top+1]);
        sort(&p[1],&p[tot+1]);
        top=unique(&S[1],&S[top+1])-S-1;
        double ans=0;
        for(int i=1;i<tot;++i)
        {
            int l=lower_bound(&S[1],&S[top+1],p[i].x1)-S;
            int r=lower_bound(&S[1],&S[top+1],p[i].x2)-S-1;
            if(l<=r)Modify(1,1,top,l,r,p[i].w);
            if(i!=tot)ans+=(p[i+1].y-p[i].y)*t[1].ss;
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",TT,ans);
        memset(t,0,sizeof(t));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/cjyyb/p/8424495.html

时间: 2024-08-04 23:34:47

【POJ1151】Atlantis(线段树,扫描线)的相关文章

HDU 1542 Atlantis(线段树扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6788    Accepted Submission(s): 2970 Problem Description There are several ancient Greek

hdu 1542 Atlantis(线段树&amp;扫描线&amp;面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 1542 Atlantis (线段树+扫描线)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18559    Accepted Submission(s): 7523 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8998    Accepted Submission(s): 3856 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

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

一维离散化, 扫描线扫另一维, 用线段树维护 POJ建议交C++...G++貌似double要用%f ? 反正同一份代码C++AC,G++WA ------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 109; struct Line { double p,

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

Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18061   Accepted: 6873 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

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

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

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

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

线段树+扫描线+离散化解poj1151 hdu 1542 ( Atlantis )

受此链接很大启发才明白扫描线: http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 我的代码如下: #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #i

HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid