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 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 

题意:矩形面积交.只用了离散化没用线段树也 47MS AC。。就是耗费的空间大了点,,不过将vis数组开成bool类型应该可以少很多空间..
///离散化
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int N = 2005;
struct Rec{
    int x1,y1,x2,y2;
}rec[N];
int x[N],y[N];
int vis[N][N];
int k,t;
int binary1(int value){
    int mid,l=0,r=k-1;
    while(l<r){
        mid = (l+r)>>1;
        if(x[mid]==value) return mid;
        if(x[mid]<value) l = mid+1;
        else r = mid-1;
    }
    return l;
}
int binary2(int value){
    int mid,l=0,r=k-1;
    while(l<r){
        mid = (l+r)>>1;
        if(y[mid]==value) return mid;
        if(y[mid]<value) l = mid+1;
        else r = mid-1;
    }
    return l;
}
void input(){
    int x1,y1,x2,y2;
    while(true){
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if(x1==-1&&x2==-1&&y1==-1&&y2==-1) break;
        rec[t].x1 = x1,rec[t].y1 = y1,rec[t].x2=x2,rec[t++].y2 = y2;
        x[k] = x1,y[k++] = y1;
        x[k] = x2,y[k++] = y2;
    }
    sort(x,x+k);
    sort(y,y+k);
}
void solve(){
    int t1,t2,t3,t4;
    for(int i=0;i<t;i++){
        t1 = binary1(rec[i].x1);
        t2 = binary1(rec[i].x2);
        t3 = binary2(rec[i].y1);
        t4 = binary2(rec[i].y2);
        for(int j=t1;j<t2;j++){
            for(int l = t3;l<t4;l++){
                vis[j][l] = 1;
            }
        }
    }
    int area = 0;
    for(int i=0;i<k;i++){
        for(int j=0;j<k;j++){
            area+=vis[i][j]*(x[i+1]-x[i])*(y[j+1]-y[j]);
        }
    }
    printf("%d\n",area);
}
int main()
{
    int x1,y1,x2,y2;
    while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF){
        if(x1==-1&&x2==-1&&y1==-1&&y2==-1) break;
        memset(vis,0,sizeof(vis));
        k=0,t=0;
        rec[t].x1 = x1,rec[t].y1 = y1,rec[t].x2=x2,rec[t++].y2 = y2;
        x[k] = x1,y[k++] = y1;
        x[k] = x2,y[k++] = y2;
        input();
        solve();
    }
    return 0;
}
时间: 2024-08-10 15:08:17

poj 1389(离散化+计算几何)的相关文章

POJ 2299 离散化线段树

点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 40827   Accepted: 14752 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by

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(面积合并,线段树+离散化)

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 1151(离散化+矩形面积并)

题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/129999.aspx 我线段树还是不会写这个.. 借个图: ///离散化 #include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <cm

POJ Transmitters(计算几何 极角排序啊)

题目链接:http://poj.org/problem?id=1106 Description In a wireless network with multiple transmitters sending on the same frequencies, it is often a requirement that signals don't overlap, or at least that they don't conflict. One way of accomplishing thi

【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 2299(离散化+树状数组)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 53777   Accepted: 19766 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

poj 1106 Transmitters (计算几何,叉积||极角排序)

Transmitters Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4817   Accepted: 2576 Description In a wireless network with multiple transmitters sending on the same frequencies, it is often a requirement that signals don't overlap, or at

poj 2318 TOYS(计算几何 点与线段的关系)

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12015   Accepted: 5792 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w