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++ ...
随机推荐
- linux常用命令及安装软件命令
1.查看操作系统是33位还是64最简单的方法 getconf LONG_BIT 或者 uname -a 2.常用命令 2.1基本操作 clear 清屏 2.2安装命令 rpm(redhat packa ...
- python学习笔记5(元组)
一.元组特性 1.类似列表,但不可变类型,正因如此,它可以做一个字典的key2.当处理一组对象时,这个组默认是元组类型3.所有的多对象,逗号分隔的,没有明确用符号定义的这些都默认为元组类型 >& ...
- xcode设置 - App内存暴增
当你发现你的项目中什么没有写,只是启动App内存就飙升到50M甚至60M以上,那么请你接着往下看吧,本文对你绝对非常有用! 1. Enable zombie object: 为了方便我们调试程序,我们 ...
- Delphi XE5 android 捕获几个事件
以下代码能监控到以下几个事件: FinishedLaunching BecameActive WillBecomeInactive EnteredBackground Wi ...
- 汇编之FS段寄存器
FS寄存器指向当前活动线程的TEB结构(线程结构) 偏移 说明 000 指向SEH链指针 004 线程堆栈顶部 008 线程堆栈底部 00C SubSystemTib 010 FiberD ...
- fiddler 插件开发二
本篇主要讲解Fildder插件开发中的涉及到的主要接口与类. 1.IFiddlerExtension 接口 如果要开发的自定义插件有UI界面,则需要实现IFiddlerExtension 接口.你程序 ...
- poj 3301 Texas Trip 三分法
思路:三分法求解凸函数的极值,三分法介绍在这:http://hi.baidu.com/czyuan_acm/item/81b21d1910ea729c99ce33db 很容易就可以推出旋转后的坐标: ...
- Android:ScaleType设置图片
设置例子:ImageViewId.setScaleType(ScaleType.CENTER); ScaleType:设置图片显示方式 效果预览:
- the service mysql56 was not found in the Windows services的解决办法
mysql无法启动,无法改变状态-CSDN论坛-CSDN.NET-中国最大的IT技术社区 http://bbs.csdn.net/topics/390943788 具体描述: 关闭,重启mysql ...
- Power Station POJ 4045
题意:给你一棵树,让你求一点,使该点到其余各点的距离之和最小.如果这样的点有多个,则按升序依次输出. 树型dp #include <cstdio> #include <cstring ...