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 ...
随机推荐
- repeat帮定删除按钮事件,并且生成去人删除提示
前台 <ItemTemplate> <tr> <td> <asp:LinkButton ID="LinkButton_cancel" On ...
- cisco 2950 3550 3750 系列交换机密码破解
破解密码原则:只删除密码 ,不破坏配置#本文中的#号表示注释的意思#第一步. 连接交换机的console口到终端#第二步. 按住交换机面板上的mode键的同时 插入电源,直到sys灯不闪,常亮再松开m ...
- 设置TabBar分栏控制器上图片的大小问题
我们都知道,iOS因为屏幕分辨率的问题,UID在交付我们iOS开发人员程序配图的时候,一般是三套图,分别对应三种不同的分辨率,对不同size的屏幕系统会自动使用不同像素的图片,我们只需要在命名时给三套 ...
- HDU 2802 F(N) 数论+打表
题目大意:f[n]-n^3=f[n-2]-(n-1)^3 (n >=3),f[1]=1,f[2]=7,求f[n]. 题目思路:将n^3移到到等式右边化简的到:f[n]=f[n-2]+3n*(n- ...
- Hadoop RPC机制
RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.Hadoop底层的交互都是通过 rp ...
- oracle-查询执行速度慢的sql
Oracle 查询每天执行慢的SQL 2014-12-11 18:00:04 分类: Oracle 链接:http://blog.itpub.net/28602568/viewspace-136484 ...
- Linux学习 -- 软件包管理
1 软件包类型 源码包 脚本安装包 install.sh 不常用 二进制包(rpm包.系统默认包) RedHat -- rpm包 Debian,Ubuntu -- beb包 2 RPM包命令管理 ...
- FileReader和BufferedReader的区别
1.FileReader不能一行行读 FileReader fr = null; try { fr = new FileReader(new File(path)); StringBuffer str ...
- JSP内置对象--web安全性及config对象的使用 (了解即可)
tomcat服务器配置的时候,在虚拟目录中必须存在一个WEB-INF文件夹,但是访问的时候并不能发现这个文件夹.改成WEB-INFs就可以看到. 所以WEB-INF文件夹不轻易让用户看到,那么其安全性 ...
- jstat undocumented
jstat -J-Djstat.showUnsupported=true -name btrace.com.sun.btrace.samples.ThreadCounter.count 11674 h ...