题目链接

题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度。

分析:

首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin/

沿x轴离散化。和之前的矩阵面积并有点像。

但是一定要去重,否则会错

 #include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL __int64
#define lson l, mid, 2*rt
#define rson mid+1, r, 2*rt+1
const int maxn = +;
using namespace std;
int n, x[maxn];
struct node
{
int l, r, cnt;
int lf, rf;
int num, c; //num是分支的个数,即当前的x段有多少段,可以想象一段是对应两个竖边的。
bool lc, rc; //表示左端是否覆盖,和右端是否覆盖,用来判断x段是否练成一块了
}tr[maxn*];
struct Line
{
int y, x1, x2, f;
}line[maxn];
bool cmp(Line a, Line b)
{
return a.y < b.y;
}
void build(int l, int r, int rt)
{
tr[rt].l = l; tr[rt].r = r;
tr[rt].lf = x[l]; tr[rt].rf = x[r];
tr[rt].cnt = ; tr[rt].num = ;
tr[rt].c = ;
tr[rt].lc = tr[rt].rc = false;
if(tr[rt].l+ == tr[rt].r) return;
int mid = (l+r)/;
build(l, mid, *rt);
build(mid, r, *rt+);
}
void calen(int rt)
{
if(tr[rt].c>)
{
tr[rt].cnt = tr[rt].rf-tr[rt].lf;
tr[rt].num = ;
tr[rt].lc = tr[rt].rc = true; //这整个一块都覆盖了,左右端点自然也覆盖
return;
}
if(tr[rt].l+ == tr[rt].r)
{
tr[rt].cnt = tr[rt].num = ;
tr[rt].lc = tr[rt].rc = false;
}
else
{
tr[rt].cnt = tr[*rt].cnt+tr[*rt+].cnt;
tr[rt].lc = tr[*rt].lc; tr[rt].rc = tr[*rt+].rc;//自己这块没有覆盖,看一下子节点是否有覆盖
tr[rt].num = tr[*rt].num+tr[*rt+].num;
if(tr[*rt].rc && tr[*rt+].lc) tr[rt].num --; //如果当前这个节点的子节点左的右
//覆盖和右的左覆盖说明中间已经练成一片了,所以分支-1
}
}
void update(int rt, Line e)
{
if(tr[rt].lf==e.x1 && tr[rt].rf==e.x2)
{
tr[rt].c += e.f;
calen(rt);
return;
}
if(e.x2<=tr[*rt].rf) update(*rt, e);
else if(e.x1>=tr[*rt+].lf) update(*rt+, e);
else
{
Line tmp;
tmp = e;
tmp.x2 = tr[*rt].rf;
update(*rt, tmp);
tmp = e;
tmp.x1 = tr[*rt+].lf;
update(*rt+, tmp);
}
calen(rt);
}
int main()
{
int i, t, ans, pre;
int x1, x2, y1, y2;
while(~scanf("%d", &n))
{
t = ; ans = ; pre = ;
for(i = ; i < n; i++)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
line[t].x1 = x1; line[t].x2 = x2;
line[t].y = y1; line[t].f = ;
x[t++] = x1;
line[t].x1 = x1; line[t].x2 = x2;
line[t].y = y2; line[t].f = -;
x[t++] = x2;
}
sort(x, x+t);
sort(line, line+t, cmp);
int y = unique(x, x+t)-x; //去重
build(, y-, ); for(i = ; i < t-; i++)
{
update(, line[i]);
ans += tr[].num**(line[i+].y-line[i].y); //竖线的长度
ans += abs(tr[].cnt-pre); //加上 新增的横线 或者 是减少的横线
pre = tr[].cnt;
}
update(, line[i]);
ans += abs(tr[].cnt-pre);
printf("%d\n", ans);
}
return ;
}

poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)的更多相关文章

  1. POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)

    题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...

  2. poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)

    题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...

  3. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  4. POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]

    题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...

  5. HDU 1828 / POJ 1177 Picture --线段树求矩形周长并

    题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...

  6. hdu 4419 线段树 扫描线 离散化 矩形面积

    //离散化 + 扫描线 + 线段树 //这个线段树跟平常不太一样的地方在于记录了区间两个信息,len[i]表示颜色为i的被覆盖的长度为len[i], num[i]表示颜色i 『完全』覆盖了该区间几层. ...

  7. Picture POJ - 1177 (线段树-扫描线)

    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...

  8. POJ 1177 Picture(线段树周长并)

      描述 A number of rectangular posters, photographs and other pictures of the same shape are pasted on ...

  9. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. 【个人笔记】003-PHP基础-01-PHP快速入门-03-PHP环境搭建

    003-PHP基础-01-PHP快速入门 03-PHP环境搭建 1.客户端(浏览器) IE FireFox CHROME Opera Safari 2.服务器 是运行网站的基本 是放置程序代码的地方 ...

  2. 关于sublime text的配置方法

    一个星期没有写博客了, 是时候来一波了 -------------------------------------------------------------------------------- ...

  3. uoj 67 新年的毒瘤 割点

    题目链接: 题目 #67. 新年的毒瘤 问题描述 辞旧迎新之际,喜羊羊正在打理羊村的绿化带,然后他发现了一棵长着毒瘤的树. 这个长着毒瘤的树可以用 nn 个结点 mm 条无向边的无向图表示.这个图中有 ...

  4. DIV+CSS高手必知的15个CSS常识

    1.不要使用过小的图片做背景平铺.这就是为何很多人都不用 1px 的原因,这才知晓.宽高 1px 的图片平铺出一个宽高 200px 的区域,需要 200*200=40, 000 次,占用资源. 2.无 ...

  5. PHP 开发中的外围资源性能分析(一)

    暂且不讨论「PHP 是不是最好的编程语言」,本文我们将分别分析一下在 PHP 程序的后端外围资源和前端外围资源,它们对整个 PHP Web 应用体验的影响,这往往比语言本身大得多. 首先,后端外围资源 ...

  6. mySql 自动备份数据库

    mysqldump -u root -proot -h 192.168.1.100 xqpd > 1.sqlcopy 1.sql D:\项目备份\工程_数据库-%date:~0,4%%date: ...

  7. mahout安装配置

    1.下载mahout 下载地址:http://mahout.apache.org 我下载的最新版:mahout-distribution-0.9 2.把mahout解压到你想存放的文档,我是放在/Us ...

  8. 使用 Storyboard Segue 实作 UIViewController 的切换

    http://blog.csdn.net/mazhen1986/article/details/7791430 Storyboard 是在 iOS 5 SDK 中才出现的新名词,它其实就是原本的 Xi ...

  9. hdu 1333 Smith Numbers

    刚开始没看清题意,要找的数一定要是素数 ;}

  10. js 后台异步执行

    public void AlertMsg(string msg, bool async) { string script = string.Format("alert('{0}'); &qu ...