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 ...
随机推荐
- 尚未配置为Web项目.指定的本地IIS URL http://localhsst/..要打开项目,需要配置虚拟目录 。是否立即创建虚拟目录 解决
1.编辑.csproj文件 2.修改 UseIIS节点改为false,再次打开程序即可
- JSP导出Excel后身份证后三位为0
JSP导出Excel身份证号码超出Excel最大限制,用科学计数法表示,但后三位为0,修改方式: <style type="text/css">.txt { ...
- fdisk添加磁盘
1. 通过Fdisk查看系统分区详细信息: Fdisk –l 详解: [root@jetsenLin ~]# fdisk -l Disk /dev/sda: 10.7 GB, ...
- HttpWebRequest 抓取页面异常处理办法
抓取页面异常处理办法 public static string GetHtmlTest(string URI) { string fullhtml = null; while (true) { try ...
- 根据XML文件生成XSD文件
在.net开发环境中查找XSD.exe文件,比如我的在C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin目录下,将该路径添加到Path中,打开控制台,到 ...
- Android 使用存放在存assets文件夹下的SQLite数据库
因为这次的项目需要自带数据,所以就就把数据都放到一个SQLite的数据库文件中了,之后把该文件放到了assets文件夹下面.一开始打算每次都从assets文件夹下面把该文件夹拷贝到手机的SD卡或者手机 ...
- Override/implements methods 如何添加
用过Eclipse 的ADT的都知道,要快速添加override或者implements方法,右键---Source---Override/Implements Method... 中文:右键---& ...
- UVA 796 Critical Links (tarjan算法求割边)
这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的-本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小. ...
- php截取中文字符串,英文字符串,中英文字符串长度的方法
今天学习了php函数截取中文字符串,英文字符串,中英文字符串的函数使用方法.对中英文截取方法不理解,此处先做记录. PHP自带的函数如strlen().mb_strlen()都是通过计算字符串所占字节 ...
- 转:Selenium2.0介绍——WebDriver两种驱动浏览器的方式.
如果之前熟悉Selenium RC,理解了Selenium RC是如何工作的,那么,当第一次接触Selenium WebDriver的时候,看到WebDriver居然可以不需要指定远端服务器的IP地址 ...