求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远

/*
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 线段树+离散化+扫描线的更多相关文章

  1. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  2. hdu1542 矩形面积并(线段树+离散化+扫描线)

    题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...

  3. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  4. Picture POJ - 1177 线段树+离散化+扫描线 求交叉图像周长

    参考  https://www.cnblogs.com/null00/archive/2012/04/22/2464876.html #include <stdio.h> #include ...

  5. POJ1151Atlantis 矩形面积并[线段树 离散化 扫描线]

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21734   Accepted: 8179 Descrip ...

  6. POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21734   Accepted: 8179 Descrip ...

  7. hdu1542线段树+离散化+扫描线

    参考博客: http://blog.csdn.net/xingyeyongheng/article/details/8927732 总的来说就是用一条(假想的)线段去平行x轴从下往上扫描,扫描的过程中 ...

  8. HDU 1542 线段树离散化+扫描线 平面面积计算

    也是很久之前的题目,一直没做 做完之后觉得基本的离散化和扫描线还是不难的,由于本题要离散x点的坐标,最后要计算被覆盖的x轴上的长度,所以不能用普通的建树法,建树建到r-l==1的时候就停止,表示某段而 ...

  9. Codeforces 610D Vika and Segments 线段树+离散化+扫描线

    可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...

随机推荐

  1. mybatis从dao传入多个参数到sqlmap时dao中要使用map或实例对象(如:user)作为参数传入, 否则报错找不到属性getter方法

    23:37 2015-07-02 注意1. 使用mybaits的resultMap查询时, 如果想传入多个参数(比如where 1=1动态多条件查询时)sqlmap文件中对应的方法中, selectL ...

  2. mir9-lua——《热血沙城》45度ARPG手游-Lua移植版

    mir9——<热血沙城>,是9秒论坛开源的一个使用Cocos2d-x-2.2.1引擎开发的45度ARPG手游Demo,源代码为c++.mir9-lua是mir9的Lua移植版,使用Quic ...

  3. 使用Yeoman搭建 AngularJS 应用 (9) —— 让我们搭建一个网页应用

    原文地址:http://yeoman.io/codelab/install-packages.html 列出当前程序包 我们现在查看一下我们已经安装的程序包,输入下面的命令 bower list 查找 ...

  4. springMVC上传图片

    http://blog.csdn.net/cheung1021/article/details/7084673/ http://toutiao.com/a6293854906445021442/ 工程 ...

  5. [转载]vs2012中使用Spring.NET报错:Spring.Context.Support.ContextRegistry 的类型初始值设定项引发异常

    学习使用Spring.NET中的时候,写了一个Demo,在运行时报了一个错误:Spring.Context.Support.ContextRegistry 的类型初始值设定项引发异常. 重新整理思绪, ...

  6. asp 文件上传(无组件上传)

    文件1.上传界面文件 upload.htm<html><head><meta http-equiv="Content-Language" conten ...

  7. 对jQuery.isArray方法的分析

    jQuery.isArray方法应于判断是不是数组,是的话返回true,否则返回false.调用如:jQuery.isArray([]),返回true.其实现源码如下: isArray: Array. ...

  8. hdu 1213

    简单并查集 #include <cstdio> #include <cstring> #define maxn 30005 int fa[maxn],ans[maxn],n,m ...

  9. Unity3D 自动打包整个项目(以AssetBundle实现)

    原地址:http://blog.csdn.net/huang7jiao/article/details/18370653 需求: 在移动开发中,手动控制资源的加载.释放和热更新,是很有必要的. 而Un ...

  10. SQLite入门与分析(四)---Page Cache之事务处理(1)

    写在前面:从本章开始,将对SQLite的每个模块进行讨论.讨论的顺序按照我阅读SQLite的顺序来进行,由于项目的需要,以及时间关系,不能给出一个完整的计划,但是我会先讨论我认为比较重要的内容.本节讨 ...