题目描述 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的更多相关文章

  1. codves 3044 矩形面积求并

    codves  3044 矩形面积求并  题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Desc ...

  2. codevs 3044 矩形面积求并

    3044 矩形面积求并   题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不 ...

  3. poj1151==codevs 3044 矩形面积求并

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21511   Accepted: 8110 Descrip ...

  4. codevs 3044 矩形面积求并 (扫描线)

    /* 之前一直偷懒离散化+暴力做着题 今天搞一下扫描线 自己按照线段树的一般写法写的有些问题 因为不用于以前的区间sum so 题解搬运者23333 Orz~ 去掉了打标记的过程 同时更新区间的时候先 ...

  5. codevs 3044 矩形面积求并 || hdu 1542

    这个线段树的作用其实是维护一组(1维 平面(?) 上的)线段覆盖的区域的总长度,支持加入/删除一条线段. 线段树只能维护整数下标,因此要离散化. 也可以理解为将每一条处理的线段分解为一些小线段,要求每 ...

  6. 【题解】codevs 3044 矩形面积合并

    传送门 3044 矩形面积求并 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下 ...

  7. [Codevs] 矩形面积求并

    http://codevs.cn/problem/3044/ 线段树扫描线矩形面积求并 基本思路就是将每个矩形的长(平行于x轴的边)投影到线段树上 下边+1,上边-1: 然后根据线段树的权值和与相邻两 ...

  8. [codevs3044][POJ1151]矩形面积求并

    [codevs3044][POJ1151]矩形面积求并 试题描述 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行一个数n ...

  9. 矩形面积求并(codevs 3044)

    题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...

随机推荐

  1. 一个WebForm中连接SQL Server的例子

    .cs using System; using System.Collections; using System.ComponentModel; using System.Data; using Sy ...

  2. 计算日期时间 自动加1天 PHP计算闰年 java与PHP时间戳对比区别

    昨天写一个同步数据库的模块  从一个数据库同步到另外一个数据库,因为数据较多,不可能一次性全部搬迁过去,所以就按照每天搬迁! 写了一个 模块,点击加1,只要点击一次,自动从A数据库取出1天的数据, 并 ...

  3. WPF密码框中禁止复制、粘贴

    如题: " Margin="215,32,151,0" > <PasswordBox.CommandBindings> <CommandBindi ...

  4. UI1_应用的程序的生命周期

    // // AppDelegate.m // UI1_应用的程序的生命周期 // // Created by zhangxueming on 15/6/29. // Copyright (c) 201 ...

  5. Mapper映射器

    在两个独立的对象之间建立通信的对象 需要在两个必须相互隔离的子系统间建立通信. 可能是因为无法修改已有的子系统,或者不愿意在两者之间建立依赖关系.甚至不愿意这两个子系统与另一个部件间建立依赖关系. 运 ...

  6. ./configure:command not found 解决方法

    有些下载下来的源码没有MAKEFILE文件,但是会有MAKEFILE.IN 和 configure, MAKEFILE文件则由后两个文件生成. 如果执行: $./configure   提示错误:./ ...

  7. 洛谷 P3368 【模板】树状数组 2

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  8. poj 2010 Moo University - Financial Aid

                                                                                                Moo Univ ...

  9. PSP编程初探 Hello World

    自己有一台PSP2000,玩了这么长时间的游戏,所以打算去探究一下PSP这个平台的程序的构建方式. 在网上搜了很多资料,感觉能用上的不多,毕竟这太小众了,通过自己的探索,总结了一下. 先搭建MinGW ...

  10. 完美解决fixed 水平居中问题

    群里的朋友问的,发现自己没写过:就写了下,原理和网上的fixed上下左右四个角的原理一样! 1.防止页面振动: body{ _background-image: url(about:blank); _ ...