图形面积
在一个笛卡尔平面坐标系里(则X轴向右是正方向,Y轴向上是正方向),有N(1 <= N <= 100)个矩形,第i个矩形的左上角坐标是(x1, y1),右下角坐标是(x2,y2)。问这N个矩形所覆盖的面积是多少?注意:被重复覆盖的区域的面积只算一次。
(-10^4 <= x1,y1,x2,y2 <= 10^4)
离散化一下之后暴力统计即可,染的时候要搞清楚s[i,j]是i(j)与前(上)一条线还是后(下)一条线围成的面积。
相当于vijos1056,但是坐标的判重就需要改一下了,很好弄,不再累述。
View Code
1 program planting(input,output);
2 type
3 node = record
4 x1,y1,x2,y2 : longint;
5 end;
6 var
7 x : array[0..200] of node;
8 numberx,numbery : array[0..200] of longint;
9 v : array[-11000..11000] of boolean;
10 mx,my,n : longint;
11 s : array[0..200,0..200] of boolean;
12 answer : longint;
13 procedure init;
14 var
15 i : longint;
16 begin
17 readln(n);
18 fillchar(v,sizeof(v),false);
19 mx:=0;
20 for i:=1 to n do
21 begin
22 readln(x[i].x1,x[i].y1,x[i].x2,x[i].y2);
23 if not v[x[i].x1] then
24 begin
25 inc(mx);
26 v[x[i].x1]:=true;
27 numberx[mx]:=x[i].x1;
28 end;
29 if not v[x[i].x2] then
30 begin
31 inc(mx);
32 v[x[i].x2]:=true;
33 numberx[mx]:=x[i].x2;
34 end;
35 end;
36 fillchar(v,sizeof(v),false);
37 my:=0;
38 for i:=1 to n do
39 begin
40 if not v[x[i].y1] then
41 begin
42 inc(my);
43 v[x[i].y1]:=true;
44 numbery[my]:=x[i].y1;
45 end;
46 if not v[x[i].y2] then
47 begin
48 inc(my);
49 v[x[i].y2]:=true;
50 numbery[my]:=x[i].y2;
51 end;
52 end;
53 end; { init }
54 procedure swap(var aa,bb :longint );
55 var
56 tt : longint;
57 begin
58 tt:=aa;
59 aa:=bb;
60 bb:=tt;
61 end; { swap }
62 procedure sort_x();
63 var
64 i,j : longint;
65 begin
66 for i:=1 to mx-1 do
67 for j:=i+1 to mx do
68 if numberx[j]<numberx[i] then
69 swap(numberx[i],numberx[j]);
70 end; { sort_x }
71 procedure sort_y();
72 var
73 i,j : longint;
74 begin
75 for i:=1 to my-1 do
76 for j:=i+1 to my do
77 if numbery[j]<numbery[i] then
78 swap(numbery[i],numbery[j]);
79 end; { sort_y }
80 function rank_x(value: longint ):longint;
81 var
82 i : longint;
83 begin
84 for i:=1 to mx do
85 if numberx[i]=value then
86 exit(i);
87 end; { rank_x }
88 function rank_y(value :longint ):longint;
89 var
90 i : longint;
91 begin
92 for i:=1 to my do
93 if numbery[i]=value then
94 exit(i);
95 end; { rank_y }
96 procedure main;
97 var
98 i,j,k : longint;
99 sx,sy,ex,ey : longint;
100 begin
101 fillchar(s,sizeof(s),false);
102 for i:=1 to n do
103 begin
104 sx:=rank_x(x[i].x1);
105 sy:=rank_y(x[i].y1);
106 ex:=rank_x(x[i].x2);
107 ey:=rank_y(x[i].y2);
108 if sx>ex then
109 swap(sx,ex);
110 if sy>ey then
111 swap(sy,ey);
112 for j:=sx+1 to ex do
113 for k:=sy+1 to ey do
114 s[j,k]:=true;
115 end;
116 answer:=0;
117 for i:=1 to 200 do
118 for j:=1 to 200 do
119 if (s[i,j]) then
120 inc(answer,abs(numbery[j-1]-numbery[j])*abs(numberx[i-1]-numberx[i]));
121 writeln(answer);
122 end; { main }
123 begin
124 assign(input,'planting.in');reset(input);
125 assign(output,'planting.out');rewrite(output);
126 init;
127 sort_x;
128 sort_y;
129 main;
130 close(input);
131 close(output);
132 end.