HDU-1828 Picture (scanning lines and the perimeter of the rectangle required)

http://acm.hdu.edu.cn/showproblem.php?pid=1828

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description

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.
Please process to the end of file.

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

 

 

Meaning of the questions:

A plurality of rectangular, parallel to the two sides of the rectangular coordinate axes, between the rectangles may cover each other, find the circumference.

Ideas:
Each record two vertical side of the rectangle (x1, y1, y2) and (x2, y1, y2), all in accordance with the vertical side x from small to large, then a start to a vertical edge perimeter, so to vertical vertical side where the x-axis, i.e., a straight line is a scanning line.
Each move to a new vertical side, we need to calculate the scanning line is useful where the vertical side edge length (i.e., the vertical side of the useful part of the current, the current may be covered with the vertical side portion), and added to the current scan line and before the scan line length * number of lateral transverse edge strip,
It has been calculated to the last vertical edge, that is, the complete perimeter.

 

code show as below:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <string>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <queue>
  9 #include <set>
 10 #include <stack>
 11 #include <map>
 12 #include <math.h>
 13 const int INF=0x3f3f3f3f;
 14 typedef long long LL;
 15 const int mod=1e9+7;
 16 //const double PI=acos(-1);
 17 const int maxn=1e5+10;
 18 using namespace std;
 19 //ios::sync_with_stdio(false);
 20 //    cin.tie(NULL);
 21 
 22 const int N=5005;
 23 
 24 struct Line_node
 25 {
 26     int x;
 27     int y1,y2;
 28     int flag;
29      BOOL  operator <( const Line_node & S)
 30      {
 31 is          IF (X == SX)
 32              return In Flag> S.FLAG;
 33 is          the else 
34 is              return X < SX;
 35      }
 36 } Line [N * 2 ];
 37 [  
38 is  struct SegTree_node
 39  {
 40      int L;
 41 is      int R & lt;
 42 is      BOOL LF, RF; // about whether the boundary point is covered; 
43 is      int cover_len;
 44     int cover_num;
 45     int num;//矩形数目
 46 }SegTree[maxn<<2];
 47 
 48 vector<int> vt;
 49 
 50 void Build(int l,int r,int rt)
 51 {
 52     SegTree[rt].l=l;
 53     SegTree[rt].r=r;
 54     SegTree[rt].cover_len=0;
 55     SegTree[rt].cover_num=0;
 56     SegTree[rt].num=0;
 57     SegTree[rt].lf=SegTree[rt].rf=false;
 58     if(l+1==r)
 59         return ;
 60     int mid=(l+r)>>1;
 61     Build(l,mid,rt<<1);
 62     Build(mid,r,rt<<1|1); 
 63 }
 64 
 65 void PushUp(int rt)
 66 {
 67     int l=SegTree[rt].l;
 68     intr = SegTree [rt] .r;
69      if (SegTree [rt] .cover_num> 0 )
 70      {
 71          SegTree [rt] .cover_len = vt [r] - vt [l];
72          SegTree [rt] = .lf SegTree [rt] .rf = true ;
73          SegTree [rt] .num = 1 ;
74          return ;
75      }
 76  //     if (l + 1 == r)
 77  //     {
 78  //         SegTree [rt] .cover_len = 0;
79  //         SegTree [rt] = .lf SegTree [rt] .rf = false;
80  //        SegTree [rt] .num = 0;
81  //         return;
82  //     } 
83      SegTree [rt] .cover_len SegTree = [rt << 1 ] + .cover_len SegTree [rt << 1 | 1 ] .cover_len;
84      SegTree [RT] = .num SegTree [rt << 1 ] + .num SegTree [rt << 1 | 1 ] .num- (SegTree [rt << 1 ] & .rf SegTree [rt << 1 | 1 ] .lf)
85      SegTree [rt] .lf SegTree = [rt << 1 ] .lf;
86      SegTree [RT] = .rf SegTree [rt << 1 | 1 ] .rf; 
 }
 88 
 89 void Update(Line_node t,int rt)
 90 {
 91     int l=SegTree[rt].l;
 92     int r=SegTree[rt].r;
 93     if(t.y1<=vt[l]&&t.y2>=vt[r])
 94     {
 95         SegTree[rt].cover_num+=t.flag;
 96         PushUp(rt);
 97         return ;
 98     }
 99     int mid=(l+r)>>1;
100     if(t.y1<vt[mid])
101         Update(t,rt<<1);
102     if(t.y2>vt[mid])
103         Update(t,rt<<1|1);
104     PushUp(rt);
105 }
106 
107 int main()
108 {
109     int n;
110     while (~scanf("%d",&n))
111     {
112         vt.clear();
113         for(int i=0;i<n;i++)
114         {
115             int x1,x2,y1,y2;
116             scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
117             Line[i*2].x=x1;
118             Line[i*2].y1=y1;
119             Line[i*2].y2=y2;
120             Line[i*2].flag=1;
121             
122             Line[i*2+1].x=x2;
123             Line[i*2+1].y1=y1;
124             Line [I * 2 + . 1 ] .Y2 = Y2;
 125              Line [I * 2 + . 1 ] .flag = - . 1 ;
 126              vt.push_back (Y1);
 127              vt.push_back (Y2);
 128          }
 129          Sort (Line , Line + 2 * n-);
 130.          // Y coordinate discretized 
131 is          Sort (vt.begin (), vt.end ());
 132          int NUM = UNIQUE (vt.begin (), vt.end ()) - VT .begin (); // to obtain discrete finished weight and number 
133          the Build ( 0 , num- . 1 ,1);
134         int ans=0;
135         int prelen=0;
136         for(int i=0;i<n*2;i++)
137         {
138             if(i>0)
139             {
140                 ans+=SegTree[1].num*2*(Line[i].x-Line[i-1].x);
141             }
142             Update(Line[i],1);
143             ans + = abs (SegTree [ 1 ] .cover_len- prelen);
144              prelen SegTree = [ 1 ] .cover_len;
145          }
 146          printf ( " % d \ n " , ans);
147      }
 148      return  0 ;
149 }

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11462730.html