POJ 1177 矩形周长并 模板
Picture
题目链接
http://poj.org/problem?id=1177
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.
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
题意
经典入门题:求矩形周长并。
题解
我不知道别人怎么做的,我要扫四边,上下左右各扫一遍,具体说说从下往上扫,没扫到一条下边,就判断他下面有没有下边遮挡,减去遮挡长度就是这条下边所能贡献的有效长度;遇到上边,就把对应的下边去掉就好了。如果学过矩形面积并的话,应该很好理解。
代码
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 100050
ll ans;
int n,m,kth[N],cnt1,cnt2,num;
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
struct Query
{
int l,r,h,id;
bool operator<(const Query&e)const
{return h<e.h;};
}que[3][N];
struct Node{int l,r,lazy,sum;};
struct segmentTree
{
Node tr[N<<2];
void push_up(int x);
void bt(int x,int l,int r);
void update(int x,int l,int r,int tt);
int query(int x,int l,int r);
}seg;
void segmentTree::push_up(int x)
{
int len=kth[tr[x].r+1]-kth[tr[x].l];
if (tr[x].l<tr[x].r)tr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum;
else tr[x].sum=0;
if (tr[x].lazy>0)tr[x].sum=len;
}
void segmentTree::bt(int x,int l,int r)
{
tr[x]={l,r,0,0};
if (l==r)return;
int mid=(l+r)>>1;
bt(x<<1,l,mid);
bt(x<<1|1,mid+1,r);
}
void segmentTree::update(int x,int l,int r,int tt)
{
if (l<=tr[x].l&&tr[x].r<=r)
{
tr[x].lazy+=tt;
push_up(x);
return;
}
int mid=(tr[x].l+tr[x].r)>>1;
if(l<=mid)update(x<<1,l,r,tt);
if(mid<r)update(x<<1|1,l,r,tt);
push_up(x);
}
int segmentTree::query(int x,int l,int r)
{
if (l<=tr[x].l&&tr[x].r<=r)return tr[x].sum;
if (tr[x].lazy>0)return kth[r+1]-kth[l];
int mid=(tr[x].l+tr[x].r)>>1,ans=0;
if (l<=mid)ans+=query(x<<1,max(tr[x].l,l),min(mid,r));
if (mid<r)ans+=query(x<<1|1,max(mid+1,l),min(tr[x].r,r));
return ans;
}
void solve(Query *que,int cnt)
{
seg.bt(1,1,num);
sort(que+1,que+cnt+1);
for(int i=1;i<=cnt-1;i++)
{
int l=lower_bound(kth+1,kth+num+1,que[i].l)-kth;
int r=lower_bound(kth+1,kth+num+1,que[i].r)-kth-1;
if (que[i].id==1)
ans+=1LL*(kth[r+1]-kth[l]-seg.query(1,l,r));
seg.update(1,l,r,que[i].id);
}
}
void work()
{
n=20050;
read(m);
for(int i=1;i<=m;i++)
{
int x1,y1,x2,y2;
read(x1); read(y1); read(x2); read(y2);
que[1][++cnt1]=Query{x1,x2,y1,1};
que[1][++cnt1]=Query{x1,x2,y2,-1};
que[2][++cnt2]=Query{y1,y2,x1,1};
que[2][++cnt2]=Query{y1,y2,x2,-1};
kth[++num]=x1;
kth[++num]=x2;
kth[++num]=y1;
kth[++num]=y2;
}
sort(kth+1,kth+num+1);
num=unique(kth+1,kth+num+1)-kth-1;//
solve(que[1],cnt1);
solve(que[2],cnt2);
for(int i=1;i<=cnt1;i++)que[1][i].h=-que[1][i].h,que[1][i].id=-que[1][i].id;
for(int i=1;i<=cnt2;i++)que[2][i].h=-que[2][i].h,que[2][i].id=-que[2][i].id;
solve(que[1],cnt1);
solve(que[2],cnt2);
printf("%lld\n",ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
work();
}
POJ 1177 矩形周长并 模板的更多相关文章
- HDU 1828 / POJ 1177 Picture --线段树求矩形周长并
题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- HDU 1828 POJ 1177 Picture
矩形周长并 POJ上C++,G++都能过,HDU上C++过了,G++WA ,不知道为什么 #include<cstdio> #include<cstring> #include ...
- POJ - 1177 线段树
POJ - 1177 扫描线 这道题也算是一道扫描线的经典题目了. 只不过这道题是算周长,非常有意思的一道题.我们已经知道了,一般求面积并,是如何求的,现在我们要把扫描线进行改造一下,使得能算周长. ...
- 25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有
package zhongqiuzuoye; //自己写的方法 public class Rect { public double width; public double height; Rect( ...
- HDU 1828 扫描线(矩形周长并)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- poj 1113 凸包周长
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33888 Accepted: 11544 Descriptio ...
- HDU 6362(求椭圆中矩形周长的期望 数学)
题意是给定一个椭圆标准方程的a,b(椭圆的长半轴长和短半轴长),在[0,b]内取一个数,则过点(0,b)且平行于x轴的直线与椭圆交于两点,再将此两点关于x轴做对称点,顺次连接此四点构成矩形,求出这些矩 ...
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
随机推荐
- 在Android中使用OpenGL ES开发第(四)节:相机预览
笔者之前写了三篇Android中使用OpenGL ES入门级的文章,从OpenGL ES的相关概念出发,分析了利用OpenGL ES实现3D绘图的重要的两个步骤:定义形状和绘制形状,简单的绘制了一个三 ...
- java编写网站数据抓取
来公司已经俩月了,每天加班平均工时11个小时的我又想起了老东家温馨舒适安逸的生活.已经有好久没时间读博客写博客了,我觉得我退步了,嗯嗯,我很不开心 今天记录下抓数据的一些东西吧. 数据抓取现在是很普遍 ...
- 2016百度之星资格赛 Problem A(前缀积与求逆元)
题意:给出一个字符串,每次询问给出x和y要求算出从x到y的每个字符的(ASCII 码值-28)的值的积(mod9973). 分析:首先的想法肯定是算出每个位置的前缀积,然后只要F[y]/F[x-1]即 ...
- Java并发指南13:Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析
Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析 转自https://www.javadoop.com/post/hashmap#toc7 部分内容转自 http: ...
- sqli-labs通关记录
环境搭建:https://www.cnblogs.com/kagari/p/11910749.html 总体感受:sqli-labs还是只适合入门 在此基础上添加了一个flag数据库,库名flag,表 ...
- Android自定义控件之基本图形绘制
入门示例代码 在Android中Paint类就是画笔,Canvas 就是画布 对于画笔的大小,粗细,颜色,透明度都在paint类中设置,对于画出的成品,比如圆,方形,等在canvas类中的函数生成 入 ...
- Qt编写数据可视化大屏界面电子看板11-自定义控件
一.前言 说到自定义控件,我是感觉特别熟悉的几个字,本人亲自原创的自定义控件超过110个,都是来自各个行业的具体应用真实需求,而不是凭空捏造的,当然有几个小控件也有点凑数的嫌疑,在编写整个数据可视化大 ...
- SpringMVC接收集合页面参数
SpringMVC接收集合页面参数 Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctyp ...
- DrawRect 内存问题
DrawRect实现画板将会大量产生内存问题. 具体请看:http://bihongbo.com/2016/01/03/memoryGhostdrawRect/ 可能的原因有两个: 一是在手指绘制的过 ...
- node.js运行内存堆溢出的解决办法
我是在将一组80多列13万多行的数据通过node-xlsx的时候出现的内存堆溢出的情况. 解决办法时将: node app.js 改成: node --max_old_space_size=10000 ...