POJ1151 Atlantis ——离散优化——Pku1151

分别将x、y坐标排序之后统计有效小矩形即可。这类题目也可以加上线段树优化,可是这道题的数据范围仅仅为n<=100,是可以不用线段树的,直接统计也能AC.

CODE

Program Atlantis;//By_Thispoet

Const

	maxn=300;

Var

	i,j,k,m,n,p,q							:Longint;

	ans										:Extended;

	x,y,hx,hy								:Array[1..maxn]of Extended;

	rankx,ranky								:Array[1..maxn]of Longint;

	rex,rey									:Array[1..maxn,0..1]of Longint;

	flag									:Array[1..maxn,1..maxn]of Boolean;



Procedure Qsortx(l,r:Longint);

var i,j,temp:Longint;

    tmp,k:Extended;

begin

	i:=l;j:=r;k:=hx[(i+j)>>1];

	repeat

		while hx[i]<k do inc(i);

		while hx[j]>k do dec(j);

		if i<=j then

			begin

				temp:=rankx[i];rankx[i]:=rankx[j];rankx[j]:=temp;

				tmp:=hx[i];hx[i]:=hx[j];hx[j]:=tmp;

				inc(i);dec(j);

			end;

	until i>j;

	if l<j then Qsortx(l,j);

	if i<r then Qsortx(i,r);

end;





Procedure Qsorty(l,r:Longint);

var i,j,temp:Longint;

    tmp,k:Extended;

begin

	i:=l;j:=r;k:=hy[(i+j)>>1];

	repeat

		while hy[i]<k do inc(i);

		while hy[j]>k do dec(j);

		if i<=j then

			begin

				temp:=ranky[i];ranky[i]:=ranky[j];ranky[j]:=temp;

				tmp:=hy[i];hy[i]:=hy[j];hy[j]:=tmp;

				inc(i);dec(j);

			end;

	until i>j;

	if l<j then Qsorty(l,j);

	if i<r then Qsorty(i,r);

end;





BEGIN

	readln(n);

	q:=0;

	while n<>0 do

		begin

			inc(q);

			fillchar(flag,sizeof(flag),0);

			for i:=1 to n do

				begin

					readln(x[i],y[i],x[i+n],y[i+n]);

					hx[i]:=x[i];hx[i+n]:=x[i+n];

					hy[i]:=y[i];hy[i+n]:=y[i+n];

					rankx[i]:=i;rankx[i+n]:=i;

					ranky[i]:=i;ranky[i+n]:=i;

				end;

			Qsortx(1,n<<1);

			Qsorty(1,n<<1);

			fillchar(rex,sizeof(rex),255);

			fillchar(rey,sizeof(rey),255);

			for i:=1 to n<<1 do

				begin

					if rey[ranky[i],0]=-1 then rey[ranky[i],0]:=i else rey[ranky[i],1]:=i;

					if rex[rankx[i],0]=-1 then rex[rankx[i],0]:=i else rex[rankx[i],1]:=i;

				end;

			for i:=1 to n do

				for j:=rex[i,0]+1 to rex[i,1] do

					for k:=rey[i,0]+1 to rey[i,1] do

						flag[j,k]:=true;

			ans:=0;

			for i:=2 to n<<1 do

				for j:=2 to n<<1 do

					if flag[i,j] then ans:=ans+(hx[i]-hx[i-1])*(hy[j]-hy[j-1]);

			writeln('Test case #',q);

			writeln('Total explored area: ',ans:0:2);

			writeln;

			readln(n);

		end;

END.

你可能感兴趣的:(ant)