Picture
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3310 Accepted Submission(s): 1723
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.
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.
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
Source
题目意思:
给n个矩形,求并后的总周长
思路:
矩形周长并,很经典的扫描线题目。周长=水平线+竖直线。
求水平线的长度时,每插入一条线段后总长度减去插入线段之前的总长度即为增长的长度,最终即得出水平线的长度。
求竖直线的长度时,每插入一条线段后“裸露”出的点的个数乘上后一线段高度和这一线段的高度差,即得出竖直线长度。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 5005 12 #define ll root<<1 13 #define rr root<<1|1 14 #define mid (a[root].l+a[root].r)/2 15 16 int max(int x,int y){return x>y?x:y;} 17 int min(int x,int y){return x<y?x:y;} 18 int abs(int x,int y){return x<0?-x:x;} 19 20 struct node{ 21 int l, r; 22 int sum; 23 int ysum; 24 int val; 25 bool lv, rv; 26 }a[N*8]; 27 28 struct Line{ 29 int x1, x2, y; 30 int val; 31 Line(){} 32 Line(int a,int b,int c,int d){ 33 x1=a; 34 x2=b; 35 y=c; 36 val=d; 37 } 38 }line[N*2]; 39 40 bool cmp(Line a,Line b){ 41 return a.y<b.y; 42 } 43 int n, m; 44 int xx[N*2]; 45 46 int b_s(int key){ 47 int l=1, r=m; 48 while(l<=r){ 49 int mm=(l+r)/2; 50 if(xx[mm]==key) return mm; 51 else if(xx[mm]>key) r=mm-1; 52 else if(xx[mm]<key) l=mm+1; 53 } 54 } 55 56 void build(int l,int r,int root){ 57 a[root].l=l; 58 a[root].r=r; 59 a[root].ysum=a[root].sum=a[root].val=0; 60 a[root].lv=a[root].rv=false; 61 if(l==r) return; 62 build(l,mid,ll); 63 build(mid+1,r,rr); 64 } 65 66 void up(int root){ 67 if(a[root].val){ 68 a[root].sum=xx[a[root].r+1]-xx[a[root].l]; 69 a[root].lv=a[root].rv=true; 70 a[root].ysum=2; 71 } 72 else if(a[root].l==a[root].r) { 73 a[root].lv=a[root].rv=false; 74 a[root].ysum=a[root].sum=0; 75 } 76 else{ 77 a[root].sum=a[ll].sum+a[rr].sum; 78 a[root].lv=a[ll].lv; 79 a[root].rv=a[rr].rv; 80 a[root].ysum=a[ll].ysum+a[rr].ysum; 81 if(a[rr].lv&&a[ll].rv) a[root].ysum-=2; 82 } 83 } 84 85 void update(int l,int r,int val,int root){ 86 if(a[root].l==l&&a[root].r==r){ 87 a[root].val+=val; 88 up(root); 89 return; 90 } 91 if(l>=a[rr].l) update(l,r,val,rr); 92 else if(r<=a[ll].r) update(l,r,val,ll); 93 else{ 94 update(l,mid,val,ll); 95 update(mid+1,r,val,rr); 96 } 97 up(root); 98 } 99 100 main() 101 { 102 int i, j, k; 103 int x1, y1, x2, y2; 104 int kase=1; 105 while(scanf("%d",&n)==1&&n){ 106 m=1;k=0; 107 for(i=0;i<n;i++){ 108 scanf("%d %d %d %d",&x1,&y1,&x2,&y2); 109 line[k++]=Line(x1,x2,y1,1); 110 line[k++]=Line(x1,x2,y2,-1); 111 xx[m++]=x1;xx[m++]=x2; 112 } 113 sort(xx+1,xx+m); 114 m=unique(xx+1,xx+m)-xx-1; 115 sort(line,line+k,cmp); 116 build(1,m,1); 117 int ans=0; 118 int pre=0; 119 for(i=0;i<k-1;i++){ 120 update(b_s(line[i].x1),b_s(line[i].x2)-1,line[i].val,1); 121 ans+=abs(a[1].sum-pre)+(line[i+1].y-line[i].y)*a[1].ysum; 122 pre=a[1].sum; 123 } 124 update(b_s(line[k-1].x1),b_s(line[k-1].x2)-1,line[k-1].val,1); 125 ans+=abs(a[1].sum-pre); 126 printf("%d\n",ans); 127 } 128 }