Picture

 

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

题意
  依次给出矩形左下角和右上角坐标,求若干矩形叠加后的周长。
解题思路
  
分别离散化x轴、y轴,然后做扫描线,一条扫x轴、一条扫y轴,累加即是周长。
//#include<bits/stdc++.h>
#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e4+;
int cnt[maxn*],n;//记录某个区间的下底边比上底边多的个数
double sum[maxn*];//记录某个区间下底边比上底边多的个数总长度
double Hash[][maxn];//对横坐标x离散化 struct node{
double l,r,h;
int f;
node(){}
node(double x1,double x2,double hh,int ff):l(x1),r(x2),h(hh),f(ff){}
bool operator<(const node &a)const{
return h<a.h;
}
}s[][maxn*]; void init(){
memset(sum,,sizeof(sum));
memset(cnt,,sizeof(cnt));
} void pushup(int x,int l,int r,int flag){
if(cnt[x]) sum[x]=Hash[flag][r+]-Hash[flag][l];//表示该区间整个线段长度可作为底边
else if(l==r) sum[x]=;//叶子结点区间长度为0,则底边长度为0
else sum[x]=sum[x*]+sum[x*+];
} void update(int x,int flag,int L,int R,int val,int l,int r){
if(L<=l&&r<=R){
cnt[x]+=val;
pushup(x,l,r,flag);
return ;
}
int mid=(l+r)/;
if(L<=mid) update(x*,flag,L,R,val,l,mid);
if(R>mid) update(x*+,flag,L,R,val,mid+,r);
pushup(x,l,r,flag);
} int main(){
double x1,x2,y1,y2;
while(cin>>n&&n){
init();int k=;
while(n--){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
if(x1>x2) swap(x1,x2);
if(y1>y2) swap(y1,y2);
Hash[][k]=x1; Hash[][k]=y1;
s[][k]=node(x1,x2,y1,);s[][k]=node(y1,y2,x1,);
k++;
Hash[][k]=x2; Hash[][k]=y2;
s[][k]=node(x1,x2,y2,-); s[][k]=node(y1,y2,x2,-);
k++;
}
sort(s[],s[]+k);sort(s[],s[]+k);//把线段按高度h从小到大排序
sort(Hash[],Hash[]+k);sort(Hash[],Hash[]+k);//把x坐标从小到大排序 int ans1=unique(Hash[],Hash[]+k)-Hash[];//去重复端点
int ans2=unique(Hash[],Hash[]+k)-Hash[];
double SUM=,last; init();last=;
for(int i=;i<k;i++){
int l=lower_bound(Hash[],Hash[]+ans1,s[][i].l)-Hash[];
int r=lower_bound(Hash[],Hash[]+ans1,s[][i].r)-Hash[];
update(,,l,r-,s[][i].f,,ans1-);
SUM+=abs(sum[]-last);
last=sum[]; }
init();last=;
for(int i=;i<k;i++){
int l=lower_bound(Hash[],Hash[]+ans2,s[][i].l)-Hash[];
int r=lower_bound(Hash[],Hash[]+ans2,s[][i].r)-Hash[];
update(,,l,r-,s[][i].f,,ans2-);
SUM+=abs(sum[]-last);
last=sum[];
}
printf("%d\n",(int)SUM);
}
return ;
}
												

POJ 1177Picture 扫描线(若干矩形叠加后周长)的更多相关文章

  1. poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)

    题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...

  2. HDU-1828 Picture(扫描线 求矩形并的周长)

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

  3. hdu1828 线段树扫描线求矩形面积的周长

    题意:       给你n个矩形,问你这n个矩形所围成的图形的周长是多少. 思路:       线段树的扫描线简单应用,这个题目我用的方法比较笨,就是扫描两次,上下扫描,求出多边形的上下边长和,然后同 ...

  4. hdu 1542 扫描线求矩形面积的并

    很久没做线段树了 求矩形面积的并分析:1.矩形比较多,坐标也很大,所以横坐标需要离散化(纵坐标不需要),熟悉离散化后这个步骤不难,所以这里不详细讲解了,不明白的还请百度2.重点:扫描线法:假想有一条扫 ...

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

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

  6. hdu 1255 覆盖的面积 (扫描线求矩形交)

    覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]

    题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...

  8. poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)

    题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...

  9. HDU 1828 扫描线(矩形周长并)

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

随机推荐

  1. IVideoWindow 在directshow采集链路中的使用

    dshow中一个完整采集链路一般如下: Capture Device----->SampleGraber ------>Render 如果只要采集原始数据可以不用渲染链路那就如下: Cap ...

  2. 学习node.js 第2篇 介绍node.js 安装

    Node.js - 环境安装配置 如果愿意安装设置Node.js环境,需要计算机上提供以下两个软件: 一.文本编辑器 二.Node.js二进制安装包 文本编辑器 这将用来编写程序代码. 一些编辑器包括 ...

  3. input设置背景透明、placeholder的字体颜色及大小

    1.设置input背景透明: background:rgba(255,255,255,0.1); 前面三个参数为对应的rgb数值,第四个参数为透明度:0~1,0:透明,1:不透明: 2.设置input ...

  4. 无法在正在进行内容生成时调用 StartAt

    刚遇到一个奇怪的问题,用户点击创建销售订单的时候,弹出个 无法在正在进行内容生成时调用 StartAt,查看详细报错. ystem.InvalidOperationException: 无法在正在进行 ...

  5. php 计算 距离

    function getdistance($lng1,$lat1,$lng2,$lat2){ //将角度转为狐度 $radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为 ...

  6. 牛客小白月赛12 J 月月查华华的手机(序列自动机)

    ---恢复内容开始--- 题目来源:https://ac.nowcoder.com/acm/contest/392/J 题意: 题目描述 月月和华华一起去吃饭了.期间华华有事出去了一会儿,没有带手机. ...

  7. TZOJ 1689 Building A New Barn(求平面上有几个其它点求到n个点的曼哈顿距离最小)

    描述 After scrimping and saving for years, Farmer John has decided to build a new barn. He wants the b ...

  8. 333. Largest BST Subtree节点数最多的bst子树

    [抄题]: Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where large ...

  9. node.js中通过dgram数据报模块创建UDP服务器和客户端

    node.js中 dgram 模块提供了udp数据包的socket实现,可以方便的创建udp服务器和客户端. 一.创建UDP服务器和客户端 服务端: const dgram = require('dg ...

  10. 20172325 2018-2019-2 《Java程序设计》第六周学习总结

    20172325 2018-2019-2 <Java程序设计>第六周学习总结 教材学习内容总结 本周学习第十章--树 1.什么是树 (1)树是一种数据结构,与之前学过的栈.队列和列表这些线 ...