zoukankan      html  css  js  c++  java
  • POJ 1177 Picture(线段树周长并)

     描述
    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

    Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.
     

    The corresponding boundary is the whole set of line segments drawn in Figure 2.
    The vertices of all rectangles have integer coordinates.
    Input
    Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

    0 <= number of rectangles < 5000
    All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
    Output
    Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
    Sample Input
    7
    -15 0 5 10
    -5 8 20 25
    15 -4 24 14
    0 -6 16 4
    2 15 10 22
    30 10 36 20
    34 0 40 16
    Sample Output
    228
    题意
    给你N个矩形,每个矩形给你左下和右上端点,求周长并
    题解
    很容易想到的就是离散化后,先扫一遍x,再扫一遍y
    每次加上这次和上次的差即为新加线段的长度
    代码
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 const int N=10005;
     7 int col[2][N<<2],sum[2][N<<2],x[2][N<<2];
     8 struct seg
     9 {
    10     int l,r,h,s;
    11     seg() {}
    12     seg(int l,int r,int h,int s):l(l),r(r),h(h),s(s) {}
    13     bool operator<(const seg &ob)const
    14     {
    15         return h<ob.h;
    16     }
    17 }s[2][N<<2];
    18 void PushUp(int rt,int l,int r,int flag)
    19 {
    20     if(col[flag][rt])sum[flag][rt]=x[flag][r+1]-x[flag][l];
    21     else if(l==r)sum[flag][rt]=0;
    22     else sum[flag][rt]=sum[flag][rt<<1]+sum[flag][rt<<1|1];
    23 }
    24 void Update(int L,int R,int flag,int C,int l,int r,int rt)
    25 {
    26     if(L<=l&&r<=R)
    27     {
    28         col[flag][rt]+=C;
    29         PushUp(rt,l,r,flag);
    30         return;
    31     }
    32     int mid=(l+r)>>1;
    33     if(L<=mid)Update(L,R,flag,C,l,mid,rt<<1);
    34     if(R>mid)Update(L,R,flag,C,mid+1,r,rt<<1|1);
    35     PushUp(rt,l,r,flag);
    36 }
    37 int main()
    38 {
    39     int n,x1,x2,y1,y2;
    40     while(scanf("%d",&n)!=EOF)
    41     {
    42         for(int i=0;i<n;i++)
    43         {
    44             scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    45             s[0][i]=seg(x1,x2,y1,1);
    46             x[0][i]=x1;
    47             s[0][n+i]=seg(x1,x2,y2,-1);
    48             x[0][n+i]=x2;
    49 
    50             s[1][i]=seg(y1,y2,x1,1);
    51             x[1][i]=y1;
    52             s[1][n+i]=seg(y1,y2,x2,-1);
    53             x[1][n+i]=y2;
    54         }
    55         n<<=1;
    56         sort(x[0],x[0]+n);
    57         sort(s[0],s[0]+n);
    58         sort(x[1],x[1]+n);
    59         sort(s[1],s[1]+n);
    60         int ans=0,pre=0,pre1=0;
    61         for(int i=0;i<n;i++)
    62         {
    63             int l=lower_bound(x[0],x[0]+n,s[0][i].l)-x[0];
    64             int r=lower_bound(x[0],x[0]+n,s[0][i].r)-x[0]-1;
    65             Update(l,r,0,s[0][i].s,0,n-1,1);
    66             ans+=abs(sum[0][1]-pre);
    67             pre=sum[0][1];
    68         }
    69         for(int i=0;i<n;i++)
    70         {
    71             int l=lower_bound(x[1],x[1]+n,s[1][i].l)-x[1];
    72             int r=lower_bound(x[1],x[1]+n,s[1][i].r)-x[1]-1;
    73             Update(l,r,1,s[1][i].s,0,n-1,1);
    74             ans+=abs(sum[1][1]-pre1);
    75             pre1=sum[1][1];
    76         }
    77         printf("%d
    ",ans);
    78     }
    79     return 0;
    80 }
  • 相关阅读:
    Hadoop与分布式开发
    Hadoop体系结构
    MapReduce基本流程与设计思想初步
    hadoop集群启动时DataNode节点启动失败
    初识Hadoop
    国家标准免费下载网站大全
    Effective C++ 之 Item 3:尽可能使用 const
    Effective C++ 之 Item 2:尽量以 const, enum, inline 替换 #define
    Effective C++ 之 Item 1: 视C++为一个语言联邦
    Effective C++ 之 0 导读(Introduction)
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9292094.html
Copyright © 2011-2022 走看看