【题目】

Picture

Problem Description
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.

The corresponding boundary is the whole set of line segments drawn in Figure 2.

The vertices of all rectangles have integer coordinates.

Input
Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

0 <= number of rectangles < 5000 
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Please process to the end of file.

Output
Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
Sample Input
7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16
Sample Output
228
Source
 
 
【题意】
  看图就懂,求矩形周长并。
 
【分析】
  很容易想到x和y分开搞,其实就是线段的并咯,但是要注意他有退出的一刻。
  进去和退出都有可能影响答案,但是不影响答案就不需要加进去ans。
 
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define Maxn 100010 struct hp
{
int x1,x2,flag;
double y;
}t[Maxn*];int tl;
struct lsh
{
double x;
int id;
}q[Maxn*];int ql; bool cmp(lsh x,lsh y) {return x.x<y.x;}
bool cmp2(hp x,hp y) {return (x.y==y.y)?(x.flag>y.flag):(x.y<y.y);} int mymin(int x,int y) {return x<y?x:y;} double dis[*Maxn]; struct node
{
int l,r,lc,rc,cnt;
double sm;
}tr[Maxn*];int len;
int build(int l,int r)
{
int x=++len;
tr[x].l=l;tr[x].r=r;
tr[x].sm=tr[x].cnt=;
if(l<r-)
{
int mid=(tr[x].l+tr[x].r)>>;
tr[x].lc=build(l,mid);
tr[x].rc=build(mid,r);
}
else tr[x].lc=tr[x].rc=;
return x;
} void upd(int x)
{
int l=tr[x].l,r=tr[x].r,lc=tr[x].lc,rc=tr[x].rc;
if(tr[x].cnt>=) tr[x].sm=dis[r]-dis[l];
else tr[x].sm=tr[lc].sm+tr[rc].sm;
} void change(int x,int l,int r,int c)
{
if(tr[x].l==l&&tr[x].r==r)
{
tr[x].cnt+=c;
upd(x);
return;
}
int mid=(tr[x].l+tr[x].r)>>;
if(r<=mid) change(tr[x].lc,l,r,c);
else if(l>=mid) change(tr[x].rc,l,r,c);
else
{
change(tr[x].lc,l,mid,c);
change(tr[x].rc,mid,r,c);
}
upd(x);
} double x1[Maxn],ya[Maxn],x2[Maxn],y2[Maxn];
int n;
void init()
{
for(int i=;i<=n;i++) scanf("%lf%lf%lf%lf",&x1[i],&ya[i],&x2[i],&y2[i]);
} double ans; void ffind()
{
tl=;ql=;
for(int i=;i<=n;i++)
{
//t[++tl].x1,
t[++tl].flag=;t[tl].y=ya[i];
t[++tl].flag=-;t[tl].y=y2[i];
q[++ql].x=x1[i],q[ql].id=tl;
q[++ql].x=x2[i],q[ql].id=tl+*n;
}
sort(q+,q++ql,cmp);
int p=;
for(int i=;i<=ql;i++)
{
if(q[i].x!=q[i-].x||p==) p++,dis[p]=dis[p-]+q[i].x-q[i-].x;
if(q[i].id<=*n) t[q[i].id].x1=t[q[i].id-].x1=p;
else t[q[i].id-*n].x2=t[q[i].id--*n].x2=p;
}
sort(t+,t++tl,cmp2);
len=;tr[].sm=;
build(,p);
double now=;
for(int i=;i<=tl;i++)
{
change(,t[i].x1,t[i].x2,t[i].flag);
double nn=tr[].sm;
if(t[i].flag==) ans+=nn-now;
else ans+=now-nn;
now=nn;
}
} int main()
{
int T;
// scanf("%d",&T);
// T=1;
while(scanf("%d",&n)!=EOF)
{ init();
ans=;
ffind();
// printf("%.0lf\n",ans);
for(int i=;i<=n;i++)
{
double tt;
tt=x1[i],x1[i]=ya[i],ya[i]=tt;
tt=x2[i],x2[i]=y2[i],y2[i]=tt;
}
ffind();
printf("%.0lf\n",ans);
}
return ;
}

ws的题目,谁说了多组啊!!!

哦,对了,有一个比较坑的地方【ORZ。。GDXB

矩形边界重合的时候的处理。举个例子,在处理x轴的时候:

中间那条一次也不算。

