hdu1542 扫描线+线段树

37 篇文章 0 订阅
给n个矩形(边平行于坐标轴) 求围起来的面积 裸的扫描线+线段树
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <cstring>
using namespace std;
#define ss printf("orz\n");
#define maxn 2000
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define getmid int mid=(l+r)>>1

int n,n1,n2;
double xx[maxn],yy[maxn];
int tag[maxn];
double sum[maxn];
struct node
{
    double x1,x2,y1,y2;
}rec[maxn];
vector<node>g[maxn],g2[maxn];
void init()
{
    for(int i=1;i<=n1;i++) g[i].clear(),g2[i].clear();
    n1=n2=0;
    memset(sum,0.0,sizeof(sum));
    memset(tag,0,sizeof(tag));
    memset(xx,0.0,sizeof(xx));
    memset(yy,0.0,sizeof(yy));
    
}
void pushup(int rt,int l,int r)
{
    if(tag[rt]>0)
    {
        sum[rt]=yy[r+1]-yy[l];
    }
    else
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void update(int l,int r,int rt,int x,int y,int add)
{
    //printf("%d %d %d %d %d\n",l,r,rt,x,y,rt);
    //system("pause");
    if(x<=l&&r<=y)
    {
        tag[rt]+=add;
        pushup(rt,l,r);
        return;
    }
    getmid;
    if(x<=mid) update(lson,x,y,add);
    if(y>mid) update(rson,x,y,add);
    pushup(rt,l,r);
}
void change(int x)
{
    for(int j=0;j<g2[x].size();j++)
                update(1,n2-1,1, (int)g2[x][j].x1 , (int)g2[x][j].x2-1,-1);

    for(int j=0;j<g[x].size();j++)
                update(1,n2-1,1, (int)g[x][j].x1 , (int)g[x][j].x2-1,1);
}
int main()
{
    int cas=0;
    while(scanf("%d",&n))
    {
        if(n==0)
        {
            break;
        }
        init();
        double x1,x2,y1,y2;
        map<double,int>mpx,mpy;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            rec[i]={x1,x2,y1,y2};
            if(!mpy[y1])
            {
                n2++;yy[n2]=y1;mpy[y1]=1;
            }
            if(!mpy[y2])
            {
                n2++;yy[n2]=y2;mpy[y2]=1;
            }
            if(!mpx[x1])
            {
                n1++;xx[n1]=x1;mpx[x1]=1;
            }
            if(!mpx[x2])
            {
                n1++;xx[n1]=x2;mpx[x2]=1;
            }
        }

        sort(xx+1,xx+1+n1);
        sort(yy+1,yy+1+n2);
        for(int i=1;i<=n1;i++) mpx[xx[i]]=i;
        for(int i=1;i<=n2;i++) mpy[yy[i]]=i;
        node tmp;
        for(int i=1;i<=n;i++)
        {
            x1=rec[i].x1;x2=rec[i].x2;
            y1=1.0*mpy[rec[i].y1];y2=1.0*mpy[rec[i].y2];
            tmp={y1,y2,1.0,0.0};
            g[mpx[x1]].push_back(tmp);
            tmp={y1,y2,-1.0,0.0};
            g2[mpx[x2]].push_back(tmp);
        }
        double ans=0;
        change(1);
        for(int i=2;i<=n1;i++)
        {
            ans+=(xx[i]-xx[i-1])*sum[1];
            change(i);
        }
        cas++;
        printf("Test case #%d\n",cas);
        printf("Total explored area: %.2f\n\n",ans);
        //init();
    }
    return 0;
}
/*
5
1.37 0.10 2.75 0.20
2.96 2.01 3.06 2.75
2.65 1.05 4.97 1.15
2.33 0.74 4.02 0.84
2.96 0.10 4.02 1.47

8
9.33 19.52 30.44 24.39
8.06 29.38 9.75 51.77
22.38 15.06 32.35 33.30
30.66 19.20 44.13 19.62
17.93 15.38 39.35 41.58
9.65 6.47 29.80 10.39
26.20 24.61 40.63 35.85
25.25 12.51 29.80 36.17

8
157.98 278.30 246.25 501.22
152.89 15.06 355.43 214.74
61.53 56.12 141.85 96.97
21.43 303.77 250.07 329.65
5.83 115.65 306.10 211.88
275.44 132.52 353.21 276.49
149.71 175.49 271.40 379.94
127.42 32.57 187.05 282.22

*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值