HDU 1828 POJ 1177 Picture
矩形周长并
POJ上C++,G++都能过,HDU上C++过了,G++WA ,不知道为什么
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std; const int maxn=;
struct Seg
{
int x;
int Y1,Y2;//离散化之后的坐标
int flag;
}s[maxn];
int X1[maxn],X2[maxn],Y1[maxn],Y2[maxn];
map<int ,int>m;
int M[maxn];int k;
int n,tot;
int sum,ans; struct SegTree
{
int len;
int cover;
} segTree[maxn*]; bool cmp(const Seg&a,const Seg&b)
{
return a.x<b.x;
} void lsh1()
{
k=;
m.clear();
for(int i=; i<=n; i++)
{
if(m[Y1[i]]==) M[k++]=Y1[i],m[Y1[i]]=;
if(m[Y2[i]]==) M[k++]=Y2[i],m[Y2[i]]=;
}
sort(M,M+k);
m.clear();
for(int i=; i<k; i++) m[M[i]]=i;
} void lsh2()
{
k=;
m.clear();
for(int i=; i<=n; i++)
{
if(m[X1[i]]==) M[k++]=X1[i],m[X1[i]]=;
if(m[X2[i]]==) M[k++]=X2[i],m[X2[i]]=;
}
sort(M,M+k);
m.clear();
for(int i=; i<k; i++) m[M[i]]=i;
} void build(int l,int r,int rt)
{
segTree[rt].cover=;
segTree[rt].len=;
if(l==r) return;
int m=(l+r)/;
build(l,m,*rt);
build(m+,r,*rt+);
} void pushUp(int rt,int l,int r)
{
if(segTree[rt].cover) segTree[rt].len=M[r]-M[l-];
else segTree[rt].len=segTree[*rt].len+segTree[*rt+].len;
} void update(int info,int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
segTree[rt].cover=segTree[rt].cover+info;
pushUp(rt,l,r);
return;
} int m=(l+r)/;
if(L<=m) update(info,L,R,l,m,*rt);
if(R>m) update(info,L,R,m+,r,*rt+);
pushUp(rt,l,r);
} int main()
{
int Case=;
while(~scanf("%d",&n))
{
if(n==)
{
printf("0\n");
continue;
}
for(int i=; i<=n; i++)
scanf("%d%d%d%d",&X1[i],&Y1[i],&X2[i],&Y2[i]); lsh1(); tot=;
for(int i=; i<=n; i++)
{
s[tot].x=X1[i],s[tot].Y1=m[Y1[i]],s[tot].Y2=m[Y2[i]],s[tot].flag=,tot++;
s[tot].x=X2[i],s[tot].Y1=m[Y1[i]],s[tot].Y2=m[Y2[i]],s[tot].flag=-,tot++;
}
sort(s,s+tot,cmp); ans=; build(,k,); for(int i=; i<tot; i++)
{
int sum1=segTree[].len;
update(s[i].flag,s[i].Y1+,s[i].Y2,,k,);
int sum2=segTree[].len;
ans=ans+abs(sum2-sum1);
} lsh2(); tot=;
for(int i=; i<=n; i++)
{
s[tot].x=Y1[i],s[tot].Y1=m[X1[i]],s[tot].Y2=m[X2[i]],s[tot].flag=,tot++;
s[tot].x=Y2[i],s[tot].Y1=m[X1[i]],s[tot].Y2=m[X2[i]],s[tot].flag=-,tot++;
}
sort(s,s+tot,cmp); build(,k,); for(int i=; i<tot; i++)
{
int sum1=segTree[].len;
update(s[i].flag,s[i].Y1+,s[i].Y2,,k,);
int sum2=segTree[].len;
ans=ans+abs(sum2-sum1);
} printf("%d\n",ans);
}
return ;
}
HDU 1828 POJ 1177 Picture的更多相关文章
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- HDU 1828 / POJ 1177 Picture --线段树求矩形周长并
题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...
- poj 1177 picture
题目链接:http://poj.org/problem?id=1177 分析:这道题主要用到了线段树.扫描线以及离散化的相关算法. 离散化 离散化是当数字不多但是范围很大时采用的一种方法,将大区间的数 ...
- POJ 1177 Picture(线段树:扫描线求轮廓周长)
题目链接:http://poj.org/problem?id=1177 题目大意:若干个矩形,求这些矩形重叠形成的图形的轮廓周长. 解题思路:这里引用一下大牛的思路:kuangbin 总体思路: 1. ...
- POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
- poj 1177 Picture(线段树周长并)
题目链接:http://poj.org/problem?id=1177 题意:给你n个矩形问你重叠后外边缘总共多长. 周长并与面积并很像只不过是处理的时候是 增加的周长=abs(上一次的线段的长度 ...
- POJ 1177 Picture(求周长并)
题目链接 看的HH的题解..周长有两部分组成,横着和竖着的,横着通过,sum[1] - last来计算,竖着的通过标记,记录有多少段. #include <cstdio> #include ...
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
- poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)
题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...
随机推荐
- QQ 自动接收远程连接之关闭了远程桌面
之前使用都好好的,后来就不知道怎么了突然就不行了,在另外一个远程桌面软件(向日葵)失效后,木有办法,查查查,终于查出来了,是我本机的时间服务停止了,导致我本机的时间和服务器时间不一致,所以连接不上.只 ...
- 实参时丢弃了类型 discards qualifiers discards qualifiers问题
百思不得其解,于是百度,google吧.. 发现Stackoverflow上也有人有相同的问题 下面是他的问题: For my compsci class, I am implementing a S ...
- RichTextEditor 文本编辑
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- mongodb分片认证
启动configsvr 1. 确保mongdb的configsvr是采用service模式启动的,即从/etc/init.d下的脚本启动的,其用户是mongod. 2. 确保mongod的配置文件完全 ...
- JSTL标签库---SUN公司开发的标签库
JSTL里的标签包含五大类标签: 核心标签库 国际化标签 JSTL函数(EL函数) 数据库标签// 操作数据库的,用不到了 XML标签//操作XML的,用不到了 都在jstl.jar,standar. ...
- eclipse的调试方法的简单介绍
声明:本文不是自己 作为编程人员,程序的调试是一项基本功.在不使用IDE的时候,程序的调试多数是通过日志或者输入语句(System.out.println)的方式.可以把程序运行的轨迹或者程序运行过程 ...
- 【转】Java 内部类种类及使用解析
Java 内部类种类及使用解析 内部类Inner Class 将相关的类组织在一起,从而降低了命名空间的混乱. 一个内部类可以定义在另一个类里,可以定义在函数里,甚至可以作为一个表达式的一部分. Ja ...
- oracle 常用sql语句
oracle 常用sql语句 1.查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_sizefrom d ...
- AI 人工智能 探索 (二)
完整被动技能代码 using UnityEngine; using System.Collections; public class AI : MonoBehaviour { private Hash ...
- parseint和parsefloat总结number。隐形转换
parseint:会认识一些字符+.-.空格,其他的就会截止譬如23hudhchauch结果为:23,对于boollen类型不能转换为1或是0. number:是对整体的转换.对true的转换为1. ...