zoukankan      html  css  js  c++  java
  • 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
    ",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;
    }
  • 相关阅读:
    html上传图片后,在页面显示上传的图片
    arcgis 将日期字段赋值为当前日期
    一些使用文件夹重定向的系统中,运行ArcToolbox中的GP工具报IE脚本错误
    Gps中的 gpx文件和excel文件互相转换方法
    ARCMAP 本文将描述如何利用识别工具显示一幅与要素相关的图片。
    arcgis 正确显示标注的大小写
    在一些系统中,运行ArcToolbox中的任何GP工具报Microsoft脚本错误,并且工具对话框显示为空白
    arcgis Howto: 用空格作为分隔符提取字符串中的字符并赋值到另一个新字段
    使用.Net开发ArcGIS9扩展组件的注册到ArcMAP中
    arcgis 利用DEM寻找最小阻力路径 来自http://support.esrichinabj.cn/2008/0325/1189.html
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5462452.html
Copyright © 2011-2022 走看看