矩形周长并

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的更多相关文章

  1. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  2. HDU 1828 / POJ 1177 Picture --线段树求矩形周长并

    题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...

  3. poj 1177 picture

    题目链接:http://poj.org/problem?id=1177 分析:这道题主要用到了线段树.扫描线以及离散化的相关算法. 离散化 离散化是当数字不多但是范围很大时采用的一种方法,将大区间的数 ...

  4. POJ 1177 Picture(线段树:扫描线求轮廓周长)

    题目链接:http://poj.org/problem?id=1177 题目大意:若干个矩形,求这些矩形重叠形成的图形的轮廓周长. 解题思路:这里引用一下大牛的思路:kuangbin 总体思路: 1. ...

  5. POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)

    题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...

  6. poj 1177 Picture(线段树周长并)

    题目链接:http://poj.org/problem?id=1177 题意:给你n个矩形问你重叠后外边缘总共多长. 周长并与面积并很像只不过是处理的时候是   增加的周长=abs(上一次的线段的长度 ...

  7. POJ 1177 Picture(求周长并)

    题目链接 看的HH的题解..周长有两部分组成,横着和竖着的,横着通过,sum[1] - last来计算,竖着的通过标记,记录有多少段. #include <cstdio> #include ...

  8. poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)

    题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...

  9. poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)

    题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...

随机推荐

  1. 转载--C# PLINQ 内存列表查询优化历程

    http://www.cnblogs.com/dengxi/p/5305066.html 产品中(基于ASP.NET MVC开发)需要经常对药品名称及名称拼音码进行下拉匹配及结果查询.为了加快查询的速 ...

  2. sqlserver低版本生成插入脚本

    --将表数据生成SQL脚本的存储过程 CREATE PROCEDURE dbo.UspOutputData @tablename sysname AS declare @column varchar( ...

  3. spring securiy使用总结

    我们常见的几个功能: 注册后直接登录,并且remember-me这种在网上找到很多注册后登录的,但是remember-me没有.其实解决方案还是看源码比较方便.a. 装载authenticationM ...

  4. Qt学习

    博客 一去丶二三里的博客 http://blog.sina.com.cn/s/articlelist_2801495241_0_4.html

  5. 笨方法学python--第一个程序

    该章主要知识点有: 1 print 打印,有双引号,单引号 2 分析报错信息,积累经验 3 # -*- coding:utf-8 -*-,可以输出汉字 4 井号,# ,注释, 英文名 octothor ...

  6. linux centos下安装g++

    1.查看是否安装 g++ -v 2.命令直接安装 yum install gcc-c++ 3.提示你找不到g++安装包,执行下面命令 yum install gcc-c++ libstdc++-dev ...

  7. 关于Ajax的type为post提交方式出现请求失效问题

    最近碰到这样一个问题,原本一个ajax异步提交数据之前是很好的,很多系统都延用了均未出现任何问题.最近这个版本却出现ajax方式post数据不到后台程序,让我折腾了好几天.起初的ajax是这样写的: ...

  8. eclipse 终极操作技巧

    eclipse作为一个java开发必备软件,从用户体验来说,还是蛮一般的(按照初始设置的话),所以有必要进行一些设置上的改良,加上对一些好用的快捷键的挖掘,能让你用eclipse更加得心应手,事半功倍 ...

  9. OpenGL与vs编程——error C2440: “glMaterialfv”: 无法从“GLfloat”转换为“const GLfloat *”

    void setMaterial(const GLfloat mat_diffuse[4],GLfloat mat_shininess){static const GLfloat mat_specul ...

  10. jquery 功能强大的下拉菜单

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org ...