3044 矩形面积求并 - Wikioi
题目描述 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.
3044 矩形面积求并 - Wikioi的更多相关文章
- codves 3044 矩形面积求并
codves 3044 矩形面积求并 题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Desc ...
- codevs 3044 矩形面积求并
3044 矩形面积求并 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不 ...
- poj1151==codevs 3044 矩形面积求并
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21511 Accepted: 8110 Descrip ...
- codevs 3044 矩形面积求并 (扫描线)
/* 之前一直偷懒离散化+暴力做着题 今天搞一下扫描线 自己按照线段树的一般写法写的有些问题 因为不用于以前的区间sum so 题解搬运者23333 Orz~ 去掉了打标记的过程 同时更新区间的时候先 ...
- codevs 3044 矩形面积求并 || hdu 1542
这个线段树的作用其实是维护一组(1维 平面(?) 上的)线段覆盖的区域的总长度,支持加入/删除一条线段. 线段树只能维护整数下标,因此要离散化. 也可以理解为将每一条处理的线段分解为一些小线段,要求每 ...
- 【题解】codevs 3044 矩形面积合并
传送门 3044 矩形面积求并 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下 ...
- [Codevs] 矩形面积求并
http://codevs.cn/problem/3044/ 线段树扫描线矩形面积求并 基本思路就是将每个矩形的长(平行于x轴的边)投影到线段树上 下边+1,上边-1: 然后根据线段树的权值和与相邻两 ...
- [codevs3044][POJ1151]矩形面积求并
[codevs3044][POJ1151]矩形面积求并 试题描述 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行一个数n ...
- 矩形面积求并(codevs 3044)
题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...
随机推荐
- android:launchMode概述
android:launchMode An instruction on how the activity should be launched. There are four modes that ...
- JOSN传字符串方法
#region 提示信息 /// <summary> /// 操作失败(无参数) /// </summary> /// <returns></returns& ...
- 【转】K3Cloud 二次开发 单据转换系列
Entity, EntryEntity, SubEntryEntity 这三个对象具有继承关系:Entity 是实体基类,用于定义各种实体的公共属性:EntryEntity 是单据体实体类,从Enti ...
- 常用的CSS缩写语法一些方法小结
使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.css缩写的主要规则如下: 颜色 16进制的色彩值,如果每两位的值相同,可以缩写一半,例如: #000000可以缩写为#000;#336699可以 ...
- OSPF多区域配置
1.配置三台路由器IP R1(config)#INTER S1/0 R1(config-if)#IP ADDress 192.1.12.1 255.255.255.0 R1(config-if)#no ...
- Handoff使用指南 - 理论篇
Handoff简介 Handoff是iOS 8 和 OS X v10.10中引入的功能,可以让同一个用户在多台设备间传递项目.In iOS 9 and OS X v10.11 支持了Spotlight ...
- NSString和NSArray平时练习总结
/*************************字符串练习****************************/ //创建字符串 //1.快速创建 NSString *str1 = @&quo ...
- 【学习笔记】【C语言】函数
一. 什么是函数 任何一个C语言程序都是由一个或者多个程序段(小程序)构成的,每个程序段都有自己的功能,我们一般称这些程序段为“函数”.所以,你可以说C语言程序是由函数构成的. 比如你用C语言编写了一 ...
- VS2013 不能打开DTCMS项目 的解决办法
<system.webServer> <validation validateIntegratedModeConfiguration="false"/> & ...
- 20141111--SQL触发器
---------------------触发器----------------------------- --触发器本质上还是一个存储过程,trigger --只不过不是通过exec调用执行,而是通 ...