POJ 1389 Area of Simple Polygons

Area of Simple Polygons

Time Limit: 1000ms

Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 1389
64-bit integer IO format: %lld      Java class name: Main

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

Taiwan 2001

解题:线段树扫描线。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 50010;
 6 struct node {
 7     int lt,rt,cover,sum;
 8 } tree[maxn<<2];
 9 struct Line {
10     int x1,x2,y,up;
11     Line(int a = 0,int b = 0,int c = 0,int d = 0) {
12         x1 = a;
13         x2 = b;
14         y = c;
15         up = d;
16     }
17     bool operator<(const Line &t)const {
18         if(y == t.y) return up > t.up;
19         return y < t.y;
20     }
21 } line[maxn];
22 void build(int L,int R,int v) {
23     tree[v].cover = tree[v].sum = 0;
24     tree[v].lt = L;
25     tree[v].rt = R;
26     if(L+1 == R) return;
27     int mid = (L + R)>>1;
28     build(L,mid,v<<1);
29     build(mid,R,v<<1|1);
30 }
31 void pushup(int v) {
32     if(tree[v].cover) {
33         tree[v].sum = tree[v].rt - tree[v].lt;
34         return;
35     } else if(tree[v].lt + 1 == tree[v].rt) {
36         tree[v].sum = 0;
37         return;
38     }
39     tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum;
40 }
41 void update(int lt,int rt,int val,int v) {
42     if(lt <= tree[v].lt && rt >= tree[v].rt) {
43         tree[v].cover += val;
44         pushup(v);
45         return;
46     }
47     if(lt < tree[v<<1].rt) update(lt,rt,val,v<<1);
48     if(rt > tree[v<<1|1].lt) update(lt,rt,val,v<<1|1);
49     pushup(v);
50 }
51 int main() {
52     int x1,y1,x2,y2;
53     while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2)) {
54         if(x1 == -1 && y1 == -1 && x2 == -1 && y2 == -1) break;
55         int tot = 0;
56         line[tot++] = Line(x1,x2,y1,1);
57         line[tot++] = Line(x1,x2,y2,-1);
58         while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2)) {
59             if(x1 == -1 && y1 == -1 && x2 == -1 && y2 == -1) break;
60             line[tot++] = Line(x1,x2,y1,1);
61             line[tot++] = Line(x1,x2,y2,-1);
62         }
63         sort(line,line+tot);
64         int ret = 0,pre = 0;
65         build(0,50000,1);
66         for(int i = 0; i < tot; ++i) {
67             ret += tree[1].sum*(line[i].y - pre);
68             pre = line[i].y;
69             update(line[i].x1,line[i].x2,line[i].up,1);
70         }
71         printf("%d\n",ret);
72     }
73     return 0;
74 }

时间: 2024-10-07 08:53:35

POJ 1389 Area of Simple Polygons的相关文章

POJ 1389 Area of Simple Polygons(面积合并,线段树+离散化)

Area of Simple Polygons Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3257   Accepted: 1678 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 segment

POJ 1389 Area of Simple Polygons(扫描线)

Area of Simple Polygons Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3278   Accepted: 1694 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 segment

POJ 1389 Area of Simple Polygons 扫描线+线段树面积并

---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 同1151.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <stdio.h> #include

【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)

离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了. 因为更新[3,6]时,它只更新到[3,3],[6,6]. 要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段树节点表示到x[l]到x[r+1]的区间. 这样tree[l,r]=tree[l,m]+tree[m+1,r]=[x[l],x[m+1]]+[x[m+1],x[r+1]] #include<iostream> #include<algorithm> #include<cstdio

poj 1389(离散化+计算几何)

Area of Simple Polygons Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3412   Accepted: 1763 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 segment

poj 1265 Area (Pick定理+求面积)

链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4969   Accepted: 2231 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionag

POJ 1265 Area

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4713   Accepted: 2129 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new resear

poj 1265 Area 面积+多边形内点数

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5861   Accepted: 2612 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new resear

poj 1654 Area (多边形求面积)

链接:http://poj.org/problem?id=1654 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14952   Accepted: 4189 Description You are going to compute the area of a special kind of polygon. One vertex of the polygon is the origin of the orth