这个东西,在排序的时候注意一下就好了。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define Maxn 100010 struct hp
{
int x1,x2,flag;
double y;
}t[Maxn*];int tl;
struct lsh
{
double x;
int id;
}q[Maxn*];int ql; bool cmp(lsh x,lsh y) {return x.x<y.x;}
bool cmp2(hp x,hp y) {return (x.y==y.y)?(x.flag>y.flag):(x.y<y.y);} int mymin(int x,int y) {return x<y?x:y;} double dis[*Maxn]; struct node
{
int l,r,lc,rc,cnt;
double sm;
}tr[Maxn*];int len;
int build(int l,int r)
{
int x=++len;
tr[x].l=l;tr[x].r=r;
tr[x].sm=tr[x].cnt=;
if(l<r-)
{
int mid=(tr[x].l+tr[x].r)>>;
tr[x].lc=build(l,mid);
tr[x].rc=build(mid,r);
}
else tr[x].lc=tr[x].rc=;
return x;
} void upd(int x)
{
int l=tr[x].l,r=tr[x].r,lc=tr[x].lc,rc=tr[x].rc;
if(tr[x].cnt>=) tr[x].sm=dis[r]-dis[l];
else tr[x].sm=tr[lc].sm+tr[rc].sm;
} void change(int x,int l,int r,int c)
{
if(tr[x].l==l&&tr[x].r==r)
{
tr[x].cnt+=c;
upd(x);
return;
}
int mid=(tr[x].l+tr[x].r)>>;
if(r<=mid) change(tr[x].lc,l,r,c);
else if(l>=mid) change(tr[x].rc,l,r,c);
else
{
change(tr[x].lc,l,mid,c);
change(tr[x].rc,mid,r,c);
}
upd(x);
} double x1[Maxn],ya[Maxn],x2[Maxn],y2[Maxn];
int n;
void init()
{
for(int i=;i<=n;i++) scanf("%lf%lf%lf%lf",&x1[i],&ya[i],&x2[i],&y2[i]);
} double ans; void ffind()
{
tl=;ql=;
for(int i=;i<=n;i++)
{
//t[++tl].x1,
t[++tl].flag=;t[tl].y=ya[i];
t[++tl].flag=-;t[tl].y=y2[i];
q[++ql].x=x1[i],q[ql].id=tl;
q[++ql].x=x2[i],q[ql].id=tl+*n;
}
sort(q+,q++ql,cmp);
int p=;
for(int i=;i<=ql;i++)
{
if(q[i].x!=q[i-].x||p==) p++,dis[p]=dis[p-]+q[i].x-q[i-].x;
if(q[i].id<=*n) t[q[i].id].x1=t[q[i].id-].x1=p;
else t[q[i].id-*n].x2=t[q[i].id--*n].x2=p;
}
sort(t+,t++tl,cmp2);
len=;tr[].sm=;
build(,p);
double now=;
for(int i=;i<=tl;i++)
{
change(,t[i].x1,t[i].x2,t[i].flag);
double nn=tr[].sm;
if(t[i].flag==) ans+=nn-now;
else ans+=now-nn;
now=nn;
}
} int main()
{
int T;
// scanf("%d",&T);
// T=1;
while(scanf("%d",&n)!=EOF)
{ init();
ans=;
ffind();
// printf("%.0lf\n",ans);
for(int i=;i<=n;i++)
{
double tt;
tt=x1[i],x1[i]=ya[i],ya[i]=tt;
tt=x2[i],x2[i]=y2[i],y2[i]=tt;
}
ffind();
printf("%.0lf\n",ans);
}
return ;
}

2016-11-10 15:49:48

【HDU 1828】 Picture (矩阵周长并,线段树,扫描法)的更多相关文章

  1. HDU 1828 Picture(长方形的周长和)

    HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...

  2. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  5. 51nod 1206 && hdu 1828 Picture (扫描线+离散化+线段树 矩阵周长并)

    1206 Picture  题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 给出平面上的N个矩形(矩形的边平行于X轴 ...

  6. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  7. HDU 1828 Picture(线段树扫描线求周长)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  9. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  10. hdu 1828 Picture(线段树 || 普通hash标记)

    http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others)    Mem ...

随机推荐

  1. [置顶] c#对于文件的操作

    在system.iO:命名空间中提供了两种遍历枚举文件夹的类,Directory 和DirectoryInfo, 他们都有一个方法GetDirectories():来便利枚举文件夹, /// < ...

  2. Linux学习新篇——常用命令和快捷键总结

    最近刚接触Linux,整理了一些常用的命令和快捷键 Tab补全命令 当命令记不清了,输入记得的前几个用Tab就可以将该命令自动补全. 启动tomcat服务用$startup.sh 停止tomcat服务 ...

  3. fedora 23 安装genymotion解决方案

    由于学习android开发,都说genymotion模拟器给力,我就尝试了下,安装过程参考 :但出现这种错误:缺少库 libjpeg.so.8 ,我就各种goole和百度找到库(链接地址),解压之后放 ...

  4. ThinkPHP函数详解:import方法

    import方法是ThinkPHP框架用于类库导入的封装实现,尤其对于项目类库.扩展类库和第三方类库的导入支持,import方法早期的版本可以和java的import方法一样导入目录和通配符导入,后来 ...

  5. ie各个版本hack

    /*类内部hack:*/ .header {_width:100px;} /* IE6专用*/ .header {*+width:100px;} /* IE7专用*/ .header {*width: ...

  6. portal单点登录的原理与实现还有ESB

    portal单点登录的原理与实现还有ESB 在毕业论文中有描述到这一点.我给我出的截图

  7. jasper

    package jasper; import java.util.ArrayList;import java.util.HashMap;import java.util.Map; import net ...

  8. dumpbin工具的使用

    当我们需要查看一个dll或exe文件中的包含的函数或是依赖的函数之类的信息,可以使用Visual Studio自带的工具dumpbin来实现,使用方法为: 1/ 启动Visual Studio 命令行 ...

  9. web services 接口测试方法

    public class Test { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new Jax ...

  10. sql查询结果集根据指定条件排序的方法

    oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 有几种办法改变这种情况: (1)用 nvl 函数或decode 函数 将null ...