POJ 1389 Area of Simple Polygons(扫描线求面积)

思路:纯纯扫描线求面积模板题,求点赞qaq。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1e3+20;
double xx[maxn<<1];
struct Edge
{
    double l,r,h;
    int inout;
    Edge() {}
    Edge(double x1,double x2, double y, int inout):l(x1),r(x2),h(y),inout(inout) {}
} e[maxn<<1];

bool cmp(Edge a, Edge b)
{
    return a.h<b.h;
}
struct Tree
{
    int l, r;
    double len;
    int s;
    int mid()
    {
        return (l+r)>>1;
    }
}tr[maxn<<2];
void pushup(int now)
{
    if(tr[now].s)
        tr[now].len = xx[tr[now].r+1]-xx[tr[now].l];
    else if(tr[now].l == tr[now].r)
        tr[now].len = 0;//这里不能写return
    else
        tr[now].len = tr[now<<1].len + tr[now<<1|1].len;
}
void build(int now, int l, int r)
{
    tr[now].l = l, tr[now].r = r;
    tr[now].s = 0, tr[now].len = 0;
    int mid = tr[now].mid();
    if(tr[now].l == tr[now].r)return;
    build(now<<1,l,mid);
    build(now<<1|1,mid+1,r);
}
void update(int now,int l, int r,int val)
{

    if(tr[now].l>=l && tr[now].r <=r)
    {
         tr[now].s +=val;
         pushup(now);
         return;
    }
    int mid = tr[now].mid();
    if(mid>=r)
    {
        update(now<<1,l,r,val);
    }
    else if(mid<l)
        update(now<<1|1,l,r,val);
    else
    {
        update(now<<1, l,mid,val);
        update(now<<1|1, mid+1, r, val);
    }

    pushup(now);

}

int main()
{
    double x1, x2,y1,y2;
    int flag= 1;
    while(scanf("%lf%lf%lf%lf",&x1, &y1, &x2, &y2)!=EOF)
    {
        int cnt = 0;
        if(flag == 0&&x1 == -1 && x2 == -1 && y1 == -1 && y2 == -1)
            break;
        else
            flag= 1;
        xx[++cnt] = x1;
        e[cnt] = Edge(x1, x2,y1,1);
        xx[++cnt] = x2;
        e[cnt] = Edge(x1,x2,y2,-1);
        while(scanf("%lf%lf%lf%lf",&x1, &y1, &x2, &y2)!=EOF)
        {
            if(x1 == -1 && x2 == -1 && y1 == -1 && y2 == -1)
            {
                flag = 0;
                break;
            }
            xx[++cnt] = x1;
            e[cnt] = Edge(x1, x2,y1,1);
            xx[++cnt] = x2;
            e[cnt] = Edge(x1,x2,y2,-1);

        }
        sort(xx+1,xx+1+cnt);
        sort(e+1,e+1+cnt,cmp);


        int len = unique(xx+1,xx+1+cnt)-(xx+1);


        build(1,1,len);
        double area = 0;
        for(int i = 1;i<cnt;i++)
        {
            int l = lower_bound(xx+1,xx+1+len,e[i].l)-xx;
            int r = lower_bound(xx+1, xx+1+len,e[i].r)-xx-1;

            update(1,l,r,e[i].inout);


            area+= tr[1].len *(e[i+1].h-e[i].h);
        }
        printf("%.0f\n",area);
    }
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值