POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

题意:给出矩形两对角点坐标,求矩形面积并。

解法:线段树+离散化。

每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线段树维护两个值:cover和len,cover表示该线段区间目前被覆盖的线段数目,len表示当前已覆盖的线段长度(化为离散前的真值),每次加入一条线段,将其y_low,y_high之间的区间染上line[i].cover,再以tree[1].len乘以接下来的线段的x坐标减去当前x坐标,即计算了一部分面积。

如图情况,将会计算三次面积:

代码:

#include <iostream>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define eps 1e-8
using namespace std;
#define N 307

struct node
{
    int cov;
    double len;
}tree[8*N];

struct Line
{
    double y1,y2,x;
    int cov;
}line[N];
double yy[N];

int cmp(Line ka,Line kb)
{
    return ka.x < kb.x;
}

int bsearch(int l,int r,double x)
{
    while(l <= r)
    {
        int mid = (l+r)/2;
        if(fabs(yy[mid]-x) < eps)
            return mid;
        if(x > yy[mid])
            l = mid+1;
        else
            r = mid-1;
    }
    return l;
}

void build(int l,int r,int rt)
{
    tree[rt].cov = 0;
    tree[rt].len = 0;
    if(l == r-1) return;
    int mid = (l+r)/2;
    build(l,mid,2*rt);
    build(mid,r,2*rt+1);
}

void pushup(int l,int r,int rt)
{
    if(tree[rt].cov)
        tree[rt].len = yy[r]-yy[l];
    else if(l+1 == r)
        tree[rt].len = 0;
    else
        tree[rt].len = tree[2*rt].len + tree[2*rt+1].len;
}

void update(int l,int r,int aa,int bb,int cover,int rt)
{
    if(aa <= l && bb >= r)
    {
        tree[rt].cov += cover;
        pushup(l,r,rt);
        return;
    }
    if(l+1 == r) return;
    int mid = (l+r)/2;
    if(aa <= mid)
        update(l,mid,aa,bb,cover,2*rt);
    if(bb > mid)
        update(mid,r,aa,bb,cover,2*rt+1);
    pushup(l,r,rt);
}

int main()
{
    int n,m,cs = 1,i,j;
    double x1,y1,x2,y2;
    while(scanf("%d",&n)!=EOF && n)
    {
        m = 1;
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[m].x = x1,line[m].y1 = y1,line[m].y2 = y2,line[m].cov = 1,yy[m++] = y1;
            line[m].x = x2,line[m].y1 = y1,line[m].y2 = y2,line[m].cov = -1,yy[m++] = y2;
        }
        m--;
        sort(yy+1,yy+m+1);
        int cnt = 2;
        for(i=2;i<=m;i++)
        {
            if(yy[i] != yy[i-1])
                yy[cnt++] = yy[i];
        }
        cnt--;
        build(1,cnt,1);
        sort(line+1,line+m+1,cmp);
        double ans = 0.0;
        printf("Test case #%d\n",cs++);
        for(i=1;i<m;i++)
        {
            int L = bsearch(1,cnt,line[i].y1);
            int R = bsearch(1,cnt,line[i].y2);
            update(1,cnt,L,R,line[i].cov,1);
            ans += tree[1].len*(line[i+1].x-line[i].x);
        }
        printf("Total explored area: %0.2lf\n\n",ans);
    }
    return 0;
}

时间: 2024-08-24 13:53:02

POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并的相关文章

POJ&#183;1151 Atlantis&#183;线段树求矩形面积并

题目在这:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K 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 is

HDU 1542.Atlantis-线段树求矩形面积并(离散化、扫描线/线段树)-贴模板

好久没写过博客了,这学期不是很有热情去写博客,写过的题也懒得写题解.现在来水一水博客,写一下若干年前的题目的题解. Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21978    Accepted Submission(s): 8714 Problem Description There are several anc

hdu 1542 Atlantis 线段树求面积并,,,尼玛数据真坑人,数组千万不能开小!

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7815    Accepted Submission(s): 3420 Problem Description There are several ancient Greek texts that contain descriptions of the fabled 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

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(线段树扫描线)

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(线段树)

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

POJ 1151 HDU 1542 Atlantis(扫描线)

题目大意就是:去一个地方探险,然后给你一些地图描写叙述这个地方,每一个描写叙述是一个矩形的右下角和左上角.地图有些地方是重叠的.所以让你求出被描写叙述的地方的总面积. 扫描线的第一道题,想了又想,啸爷还给我讲了讲,最终有点理解了啊. 先说扫描线:书上说扫描线不是一个物体.而是一个概念. 在计算几何中的作用类似于图论中的bfs与dfs.所以还是须要多做题目来体会一下啊. 这道题目的做法是:离散化x坐标.然后依照y坐标的大小进行排序,每一条保存它的左边界的位置与右边界的位置.以及自身的高度. 还有就