poj 1389 线段树面积覆盖

http://poj.org/problem?id=1389

Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2772 Accepted: 1409

Description

There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers in the range of 0 through 50,000 indicating its x and y coordinates. 

Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a 2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon. A polygon is simple if there is no pair of nonconsecutive edges sharing a point. 

Example: Consider the following three rectangles: 

rectangle 1: < (0, 0) (4, 4) >, 

rectangle 2: < (1, 1) (5, 2) >, 

rectangle 3: < (1, 1) (2, 5) >. 

The total area of all simple polygons constructed by these rectangles is 18. 

Input

The input consists of multiple test cases. A line of 4 -1's separates each test case. An extra line of 4 -1's marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, 4 non-negative integers are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.

Output

For each test case, output the total area of all simple polygons in a line. 

Sample Input

0 0 4 4
1 1 5 2
1 1 2 5
-1 -1 -1 -1
0 0 2 2
1 1 3 3
2 2 4 4
-1 -1 -1 -1
-1 -1 -1 -1  

Sample Output

18
10 

Source

这题和poj 1151一样,就是输入的时候复杂点。把poj 1151代码拿过来改的。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int N=1000+10;
struct node
{
    int l,r;
    int lf,rf,width;
    int cover;
}mem[N*6];//线段树 lf rf 分别为边界 cover 为覆盖情况 width 为覆盖的宽度
struct poin
{
    int x; //记录x坐标
    int y1,y2;  //y1,y2用来记录平行y轴的线段的两个端点,
    int k; //标记是左边的边还是右边的边,拆点 k为-1代表左点 1 代表右点
}point[N*2];
int Y[N*2];
bool cmp(poin a,poin b) //排序(x按照从小到大,只需要把x排序)
{
    return a.x<b.x;
}
void build(int x,int l,int r)//建树
{
    mem[x].l=l;
    mem[x].r=r;
    mem[x].lf=Y[l];
    mem[x].rf=Y[r];
    mem[x].cover=mem[x].width=0;
    if(l+1==r) //叶子结点
    return ;
    int mid=(l+r)>>1;
    build(x*2,l,mid);//端点模型,不是格子模型
    build(x*2+1,mid,r);
}
void find(int x)//更新此处的覆盖宽度
{
    if(mem[x].cover>0) //完全覆盖(右点则要完全覆盖)
    {
        mem[x].width=mem[x].rf-mem[x].lf; //求出长度
        return ;
    }
    if(mem[x].l+1==mem[x].r) // 叶子节点,wiith 为 0
   mem[x].width=0;
    else//非叶子节点即为下面子区间的覆盖综合,注意由于插入的递归性保证了此等式的执行顺序的正确性
   mem[x].width=mem[x*2].width+mem[x*2+1].width;
}
void add(int x,int k)//加入一个点k的影响
{
    if(mem[x].lf>=point[k].y1&&mem[x].rf<=point[k].y2)//全部覆盖 ;//直接比较实际值,显示出了cover和标记为1,-1的作用
    {
        mem[x].cover+=point[k].k;
        find(x);
        return ;
    }
    int mid=(mem[x].l+mem[x].r)>>1;
    if(Y[mid]<=point[k].y1)//查看 需要怎样往下更新
    {
        add(x*2+1,k);
    }
 else if(Y[mid]>=point[k].y2)
    {
        add(x*2,k);
    }
 else
    {
        add(x*2,k);
        add(x*2+1,k);
    }
    find(x);
}
int main()
{
    //freopen("data.txt","r",stdin);
    int x1,y1,x2,y2;
    while(1)
    {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if(x1==-1&&y1==-1&&x2==-1&&y2==-1)
            break;
        int I=0;
        point[I].x=x1; //把坐标映射到数组里(离散化)
        point[I].y1=y1;
        point[I].y2=y2;
        point[I].k=1;
        Y[I]=y1;
        ++I;
        point[I].x=x2;
        point[I].y1=y1;
        point[I].y2=y2;
        point[I].k=-1;
        Y[I]=y2;
        ++I;
        while(1){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            if(x1==-1&&y1==-1&&x2==-1&&y2==-1)
                break;
            point[I].x=x1; //把坐标映射到数组里(离散化)
            point[I].y1=y1;
            point[I].y2=y2;
            point[I].k=1;
            Y[I]=y1;
            ++I;
            point[I].x=x2;
            point[I].y1=y1;
            point[I].y2=y2;
            point[I].k=-1;
            Y[I]=y2;
            ++I;
        }
        sort(Y,Y+I); //纵坐标排序(小到大)
        sort(point,point+I,cmp);
        build(1,0,I-1); //建树
        add(1,0);
        int ans=0;
        for(int i=1;i<I;++i)
        {
            //cout<<i<<endl;
            ans+=(mem[1].width*(point[i].x-point[i-1].x));//每次用两个x坐标差 ×   当前覆盖宽度
            //cout<<ans<<endl;
            add(1,i);//cout<<"TTT"<<endl;
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值