(概念题)线段树 HOJ 1495 Area of Simple Polygons

Area of Simple Polygons

My Tags  (Edit)
Source : ACM ICPC Taipei Regional Contest 2001
Time limit : 1 secMemory limit : 32 M

Submitted : 103, Accepted : 73

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
题意:也是求矩形的覆盖面积
做法:略

要注意的是,答案用long long吧 用int的话 可能会溢出
代码:
#include<iostream>
#include<string.h>
#include<cstdio>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
#define MAX 2000+10
#define MOD 100000000
const int inf = 0x7fffffff;
#define lson l , m , rt<<1
#define rson m+1 , r , rt<<1|1

struct Seg
{
int l , r , cover , h;
}seg[MAX];

int cover[MAX<<2];
int sum[MAX<<2];
int X[MAX];

void PushUp(int l,int r,int rt)
{
if (cover[rt]) sum[rt] = X[r+1]-X[l];
else if (l==r) sum[rt] = 0;
else sum[rt] = sum[rt<<1]+sum[rt<<1|1];
}

void Update(int L,int R,int c,int l,int r,int rt)
{
if (L<=l && r<=R)
{
cover[rt] += c;
PushUp(l,r,rt);
return;
}
int m = (l+r)>>1;
if (L<=m) Update(L,R,c,lson);
if (R>m) Update(L,R,c,rson);
PushUp(l,r,rt);
}

int Bisearch(int left,int right,int x)
{
int mid = (left+right)>>1;
while (left<=right)
{
if (X[mid]==x) return mid;
else if (X[mid]<x) 
left = mid+1;
else 
right = mid-1;
mid = (left+right)>>1;
}
return -1;
}

bool cmp(const Seg& s1,const Seg& s2)
{
return s1.h<s2.h;
}

int main()
{
int a,b,c,d;
while (scanf("%d%d%d%d",&a,&b,&c,&d)==4)
{
if (a+b+c+d==-4) return 0;
int m = 0;
X[m] = a;
seg[m].l = seg[m+1].l = a;
seg[m].r = seg[m+1].r = c;
seg[m].h = d;
seg[m].cover = 1;
++m;
X[m] = c;
seg[m].h = b;
seg[m].cover = -1;
++m;
while (scanf("%d%d%d%d",&a,&b,&c,&d)==4)
{
if (a+b+c+d==-4)
{
sort(seg,seg+m,cmp);
sort(X,X+m);
int k = 0;
for (int i = 0 ; i < m ; ++i) if (X[i]!=X[i+1])
X[k++] = X[i];
long long ret = 0;
memset(sum,0,sizeof(sum));
memset(cover,0,sizeof(cover));
for (int i = 0 ; i < m-1 ; ++i)
{
int L = Bisearch(0,k-1,seg[i].l);
int R = Bisearch(0,k-1,seg[i].r)-1;
Update(L,R,seg[i].cover,0,k-1,1);
ret += (long long)sum[1]*(seg[i+1].h-seg[i].h);
}
printf("%lld\n",ret);
break;
}
X[m] = a;
seg[m].l = seg[m+1].l = a;
seg[m].r = seg[m+1].r = c;
seg[m].h = d;
seg[m].cover = 1;
++m;
X[m] = c;
seg[m].h = b;
seg[m].cover = -1;
++m;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值