Shaping Regions
N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the sheet's borders. All rectangles fall within the borders of the sheet so that different figures of different colors will be seen.
The coordinate system has its origin (0,0) at the sheet's lower left corner with axes parallel to the sheet's borders.
PROGRAM NAME: rect1
INPUT FORMAT
The order of the input lines dictates the order of laying down the rectangles. The first input line is a rectangle "on the bottom".
Line 1: | A, B, and N, space separated (1 <= A,B <= 10,000) |
Lines 2-N+1: | Five integers: llx, lly, urx, ury, color: the lower left coordinates and upper right coordinates of the rectangle whose color is `color' (1 <= color <= 2500) to be placed on the white sheet. The color 1 is the same color of white as the sheet upon which the rectangles are placed. |
SAMPLE INPUT (file rect1.in)
20 20 32 2 18 18 20 8 19 19 38 0 10 19 4
INPUT EXPLANATION
Note that the rectangle delineated by 0,0 and 2,2 is two units wide and two high. Here's a schematic diagram of the input:
1111111111111111111133333333443333333331333333334433333333313333333344333333333133333333443333333331333333334433333333313333333344333333333133333333443333333331333333334433333333313333333344333333333133333333443333333331333333334433333333311122222244222222221111222222442222222211112222224422222222111122222244222222221111222222442222222211112222224422222222111111111144111111111111111111441111111111
The '4's at 8,0 to 10,19 are only two wide, not three (i.e., the grid contains a 4 and 8,0 and a 4 and 8,1 but NOT a 4 and 8,2 since this diagram can't capture what would be shown on graph paper).
OUTPUT FORMAT
The output file should contain a list of all the colors that can be seen along with the total area of each color that can be seen (even if the regions of color are disjoint), ordered by increasing color. Do not display colors with no area.
SAMPLE OUTPUT (file rect1.out)
1 912 843 1874 38
HINTS (use them carefully!)
思路:俗称漂浮法,从最上层开始始逐步向下,而当前层则一直往上飘浮,若碰到长方形则裂成几个长方形。
最后的面积及飘到水面时剩余的面积。
注意点:输入的相同颜色的长方形可能有多块,并且不一定按照颜色递增输入。所以需处理一下在输出。
不足点:思路是我自己想出来的,可是碰到长方形就裂成几个长方形,一直想不明白咋怎,咋裂,就去百度了,看他人的裂法才知道这就漂浮法。
准确的来说这不能算是完全原创。
代码:
/*ID:nealgav1LANG:C++PROG:rect1*/#include#include using namespace std;ifstream cin("rect1.in");ofstream cout("rect1.out");const int mm=2600;class point{ public: int l,r,u,d; int colour,area; point(){area=0;colour=0;}}pos[1200];int ans[mm];int cas;int ce;void kill(int l,int d,int r,int u,int k){ if(k>cas)///漂浮到水面了,可以计算面积了。 { pos[ce].area+=(r-l)*(u-d);return; } if(l==r||u==d)return;///优化剪枝,面积为0的长方形就不用在裂了。 ///四个方向的裂法:1:不裂重复,2,不裂没有的面积。 if(l pos[k].r)kill(max(l,pos[k].r),max(pos[k].d,d),r,max(pos[k].d,u),k+1); if(u>pos[k].u)kill(min(l,pos[k].r),max(d,pos[k].u),min(r,pos[k].r),u,k+1); if(d >m>>n>>cas; for(int i=1;i<=cas;i++) cin>>pos[i].l>>pos[i].d>>pos[i].r>>pos[i].u>>pos[i].colour; pos[0].area=m*n;pos[0].colour=1; for(int i=cas;i>=1;i--) { ce=i;kill(pos[i].l,pos[i].d,pos[i].r,pos[i].u,i+1); pos[0].area-=pos[ce].area; ans[pos[i].colour]+=pos[i].area;///ans[],记录答案,同时省去了排序和,加和处理。 } ans[pos[0].colour]+=pos[0].area; for(int i=0;i
Analysis by Mathijs Vogelzang
A straightforward approach to this problem would be to make an array which represents the table, and then draw all the rectangles on it. In the end, the program can just count the colors from the array and output them. Unfortunately, the maximum dimensions of this problem are 10,000 x 10,000, which means the program uses 100 million integers. That's too much, so we need another approach.
An approach that does work for such large cases (and it actually is a lot faster too) is to keep track of the rectangles, and delete portions of them when they are covered by other rectangles.
Consider this input set:
0 0 10 10 55 0 15 10 10
The program first reads in the first rectangle and puts it in a list. When it reads a new rectangle it checks all items in the list if they overlap with the new rectangle. This is the case, and then it deletes the old rectangle from the list and adds all parts which aren't covered to the list. (So in this case, the program would delete the first rectangle, add 0 0 5 10 5 to the list and then add the second rectangle to the list). ``
If you're unlucky, a new rectangle can create lots of new rectangles (when the new rectangle entirely fits into another one, the program creates four new rectangles which represent the leftover border:
+--------+ +-+--+--+| | | |2 | || | + +--+ || +-+ | --> | | | || +-+ | |1| |3 || | | +--+ || | | | 4| |+--------+ +-+--+--+
This is not a problem however, because there can be only 2500 rectangles and there is plenty of memory, so rectangles have to be cut very much to run out of memory.
Note that with this approach, the only thing that matters is how many rectangles there are and how often they overlap. The maximum dimensions can be as large as you want, it doesn't matter for the running time.
Further Analysis by Tomek Czajka
There is another solution to this problem, which runs in O(n*n*log n) time, but is quite tricky. First, we add one big white rectangle at the bottom - the paper. Then we make two arrays: one containing all vertical edges of the rectangles, and the other the horizontal ones. For each edge we have its coordinates and remember, whether it's the left or right edge (top or bottom). We sort these edges from left to right and from top to bottom. Then we go from left to right (sweep), jumping to every x-coordinate of vertical edges. At each step we update the set of rectangles seen. We also want to update the amount of each color seen so far. So for each x we go from top to bottom, for each y updating the set of rectagles at a point (in the structure described below) and choosing the top one, so that we can update the amounts of colors seen.
The structure to hold the set of rectangles at a point should allow adding a rectangle (number from 1..1000), deleting a rectangle, and finding the top one. We can implement these operations in O(log n) time if we use a heap. To make adding and deleting run in O(log n) we must also have for each rectangle its position in the heap.
So the total time spent at each point is O(log n). Thus the algorithm works in O(n*n*log n) time.
And a solution from mrsigma:
#include#include #include FILE *fp,*fo;struct rect{ int c; int x1,y1,x2,y2;};int c[2501];rect r[10001];int intersect(rect a,const rect &b,rect out[4]){ /* do they at all intersect? */ if(b.x2 =a.x2) return 0; if(b.y2 =a.y2) return 0; /* they do */ rect t; if(b.x1<=a.x1&&b.x2>=a.x2&&b.y1<=a.y1&&b.y2>=a.y2) return -1; /* cutting `a' down to match b */ int nout=0; if(b.x1>=a.x1) { t=a,t.x2=b.x1; if(t.x1!=t.x2) out[nout++]=t; a.x1=b.x1; } if(b.x2 =a.y1) { t=a,t.y2=b.y1; if(t.y1!=t.y2) out[nout++]=t; a.y1=b.y1; } if(b.y2 r[rr].x2) { tmp=r[rr].x1; r[rr].x1=r[rr].x2; r[rr].x2=tmp; } if(r[rr].y1>r[rr].y2) { tmp=r[rr].y1; r[rr].y1=r[rr].y2; r[rr].y2=tmp; } int nr=rr; rect curr=r[rr++]; for(j=0;j 0;) r[rr++]=t[n]; } } for(i=0;i
And another fast solution from Saber Fadaee:
In Shaping Regions, I changed the whole A*B page into (2*N) * (2*N).
Program rrect1; Var Inf,Outf : Text; A,B,N,I,Z,Middle,J : Longint; Color : Array [1..2500] of Longint; D : Array [1..5000,1..5000] of boolean; Xar,Yar : Array [0..2500] of Longint; Col : Array [1..10000] of Record x1 : Longint; x2 : Longint; y1 : Longint; y2 : Longint; c : Longint; End; Function Find (K1 : integer) : Longint; Var Pointer,N1,N2 : Longint; Begin N1 := 1; N2 := N + N; While N1 > 0 Do Begin Pointer := (N1 + N2) Div 2; If Xar[Pointer] = K1 then Begin Find := Pointer; Exit; End; If Xar[Pointer] > K1 Then N2 := Pointer - 1; If Xar[Pointer] < K1 Then N1 := Pointer + 1; End; End; Function Find1 (K2 : Longint) : Longint; Var Pointer,N1,N2 : Longint; Begin N1 := 1; N2 := N + N; While N1 > 0 Do Begin Pointer := (N1 + N2) Div 2; If Yar[Pointer] = K2 then Begin Find1 := Pointer; Exit; End; If Yar[Pointer] > K2 Then N2 := Pointer - 1; If Yar[Pointer] < K2 Then N1 := Pointer + 1; End; End; Procedure Partition1 ( Lf , Rg : Longint ); Var Pivot,L,R,Temp : Longint; Begin Pivot := Yar[Lf]; L := Lf; R := Rg; While L < R Do Begin While (Yar[L] <= Pivot) and (L <= R) Do Inc(L); While (Yar[R] > Pivot) And (R >= L) Do Dec(R); If L < R Then begin Temp := Yar[L]; Yar[L] := Yar[R]; Yar[R] := Temp; end; End; Middle := R; Temp := Yar[Lf]; Yar[Lf] := Yar[R]; Yar[R] := Temp; End; Procedure QSort1 ( Left , Right : Longint ); Begin if Left < Right Then Begin Partition1 (Left,Right); QSort1 (Left,Middle-1); QSort1 (Middle + 1, Right); End; End; Procedure Partition ( Lf , Rg : Longint ); Var Pivot,L,R,Temp : Longint; Begin Pivot := Xar[Lf]; L := Lf; R := Rg; While L < R Do Begin While (Xar[L] <= Pivot) and (L <= R) Do Inc(L); While (Xar[R] > Pivot) And (R >= L) Do Dec(R); If L < R Then begin Temp := Xar[L]; Xar[L] := Xar[R]; Xar[R] := Temp; end; End; Middle := R; Temp := Xar[Lf]; Xar[Lf] := Xar[R]; Xar[R] := Temp; End; Procedure QSort ( Left , Right : Longint ); Begin if Left < Right Then Begin Partition (Left,Right); QSort (Left,Middle-1); QSort (Middle + 1, Right); End; End; Begin Assign (Inf ,'rect1.in'); Reset (Inf); Readln (Inf,A,B,N); For I := 1 To N Do Readln (Inf , Col[I].x1,col[i].y1,col[i].x2,col[i].y2,col[i].c); Close (Inf); For I := 1 to 2500 do Color[I] := 0; Color[1] := A * B; For I := 0 to n do For J := 0 to n do D[I,J] := False; Xar[0] := 0; Xar[n+n+1] := a; For I := 1 To N do Xar[i] := Col[i].x1; For I := N + 1 To N + N do Xar[i] := Col[i - n].x2; Qsort (1,N + N); Yar[0] := 0; Yar[n+n+1] := b; For I := 1 To N Do Yar[i] := Col[i].y1; For I := N + 1 To N + N do Yar[i] := col[i - n].y2; Qsort1 (1,N + N); For I := N Downto 1 Do For J := find(Col[i].x1) + 1 to find(col[i].x2) do For Z := find1(col[i].y1) + 1 to find1(col[i].y2) do If not D[J,Z] then Begin If col[i].c > 1 then Begin Middle := (Xar[j] - Xar[j-1]) * (Yar[z] - Yar[z-1]); Color[Col[i].c] := color[Col[i].c] + Middle; Color[1] := Color[1] - Middle; End; D[J,Z] := True; End; Assign (Outf, 'rect1.out'); Rewrite (Outf); For I := 1 To 2500 Do if color[i] > 0 then Writeln (Outf,i,' ', Color[i]); Close(Outf); End.
And, finally, an unbelievably compact recursive solution from Christoph Roick:
This solution uses recursion. We start with painting the last rectangle and go through to the first (the white paper). We save the edges of every rectangle, because we shouldn't paint over the rectangles above it. Now we have to divide the rectangles in 0 to 4 pieces around the rectangles covering it. In the end we have a lot of small rectangles and the colors can be added to an array by calculating the areas of the rectangle. So, we won't get any problems concerning too less memory.
program rect1;var F: Text; i,a,b,n,cused,maxcolor:word; inform: array[1..1001,1..5] of word; used: array[1..1001,1..4] of word; countcolor: array[1..2500] of longint;procedure cac(count,x1,y1,x2,y2,color: word); //cut and countvar py1,py2 : word;begin if countused[count,3]) or (x2 used[count,4]) or (y2 used[count,2] then py1:=y1 else py1:=used[count,2]; if y2 used[count,4] then cac(succ(count),x1,succ(used[count,4]),x2,y2,color); if x1 used[count,3] then cac(succ(count),succ(used[count,3]),py1,x2,py2,color); end; end else inc(countcolor[color],succ(x2-x1)*succ(y2-y1));end;begin Assign(F,'rect1.in'); Reset(F); Readln(F,a,b,n); inc(n); for i:=2 to n do Readln(F,inform[i,1],inform[i,2],inform[i,3],inform[i,4],inform[i,5]); //x1,y1,x2,y2,color Close(F); inform[1,1]:=0; inform[1,2]:=0; //white paper inform[1,3]:=a; inform[1,4]:=b; inform[1,5]:=1; maxcolor:=1; cused:=1; for i:=n downto 1 do begin cac(1,inform[i,1],inform[i,2],pred(inform[i,3]),pred(inform[i,4]),inform[i,5]); if inform[i,5]>maxcolor then maxcolor:=inform[i,5]; //we don't have to check all 2500 colors used[cused,1]:=inform[i,1]; //saving the coordinates of the rectangle used[cused,2]:=inform[i,2]; used[cused,3]:=pred(inform[i,3]); used[cused,4]:=pred(inform[i,4]); inc(cused); end; Assign(F,'rect1.out'); Rewrite(F); for i:=1 to maxcolor do if countcolor[i]>0 then Writeln(F,i,' ',countcolor[i]); Close(F);end.
Analysis by Mathijs Vogelzang
A straightforward approach to this problem would be to make an array which represents the table, and then draw all the rectangles on it. In the end, the program can just count the colors from the array and output them. Unfortunately, the maximum dimensions of this problem are 10,000 x 10,000, which means the program uses 100 million integers. That's too much, so we need another approach.
An approach that does work for such large cases (and it actually is a lot faster too) is to keep track of the rectangles, and delete portions of them when they are covered by other rectangles.
Consider this input set:
0 0 10 10 55 0 15 10 10
The program first reads in the first rectangle and puts it in a list. When it reads a new rectangle it checks all items in the list if they overlap with the new rectangle. This is the case, and then it deletes the old rectangle from the list and adds all parts which aren't covered to the list. (So in this case, the program would delete the first rectangle, add 0 0 5 10 5 to the list and then add the second rectangle to the list). ``
If you're unlucky, a new rectangle can create lots of new rectangles (when the new rectangle entirely fits into another one, the program creates four new rectangles which represent the leftover border:
+--------+ +-+--+--+| | | |2 | || | + +--+ || +-+ | --> | | | || +-+ | |1| |3 || | | +--+ || | | | 4| |+--------+ +-+--+--+
This is not a problem however, because there can be only 2500 rectangles and there is plenty of memory, so rectangles have to be cut very much to run out of memory.
Note that with this approach, the only thing that matters is how many rectangles there are and how often they overlap. The maximum dimensions can be as large as you want, it doesn't matter for the running time.
Further Analysis by Tomek Czajka
There is another solution to this problem, which runs in O(n*n*log n) time, but is quite tricky. First, we add one big white rectangle at the bottom - the paper. Then we make two arrays: one containing all vertical edges of the rectangles, and the other the horizontal ones. For each edge we have its coordinates and remember, whether it's the left or right edge (top or bottom). We sort these edges from left to right and from top to bottom. Then we go from left to right (sweep), jumping to every x-coordinate of vertical edges. At each step we update the set of rectangles seen. We also want to update the amount of each color seen so far. So for each x we go from top to bottom, for each y updating the set of rectagles at a point (in the structure described below) and choosing the top one, so that we can update the amounts of colors seen.
The structure to hold the set of rectangles at a point should allow adding a rectangle (number from 1..1000), deleting a rectangle, and finding the top one. We can implement these operations in O(log n) time if we use a heap. To make adding and deleting run in O(log n) we must also have for each rectangle its position in the heap.
So the total time spent at each point is O(log n). Thus the algorithm works in O(n*n*log n) time.
And a solution from mrsigma:
#include#include #include FILE *fp,*fo;struct rect{ int c; int x1,y1,x2,y2;};int c[2501];rect r[10001];int intersect(rect a,const rect &b,rect out[4]){ /* do they at all intersect? */ if(b.x2 =a.x2) return 0; if(b.y2 =a.y2) return 0; /* they do */ rect t; if(b.x1<=a.x1&&b.x2>=a.x2&&b.y1<=a.y1&&b.y2>=a.y2) return -1; /* cutting `a' down to match b */ int nout=0; if(b.x1>=a.x1) { t=a,t.x2=b.x1; if(t.x1!=t.x2) out[nout++]=t; a.x1=b.x1; } if(b.x2 =a.y1) { t=a,t.y2=b.y1; if(t.y1!=t.y2) out[nout++]=t; a.y1=b.y1; } if(b.y2 r[rr].x2) { tmp=r[rr].x1; r[rr].x1=r[rr].x2; r[rr].x2=tmp; } if(r[rr].y1>r[rr].y2) { tmp=r[rr].y1; r[rr].y1=r[rr].y2; r[rr].y2=tmp; } int nr=rr; rect curr=r[rr++]; for(j=0;j 0;) r[rr++]=t[n]; } } for(i=0;i
And another fast solution from Saber Fadaee:
In Shaping Regions, I changed the whole A*B page into (2*N) * (2*N).
Program rrect1; Var Inf,Outf : Text; A,B,N,I,Z,Middle,J : Longint; Color : Array [1..2500] of Longint; D : Array [1..5000,1..5000] of boolean; Xar,Yar : Array [0..2500] of Longint; Col : Array [1..10000] of Record x1 : Longint; x2 : Longint; y1 : Longint; y2 : Longint; c : Longint; End; Function Find (K1 : integer) : Longint; Var Pointer,N1,N2 : Longint; Begin N1 := 1; N2 := N + N; While N1 > 0 Do Begin Pointer := (N1 + N2) Div 2; If Xar[Pointer] = K1 then Begin Find := Pointer; Exit; End; If Xar[Pointer] > K1 Then N2 := Pointer - 1; If Xar[Pointer] < K1 Then N1 := Pointer + 1; End; End; Function Find1 (K2 : Longint) : Longint; Var Pointer,N1,N2 : Longint; Begin N1 := 1; N2 := N + N; While N1 > 0 Do Begin Pointer := (N1 + N2) Div 2; If Yar[Pointer] = K2 then Begin Find1 := Pointer; Exit; End; If Yar[Pointer] > K2 Then N2 := Pointer - 1; If Yar[Pointer] < K2 Then N1 := Pointer + 1; End; End; Procedure Partition1 ( Lf , Rg : Longint ); Var Pivot,L,R,Temp : Longint; Begin Pivot := Yar[Lf]; L := Lf; R := Rg; While L < R Do Begin While (Yar[L] <= Pivot) and (L <= R) Do Inc(L); While (Yar[R] > Pivot) And (R >= L) Do Dec(R); If L < R Then begin Temp := Yar[L]; Yar[L] := Yar[R]; Yar[R] := Temp; end; End; Middle := R; Temp := Yar[Lf]; Yar[Lf] := Yar[R]; Yar[R] := Temp; End; Procedure QSort1 ( Left , Right : Longint ); Begin if Left < Right Then Begin Partition1 (Left,Right); QSort1 (Left,Middle-1); QSort1 (Middle + 1, Right); End; End; Procedure Partition ( Lf , Rg : Longint ); Var Pivot,L,R,Temp : Longint; Begin Pivot := Xar[Lf]; L := Lf; R := Rg; While L < R Do Begin While (Xar[L] <= Pivot) and (L <= R) Do Inc(L); While (Xar[R] > Pivot) And (R >= L) Do Dec(R); If L < R Then begin Temp := Xar[L]; Xar[L] := Xar[R]; Xar[R] := Temp; end; End; Middle := R; Temp := Xar[Lf]; Xar[Lf] := Xar[R]; Xar[R] := Temp; End; Procedure QSort ( Left , Right : Longint ); Begin if Left < Right Then Begin Partition (Left,Right); QSort (Left,Middle-1); QSort (Middle + 1, Right); End; End; Begin Assign (Inf ,'rect1.in'); Reset (Inf); Readln (Inf,A,B,N); For I := 1 To N Do Readln (Inf , Col[I].x1,col[i].y1,col[i].x2,col[i].y2,col[i].c); Close (Inf); For I := 1 to 2500 do Color[I] := 0; Color[1] := A * B; For I := 0 to n do For J := 0 to n do D[I,J] := False; Xar[0] := 0; Xar[n+n+1] := a; For I := 1 To N do Xar[i] := Col[i].x1; For I := N + 1 To N + N do Xar[i] := Col[i - n].x2; Qsort (1,N + N); Yar[0] := 0; Yar[n+n+1] := b; For I := 1 To N Do Yar[i] := Col[i].y1; For I := N + 1 To N + N do Yar[i] := col[i - n].y2; Qsort1 (1,N + N); For I := N Downto 1 Do For J := find(Col[i].x1) + 1 to find(col[i].x2) do For Z := find1(col[i].y1) + 1 to find1(col[i].y2) do If not D[J,Z] then Begin If col[i].c > 1 then Begin Middle := (Xar[j] - Xar[j-1]) * (Yar[z] - Yar[z-1]); Color[Col[i].c] := color[Col[i].c] + Middle; Color[1] := Color[1] - Middle; End; D[J,Z] := True; End; Assign (Outf, 'rect1.out'); Rewrite (Outf); For I := 1 To 2500 Do if color[i] > 0 then Writeln (Outf,i,' ', Color[i]); Close(Outf); End.
And, finally, an unbelievably compact recursive solution from Christoph Roick:
This solution uses recursion. We start with painting the last rectangle and go through to the first (the white paper). We save the edges of every rectangle, because we shouldn't paint over the rectangles above it. Now we have to divide the rectangles in 0 to 4 pieces around the rectangles covering it. In the end we have a lot of small rectangles and the colors can be added to an array by calculating the areas of the rectangle. So, we won't get any problems concerning too less memory.
program rect1;var F: Text; i,a,b,n,cused,maxcolor:word; inform: array[1..1001,1..5] of word; used: array[1..1001,1..4] of word; countcolor: array[1..2500] of longint;procedure cac(count,x1,y1,x2,y2,color: word); //cut and countvar py1,py2 : word;begin if countused[count,3]) or (x2 used[count,4]) or (y2 used[count,2] then py1:=y1 else py1:=used[count,2]; if y2 used[count,4] then cac(succ(count),x1,succ(used[count,4]),x2,y2,color); if x1 used[count,3] then cac(succ(count),succ(used[count,3]),py1,x2,py2,color); end; end else inc(countcolor[color],succ(x2-x1)*succ(y2-y1));end;begin Assign(F,'rect1.in'); Reset(F); Readln(F,a,b,n); inc(n); for i:=2 to n do Readln(F,inform[i,1],inform[i,2],inform[i,3],inform[i,4],inform[i,5]); //x1,y1,x2,y2,color Close(F); inform[1,1]:=0; inform[1,2]:=0; //white paper inform[1,3]:=a; inform[1,4]:=b; inform[1,5]:=1; maxcolor:=1; cused:=1; for i:=n downto 1 do begin cac(1,inform[i,1],inform[i,2],pred(inform[i,3]),pred(inform[i,4]),inform[i,5]); if inform[i,5]>maxcolor then maxcolor:=inform[i,5]; //we don't have to check all 2500 colors used[cused,1]:=inform[i,1]; //saving the coordinates of the rectangle used[cused,2]:=inform[i,2]; used[cused,3]:=pred(inform[i,3]); used[cused,4]:=pred(inform[i,4]); inc(cused); end; Assign(F,'rect1.out'); Rewrite(F); for i:=1 to maxcolor do if countcolor[i]>0 then Writeln(F,i,' ',countcolor[i]); Close(F);end.