3044 矩形面积求并 - Wikioi
2014-04-25 14:14
260 查看
题目描述 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
水题,我只是拿来练习扫描线的
View Code
输入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=110; type node=record l,r,lc,rc,cnt:longint; sum,cover:double; end; var tree:array[0..maxn*4]of node; x:array[0..maxn*2]of double; l,r,c:array[0..maxn*2]of longint; y,lll,rrr:array[0..maxn*2]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)>>1]; 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:=0; cnt:=0; if l=r then begin sum:=x[r+1]-x[r]; lc:=0; rc:=0; exit; end; mid:=(l+r)>>1; lc:=tot+1; build(l,mid); rc:=tot+1; build(mid+1,r); sum:=x[r+1]-x[l]; end; end; procedure sort2(ll,rr:longint); var i,j:longint; z:double; begin i:=ll; j:=rr; z:=y[(ll+rr)>>1]; 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:=1; r:=n*2; while l<>r do begin mid:=(l+r)>>1; if x[mid]=k then exit(mid); if x[mid]>k then r:=mid-1 else l:=mid+1; end; exit(l); end; procedure init; var i:longint; x1,y1,x2,y2:double; begin read(n); if n=0 then halt; ans:=0; tot:=0; for i:=1 to n do begin read(x1,y1,x2,y2); lll[i*2-1]:=x1; rrr[i*2-1]:=x2; y[i*2-1]:=y1; c[i*2-1]:=1; lll[i*2]:=x1; rrr[i*2]:=x2; y[i*2]:=y2; c[i*2]:=-1; x[i*2-1]:=x1; x[i*2]:=x2; end; sort(1,n*2); build(1,n*2-1); for i:=1 to n*2 do begin l[i]:=find(lll[i]); r[i]:=find(rrr[i]); end; sort2(1,n*2); 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>0 then cover:=sum else cover:=tree[lc].cover+tree[rc].cover; exit; end; mid:=(l+r)>>1; if ll<=mid then insert(lc,c); if rr>mid then insert(rc,c); if cnt>0 then cover:=sum else cover:=tree[lc].cover+tree[rc].cover; end; end; procedure work; var i:longint; begin for i:=1 to n*2 do begin if y[i]<>y[i-1] then ans:=ans+tree[1].cover*(y[i]-y[i-1]); ll:=l[i]; rr:=r[i]-1; insert(1,c[i]); end; writeln(ans:0:2); end; begin while true do begin init; work; end; end.
View Code
相关文章推荐
- 【codevs3044】矩形面积求并&&【POJ1151】Atlantis,第一次的扫描线
- codevs 3044_矩形面积求并_离散
- 【codevs 3044】【HDU 1542】【poj 1151】矩形面积并 线段树+‘扫描线’
- 【codevs 3044】 矩形面积求并 【线段树 扫描线 离散化】
- (codevs 3044 矩形面积求并)<可以不用线段树>
- Codevs 题目3044 矩形面积求并(线段树+扫描线+离散化)
- codevs 3044 矩形面积求并
- [codevs3044]矩形面积求并(线段树+扫描线)
- [Codevs] 3044 矩形面积求并(离散化)
- poj1151==codevs 3044 矩形面积求并
- codves 3044 矩形面积求并
- Codevs P3044矩形面积求并
- CodeVS3044矩形面积求并
- 【codevs 3044 矩形面积合并】【poj 1151 Atlantis】【hdu 1542 Atlantis】题意&题解&代码(c++)
- codevs3044 矩形面积求并
- 矩形面积求并(codevs 3044)
- 3044矩形面积求并
- codevs 3044 线段树+离散化+扫描线 (矩形面积并
- [CODEVS 3044] 矩形面积求并
- codevs 3044 矩形面积求并 (扫描线)