3044 矩形面积求并 - Wikioi

时间:2024-06-11 10:04:02

题目描述 Description

输入n个矩形,求他们总共占地面积(也就是求一下面积的并)

输入描述 Input Description

可能有多组数据,读到n=0为止(不超过15组)

每组数据第一行一个数n,表示矩形个数(n<=100)

接下来n行每行4个实数x1,y1,x2,y1(0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000),表示矩形的左下角坐标和右上角坐标

输出描述 Output Description

每组数据输出一行表示答案

样例输入 Sample Input

2
    10 10 20 20
    15 15 25 25.5
    0

样例输出 Sample Output

180.00

水题,我只是拿来练习扫描线的

 const
maxn=;
type
node=record
l,r,lc,rc,cnt:longint;
sum,cover:double;
end;
var
tree:array[..maxn*]of node;
x:array[..maxn*]of double;
l,r,c:array[..maxn*]of longint;
y,lll,rrr:array[..maxn*]of double;
n,tot,ll,rr:longint;
ans:double; procedure swap(var x,y:longint);
var
t:longint;
begin
t:=x;x:=y;y:=t;
end; procedure swap(var x,y:double);
var
t:double;
begin
t:=x;x:=y;y:=t;
end; procedure sort(l,r:longint);
var
i,j:longint;
y:double;
begin
i:=l;
j:=r;
y:=x[(l+r)>>];
repeat
while x[i]<y do
inc(i);
while x[j]>y do
dec(j);
if i<=j then
begin
swap(x[i],x[j]);
inc(i);
dec(j);
end;
until i>j;
if i<r then sort(i,r);
if j>l then sort(l,j);
end; procedure build(l,r:longint);
var
now,mid:longint;
begin
inc(tot);
now:=tot;
tree[now].l:=l;
tree[now].r:=r;
with tree[now] do
begin
cover:=;
cnt:=;
if l=r then
begin
sum:=x[r+]-x[r];
lc:=;
rc:=;
exit;
end;
mid:=(l+r)>>;
lc:=tot+;
build(l,mid);
rc:=tot+;
build(mid+,r);
sum:=x[r+]-x[l];
end;
end; procedure sort2(ll,rr:longint);
var
i,j:longint;
z:double;
begin
i:=ll;
j:=rr;
z:=y[(ll+rr)>>];
repeat
while y[i]<z do
inc(i);
while y[j]>z do
dec(j);
if i<=j then
begin
swap(l[i],l[j]);
swap(r[i],r[j]);
swap(y[i],y[j]);
swap(c[i],c[j]);
inc(i);
dec(j);
end;
until i>j;
if i<rr then sort2(i,rr);
if j>ll then sort2(ll,j);
end; function find(k:double):longint;
var
l,r,mid:longint;
begin
l:=;
r:=n*;
while l<>r do
begin
mid:=(l+r)>>;
if x[mid]=k then exit(mid);
if x[mid]>k then r:=mid-
else l:=mid+;
end;
exit(l);
end; procedure init;
var
i:longint;
x1,y1,x2,y2:double;
begin
read(n);
if n= then halt;
ans:=;
tot:=;
for i:= to n do
begin
read(x1,y1,x2,y2);
lll[i*-]:=x1;
rrr[i*-]:=x2;
y[i*-]:=y1;
c[i*-]:=;
lll[i*]:=x1;
rrr[i*]:=x2;
y[i*]:=y2;
c[i*]:=-;
x[i*-]:=x1;
x[i*]:=x2;
end;
sort(,n*);
build(,n*-);
for i:= to n* do
begin
l[i]:=find(lll[i]);
r[i]:=find(rrr[i]);
end;
sort2(,n*);
end; procedure insert(now,c:longint);
var
mid:longint;
begin
with tree[now] do
begin
if (ll<=l) and (rr>=r) then
begin
inc(cnt,c);
if cnt> then cover:=sum
else cover:=tree[lc].cover+tree[rc].cover;
exit;
end;
mid:=(l+r)>>;
if ll<=mid then insert(lc,c);
if rr>mid then insert(rc,c);
if cnt> then cover:=sum
else cover:=tree[lc].cover+tree[rc].cover;
end;
end; procedure work;
var
i:longint;
begin
for i:= to n* do
begin
if y[i]<>y[i-] then ans:=ans+tree[].cover*(y[i]-y[i-]);
ll:=l[i];
rr:=r[i]-;
insert(,c[i]);
end;
writeln(ans::);
end; begin
while true do
begin
init;
work;
end;
end.