hdu 1828 Picture(线段树,扫描线)
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.
InputYour 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.OutputYour 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 题解:
扫描线,横着一次,竖着一次,就可以了,
每次是局对值相减,维护一个l,r(这样不需要坐标转化),然后一个cnt表示这条线段被覆盖了多少次,
以及该线段多少区域,被覆盖了多少次,向上更新即可。
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdio> #define N 5007
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if (ch=='-') f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
} int n,top,ans,minn,maxx;
struct Node
{
int a,b,c,d;
}a[N],cl[N*2]; struct date
{
int l,r,sum,cnt;
void init(){sum=cnt=0;}
}tr[40007]; void build(int p,int l,int r)
{
tr[p].init();tr[p].l=l,tr[p].r=r;
if (l==r) return;
int mid=(l+r)>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);
}
bool cmp(Node x,Node y){return x.a<y.a;}
void update(int p)
{
if (tr[p].cnt) tr[p].sum=tr[p].r-tr[p].l+1;
else tr[p].sum=tr[p<<1].sum+tr[p<<1|1].sum;
}
void change(int p,int l,int r,int x,int y,int z)
{
if (l==x&&y==r)
{
tr[p].cnt+=z;
update(p);
return ;
}
int mid=(l+r)>>1;
if (y<=mid) change(p<<1,l,mid,x,y,z);
else if (x>mid) change(p<<1|1,mid+1,r,x,y,z);
else change(p<<1,l,mid,x,mid,z),change(p<<1|1,mid+1,r,mid+1,y,z);
update(p);
}
void solve()
{
build(1,minn,maxx);
sort(cl+1,cl+top+1,cmp);
int last=0;
for (int i=1;i<=top;i++)
{
change(1,minn,maxx,cl[i].b,cl[i].c-1,cl[i].d);
ans+=abs(tr[1].sum-last);
last=tr[1].sum;
}
}
int main()
{
while(~scanf("%d",&n))
{
minn=10000,maxx=-10000;
for (int i=1;i<=n;i++)
{
a[i].a=read(),a[i].b=read(),a[i].c=read(),a[i].d=read();
minn=min(minn,a[i].a),minn=min(minn,a[i].b),minn=min(minn,a[i].c),minn=min(minn,a[i].d);
maxx=max(maxx,a[i].a),maxx=max(maxx,a[i].b),maxx=max(maxx,a[i].c),maxx=max(maxx,a[i].d);
}
for (int i=1;i<=n;i++)
{
cl[++top].a=a[i].b;
cl[top].b=a[i].a,cl[top].c=a[i].c;
cl[top].d=1;
cl[++top].a=a[i].d;
cl[top].b=a[i].a,cl[top].c=a[i].c;
cl[top].d=-1;
}
solve(),top=0;
for (int i=1;i<=n;i++)
{
cl[++top].a=a[i].a;
cl[top].b=a[i].b,cl[top].c=a[i].d;
cl[top].d=1;
cl[++top].a=a[i].c;
cl[top].b=a[i].b,cl[top].c=a[i].d;
cl[top].d=-1;
}
solve();
printf("%d\n",ans);
}
}
hdu 1828 Picture(线段树,扫描线)的更多相关文章
- HDU 1828 Picture (线段树+扫描线)(周长并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...
- HDU 1828 Picture (线段树:扫描线周长)
依然是扫描线,只不过是求所有矩形覆盖之后形成的图形的周长. 容易发现,扫描线中的某一条横边对答案的贡献. 其实就是 加上/去掉这条边之前的答案 和 加上/去掉这条边之后的答案 之差的绝对值 然后横着竖 ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- hdu 1828 Picture(线段树 || 普通hash标记)
http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others) Mem ...
- hdu 1828 Picture(线段树轮廓线)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- 覆盖的面积 HDU - 1255 (线段树-扫描线)模板提
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1& ...
- HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1542 Atlantis (线段树扫描线)
大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...
随机推荐
- java数组实现买彩票(重复则重新遍历查询思想)
package com.wh.shuzu; import java.util.Arrays; /** * 买彩票 * @author 丁璐同学 * 重复则重新遍历查询思想 */ public clas ...
- 用java打印输出九九乘法表
package com.wh.multiplication public class Multiplication Table { public static void main(String[] a ...
- 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...
- [转]为革命保护视力 --- 给 Visual Studio 换颜色
本文转自:http://www.cnblogs.com/stg609/p/3723968.html “为革命,保护视力,预防近视,眼保健操开始......” 这个应该是最老版本的眼保健操了,你听过? ...
- 12.1Java-构造方法
一.构造方法 作用:在new的同时对成员变量赋值,给对象的属性初始化赋值格式:权限 方法名(参数列表){}方法的名字,必须和类的名字完全一致,大小写一致构造方法不许写返回值类型,如void,int 构 ...
- 《effective java》中文第2版 note
,第15条[66]: 为不可变类提供静态工厂, eg : Integer/BigInteger 使用了静态工厂缓存了一些常用的实例, 通常 Integer -128 ~ +127. BigIntege ...
- Windows Phone8.1应用 提交
昨天晚上,试了试把一个WP8.1应用提交到微软商店上去,期间遇到了不少问题,搞到凌晨还没弄好.今天早上,终于把WP8.1应用成功提交上去了. 下面说说如何把WP8.1应用提交商店去.提交WP8.1以及 ...
- mysql提升效率
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- Android(java)学习笔记200:JNI之NDK的概念
1.交叉编译 (1)概念 在一个平台(硬件)和os(软件)环境下,编译出另一种平台和os下可以运行的二进制代码. e.g: 电脑端 ...
- mysql5大引擎之间的区别和优劣之分
数据库引擎介绍 MySQL数据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译MYSQL.在缺省情况下,MYSQL支持三个引擎:ISAM.MYISAM和HEAP.另 ...