【HDOJ1828&&POJ1177】Picture(线段树,扫描线)
题意:给定n个矩形,求他们的并的周长
n<=5e3,abs(x[i])<=1e4
思路:From https://www.cnblogs.com/kuangbin/archive/2013/04/10/3013437.html
真实“线段”树
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 11000
#define M 7010
#define eps 1e-8
#define pi acos(-1)
#define oo 1e9
#define MOD 10007 struct node
{
int l,r,cnt,num,c;
bool lc,rc;
}t[N<<]; struct Line
{
int x1,x2,y,f;
}line[N]; bool cmp(Line a,Line b)
{
return a.y<b.y;
} int x[N]; void build(int l,int r,int p)
{
t[p].l=x[l];
t[p].r=x[r];
t[p].cnt=t[p].num=t[p].c=;
t[p].lc=t[p].rc=false;
if(l+==r) return;
int mid=(l+r)>>;
build(l,mid,p<<);
build(mid,r,p<<|);
} void calc(int p)
{
if(t[p].c>)
{
t[p].cnt=t[p].r-t[p].l;
t[p].num=;
t[p].lc=t[p].rc=true;
return;
}
if(t[p].l+==t[p].r)
{
t[p].cnt=t[p].num=;
t[p].lc=t[p].rc=false;
}
else
{
t[p].cnt=t[p<<].cnt+t[p<<|].cnt;
t[p].lc=t[p<<].lc;
t[p].rc=t[p<<|].rc;
t[p].num=t[p<<].num+t[p<<|].num;
if(t[p<<].rc&&t[p<<|].lc) t[p].num--;
}
} void update(int l,int r,Line e,int p)
{
if(t[p].l==e.x1&&t[p].r==e.x2)
{
t[p].c+=e.f;
calc(p);
return;
}
int mid=(l+r)>>;
if(e.x2<=t[p<<].r) update(l,mid,e,p<<);
else if(e.x1>=t[p<<|].l) update(mid,r,e,p<<|);
else
{
Line tmp=e;
tmp.x2=t[p<<].r;
update(l,mid,tmp,p<<);
tmp=e;
tmp.x1=t[p<<|].l;
update(mid,r,tmp,p<<|);
}
calc(p);
} int main()
{
//freopen("poj1177.in","r",stdin);
//freopen("poj1177.out","w",stdout);
int n;
while(scanf("%d",&n)!=EOF)
{
int cnt=;
for(int i=;i<=n-;i++)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[cnt].x1=x1;
line[cnt].x2=x2;
line[cnt].y=y1;
line[cnt].f=;
x[cnt++]=x1;
line[cnt].x1=x1;
line[cnt].x2=x2;
line[cnt].y=y2;
line[cnt].f=-;
x[cnt++]=x2;
}
sort(line,line+cnt,cmp);
sort(x,x+cnt);
int m=unique(x,x+cnt)-x;
build(,m-,); int ans=;
int last=;
for(int i=;i<cnt-;i++)
{
update(,m-,line[i],); ans+=t[].num**(line[i+].y-line[i].y);
ans+=abs(t[].cnt-last);
last=t[].cnt;
}
update(,m-,line[cnt-],);
ans+=abs(t[].cnt-last);
printf("%d\n",ans);
} return ;
}
【HDOJ1828&&POJ1177】Picture(线段树,扫描线)的更多相关文章
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
- POJ1177 Picture 线段树+离散化+扫描线
求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远 /* Problem: 1177 User: 96655 Memory: 348K Time: 32MS Language: C++ Resu ...
- poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)
题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...
- HDU1828 Picture 线段树+扫描线模板题
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 1828 Picture (线段树:扫描线周长)
依然是扫描线,只不过是求所有矩形覆盖之后形成的图形的周长. 容易发现,扫描线中的某一条横边对答案的贡献. 其实就是 加上/去掉这条边之前的答案 和 加上/去掉这条边之后的答案 之差的绝对值 然后横着竖 ...
- Luogu1856 [USACO5.5]矩形周长Picture (线段树扫描线)
对于横轴,加上与上一次扫描的差值:对于竖轴,加上高度差与区间内不相交线段\(*2\)的积: 难点在pushdown,注意维护覆盖关系.再就注意负数 #include <iostream> ...
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
- HDU 1828 Picture (线段树+扫描线)(周长并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...
- 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))
扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...
随机推荐
- ajaxfileuplod 上传文件 essyui laoding 效果,防止重复上传文件
//放于上传前 function ajaxLoading(){ $("<div class=\"datagrid-mask\"></div>&qu ...
- Mysql占用内存过高的优化过程
一.环境说明: 操作系统:CentOS 6.5 x86_64 数据库:Mysql 5.6.22 服务器:阿里云VPS,32G Mem,0 swap 二.问题情况: 1.某日发现公司线上系统的Mysql ...
- mysql安装记录
一.创建mysql用户 useradd mysql 二.解压 tar -zxvf mysql-5.6.38.tar.gz 三.安装依赖包 yum install -y ncurses-devel li ...
- Javascript 模块化指北
前言 随着 Web 技术的蓬勃发展和依赖的基础设施日益完善,前端领域逐渐从浏览器扩展至服务端(Node.js),桌面端(PC.Android.iOS),乃至于物联网设备(IoT),其中 JavaScr ...
- 学习Pytbon第七天,集合
list_1=[5,22,2,6,5,66,6,8] list_1=set(list_1)#把列表转成集合,天生不允许 重复 print(list_1,type(list_1) list_2=set( ...
- Debug调试文件
在debug.h中设置g_debug_switch即可控制调试级别. /* debug.c */ #include "debug.h" const char *get_log_le ...
- git上传自己的代码
感谢这个哥们的博客,不过里面有些错误. http://www.cnblogs.com/ruofengzhishang/p/3842587.html 下面是我自己的实践成功的: 这篇文章写得是windo ...
- 自己用C语言写RH850 F1KM serial bootloader
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 手上有块Renesas ...
- JQuery easyUi datagrid 中 自定义editor作为列表操作按钮列
转自 http://blog.csdn.net/tianlincao/article/details/7494467 前言 JQuery easyUi datagrid 中 使用datagrid生 ...
- 14、响应式布局和BootStrap 全局CSS样式知识点总结-part1
1.什么是响应式布局 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,这个概念是为解决移动互联网浏览而诞生的. 简而言之,就是一个网站能够兼容多个终端——而不是为每个终端做一 ...