• POJ 1389 Area of Simple Polygons(面积并)


    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 

    线段树-扫描线求面积并裸题。

    代码:

    #include <cstdio>
    #include <algorithm>
      
    using namespace std;  
      
    const int MAXN = 1005;  
      
    struct Node{  
        int l,r,h;  
        int flag; 
    	 
        Node(){}  
        
        Node(int a,int b,int c,int d): l(a),r(b),h(c),flag(d){}    
        bool operator < (const struct Node &b)const{   
            return h < b.h;  
        }  
        
    }node[MAXN*2];  
      
    struct D{  
    
        int value;  
        int cnt;  
        bool lazy;
    	  
    }Tree[MAXN*8];  
      
    int X[MAXN*2];  
      
    int Bin(int key , int Num){  
    
        int l = 1,r = Num,mid; 
    	 
        while(l<=r){  
            mid = l + (r-l)/2;  
            if(X[mid] == key)return mid;  
            else if(X[mid] > key)r = mid-1;  
            else if(X[mid] < key)l = mid+1;  
        }  
        
        return -1;  
        
    }  
      
    void Build(int temp , int left , int right){  
    
        Tree[temp].cnt = 0;  
        Tree[temp].value = 0;  
        Tree[temp].lazy = false;  
        
        if(left == right)return;  
        
        int mid = left + (right-left)/2;  
        
        Build(temp<<1,left,mid);  
        Build(temp<<1|1,mid+1,right);  
        
    }  
      
    void PushDown(int temp , int left , int right){  
    
        if(Tree[temp].lazy){  
        
            Tree[temp<<1].cnt = Tree[temp<<1|1].cnt = Tree[temp].cnt;  
            Tree[temp<<1].lazy = Tree[temp<<1|1].lazy = true; 
    		 
            int mid = left + (right-left)/2;  
            
            if(Tree[temp].cnt>0){  
                Tree[temp<<1].value = X[mid+1]-X[left];  
                Tree[temp<<1|1].value = X[right+1]-X[mid+1];  
            }  
            else Tree[temp<<1].value = Tree[temp<<1|1].value = 0;  
            
            Tree[temp].lazy = false;  
            
        }  
        
    }  
      
    void Up(int temp){  
    
        if(Tree[temp<<1].cnt==-1 || Tree[temp<<1|1].cnt==-1 || Tree[temp<<1].cnt!=Tree[temp<<1|1].cnt)Tree[temp].cnt = -1;  
        else Tree[temp].cnt = Tree[temp<<1].cnt;  
        
        Tree[temp].value = Tree[temp<<1].value + Tree[temp<<1|1].value;  
        
    }  
      
    void Updata(int temp , int left , int right , int ql , int qr ,int flag){  
    
        if(ql>right || qr<left)return ;  
        
        if(ql<=left && qr>=right){  
        
            if(Tree[temp].cnt != -1){  
                Tree[temp].cnt += flag;  
                Tree[temp].lazy = true; 
    			 
                if(Tree[temp].cnt>0)Tree[temp].value = X[right+1]-X[left];  
                else Tree[temp].value = 0;  
                
                return ;  
                
            }  
            
        }  
        
        PushDown(temp,left,right); 
    	 
        int mid = left + (right-left)/2; 
    	 
        if(ql<=mid)Updata(temp<<1,left,mid,ql,qr,flag);  
        if(qr>mid)Updata(temp<<1|1,mid+1,right,ql,qr,flag);  
        
        Up(temp);  
        
    }  
      
    int main(){   
    
       	int x1,x2,y1,y2;  
        int ans;  
        
        while(1){  
        
            ans = 0;//最终面积   
            int n = 0,m = 0;  
            
        	while( scanf("%d %d %d %d",&x1,&y1,&x2,&y2) && (x1!=-1 || y1!=-1 || x2!=-1 || y2!=-1)){  
                X[++n] = x1;  
                node[++m] = Node(x1,x2,y1,1);  
                X[++n] = x2;  
                node[++m] = Node(x1,x2,y2,-1);  
            }  
            if(n == 0 && m == 0)break;
            
            sort(X+1,X+1+n);  
            sort(node+1,node+1+m); 
    		 
            int sum = 1;  
            
            for(int i=2 ; i<=n ; i++)if(X[i]!=X[i-1])X[++sum] = X[i];  
            
            Build(1,1,sum-1);  
            
            for(int i=1 ; i<m ; i++){  
                int l = Bin(node[i].l,sum);  
                int r = Bin(node[i].r,sum)-1;  
                
                Updata(1,1,sum-1,l,r,node[i].flag); 
    			 
                ans += Tree[1].value*(node[i+1].h-node[i].h);  
            }  
            
            printf("%lld
    ",ans); 
    		   
        }  
          
        return 0;  
        
    }  

  • 相关阅读:
    2-sat问题,输出方案,几种方法(赵爽的论文染色解法+其完全改进版)浅析 / POJ3683
    hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。
    最小费用最大流粗解 poj2516
    hdu3078 建层次树+在线LCA算法+排序
    hdu 3594 Cactus /uva 10510 仙人掌图判定
    有向图最小路径覆盖方法浅析、证明 //hdu 3861
    hdu 1827 有向图缩点看度数
    条件转化,2-sat BZOJ 1997
    2-sat基础题 BZOJ 1823
    2-sat 分类讨论 UVALIVE 3713
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514096.html
Copyright © 2020-2023  润新知