矩形周长并

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. [nginx] 网上最全面nginx教程(近100篇文章整理)

    转载:http://bbs.linuxtone.org/thread-25588-1-1.html Nginx基础 1.  nginx安装 2.  nginx 编译参数详解 3.  nginx安装配置 ...

  2. php 获取地址栏参数

    javascript实现: top.location.href 顶级窗口的地址this.location.href 当前窗口的地址 PHP实现: //获取域名或主机地址 echo $_SERVER[' ...

  3. .htaccess重写URL讲解

    使用ThinkPHP和Laravel等框架的都知道,所以的请求都需要经过index.php文件入口,无论你的URI是什么. 当然除了访问的是静态文件或者访问路径的文件真实存在,例如你访问xxx.com ...

  4. MSSQL 字符串XML 合成列

    declare @str varchar(2000) set @str='1,2,3,4,6,8,5,9,10,11,12,13,14,15,16,17,18,19,20,29,30,31,32,33 ...

  5. iOS View 模糊效果(毛玻璃)

    iOS View 模糊效果(毛玻璃)   相关资料 http://stackoverflow.com/questions/18404907/using-gpuimage-to-recreate-ios ...

  6. HDU 1022 Train Problem I 用栈瞎搞

    题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种 ...

  7. UIApplicationDelegate 协议 浅析

    @protocol UIApplicationDelegate<NSObject> @optional - (void)applicationDidFinishLaunching:(UIA ...

  8. sql server 2005导出数据

    */ EXEC sp_configure 'show advanced options', 1 GO */     配置选项 'show advanced options' 已从 0 更改为 1.请运 ...

  9. UVALive 2522 Chocolate(概率DP)

    思路:定义DP方程dp[i][j]标记选到第i个巧克力的时候,桌面上还剩下j个巧克力,状态转移有两个方向,dp[i-1][j-1],dp[i-1]lj+1],分别表示桌面上多了一个和消了一个,乘上需要 ...

  10. js 基础对象一

    JavaScript 通常用于操作 HTML 元素. Document元素 每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页 ...