POJ1177 Picture 线段树+离散化+扫描线
求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远
/*
Problem: 1177 User: 96655
Memory: 348K Time: 32MS
Language: C++ Result: Accepted
*/
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <algorithm>
using namespace std;
const int maxn=;
struct Node
{
int s,t,num,len,cover;
bool lb,rb;
void change(int o)
{
cover+=o;
if(cover==)len=lb=rb=num=;
else len=t-s,lb=,rb=,num=;
}
} node[maxn<<];
struct Line
{
int x,y1,y2,flag;
void fun(int a,int b,int c,int d)
{
x=a,y1=b,y2=c,flag=d;
}
bool operator<(const Line &e)const
{
if(x==e.x)
return flag>e.flag;
return x<e.x;
}
} line[maxn];
int y[maxn];
void build(int rt,int l,int r)
{
node[rt].s=y[l];
node[rt].t=y[r];
node[rt].num=node[rt].len=node[rt].cover=; if(l+==r)return;
int m=(l+r)>>;
build(rt*,l,m);
build(rt*+,m,r);
}
void update_line(int rt)
{
node[rt].lb=node[rt*].lb;
node[rt].rb=node[rt*+].rb;
node[rt].num=node[rt*].num+node[rt*+].num-node[rt*].rb*node[rt*+].lb;
}
void update_len(int rt)
{
node[rt].len=node[rt*].len+node[rt*+].len;
}
void update(int rt,int l,int r,Line e)
{
if(l+==r)
{
node[rt].change(e.flag);
return;
}
int m=(l+r)>>;
if(e.y1<node[rt*].t)update(rt*,l,m,e);
if(e.y2>node[rt*+].s)update(rt*+,m,r,e);
update_len(rt);
update_line(rt);
}
int main()
{
int n,x1,x2,y1,y2,cnt=,d=;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[++cnt].fun(x1,y1,y2,);
y[cnt]=y1;
line[++cnt].fun(x2,y1,y2,-);
y[cnt]=y2;
}
sort(y+,y++cnt);
sort(line+,line++cnt);
for(int i=; i<=cnt; ++i)
if(y[i]!=y[i-])y[++d]=y[i];
build(,,d);
int perimeter=;
int now_len=;
int now_num=;
for(int i=; i<=cnt; ++i)
{
update(,,d,line[i]);
if(i>)perimeter+=*now_num*(line[i].x-line[i-].x);
perimeter+=abs(node[].len-now_len);
now_num=node[].num;
now_len=node[].len;
}
printf("%d\n",perimeter);
return ;
}
POJ1177 Picture 线段树+离散化+扫描线的更多相关文章
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- hdu1542 矩形面积并(线段树+离散化+扫描线)
题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
- 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)
[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- Picture POJ - 1177 线段树+离散化+扫描线 求交叉图像周长
参考 https://www.cnblogs.com/null00/archive/2012/04/22/2464876.html #include <stdio.h> #include ...
- POJ1151Atlantis 矩形面积并[线段树 离散化 扫描线]
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21734 Accepted: 8179 Descrip ...
- POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21734 Accepted: 8179 Descrip ...
- hdu1542线段树+离散化+扫描线
参考博客: http://blog.csdn.net/xingyeyongheng/article/details/8927732 总的来说就是用一条(假想的)线段去平行x轴从下往上扫描,扫描的过程中 ...
- HDU 1542 线段树离散化+扫描线 平面面积计算
也是很久之前的题目,一直没做 做完之后觉得基本的离散化和扫描线还是不难的,由于本题要离散x点的坐标,最后要计算被覆盖的x轴上的长度,所以不能用普通的建树法,建树建到r-l==1的时候就停止,表示某段而 ...
- Codeforces 610D Vika and Segments 线段树+离散化+扫描线
可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...
随机推荐
- Objective-C程序结构及语法特点
程序文件分为头文件(.h)和实现文件(.m): 使用#import关键字将所需的头文件导入程序,并且可以避免程序重复引用相同的头文件: @autoreleasepool { … } 自动释放池: 符号 ...
- validate[.unobtrusive]和Bootstrap实现tooltip错误提示
validate[.unobtrusive]和Bootstrap实现tooltip错误提示 类似的文章园子里已有,请看这里,个人感觉稍显复杂,日前也打算写一个简单的给项目用,一些关键点记录于此.最终效 ...
- jQuery中的join方法
和JS 中的JOIN 方法一样,将一数组按照JOIN的参数连接起来.比如: var arr = [ "a", "b", "c", " ...
- 让wordpress投稿作者在后台只看到自己的文章
wordpress支持多作者撰写,让更多的人参与网站内容的创建是个不错的想法,UGC(User-generated content)使网站主题更丰富,不同的内容吸引不同的受众,一个好的网站应该多产生U ...
- Android支付接入(四):联通VAC计费
原地址:http://blog.csdn.net/simdanfeg/article/details/9012031 注意事项: 1.联通支付是不需要自己标识软硬计费点的,当平台申请计费点的时候会提交 ...
- jquery类选择器无法取得对象问题原因
<html> <script type="text/javascript" src="jquery-1.9.1.js"></scr ...
- STL,ATL,WTL之间的联系和区别
STL即 Standard Template Library (标准模板库) STL是惠普实验室开发的一系列软件的统称.它是由Alexander Stepanov.Meng Lee和David R M ...
- 【leetcode】Container With Most Water(middle)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Android studio 下的 NDK 配置方法和注意事项
http://blog.csdn.net/u013598660/article/details/47341963
- centos6.5下Zabbix系列之Zabbix安装搭建及汉化
最近在研究zabbix,在整理完成之后就有了写一下总结博客的想法,在我研究zabbix的时候给我很大帮助的是it你好,博客地址http://itnihao.blog.51cto.com/他做的zabb ...