Picture poj1177
Picture
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 12265 | Accepted: 6484 |
Description
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
0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
Output
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
#include <stdio.h>
#include <algorithm>
#define LEN 10000
using namespace std; struct Node
{
int left;
int right;
int count;//被覆盖次数
int line;//所包含的区间数量
int lbd;//左端点是否被覆盖
int rbd;//右端点是否被覆盖
int m;//测度,即覆盖的区间长度,如[2,8]就为6
}; struct ScanLine
{
int x;//横坐标
int y1;//扫描线的下端点
int y2;//扫描线的上端点
int flag;//若该扫描线属于矩形的左边的竖边,
//如AB,则叫做入边,值为1,若属于矩形的右边的竖边,如CD,则叫做出边,值为0
}; struct Node node[LEN*];
struct ScanLine scan[LEN];
int y[LEN]; void build(int l, int r, int i)
{
node[i].left = l;
node[i].right = r;
node[i].count = ;
node[i].m = ;
node[i].line = ;
if (r - l > )
{
int middle = (l + r)/;
build(l, middle, *i + );
build(middle, r, *i + );
}
} //更新测度m
void update_m(int i)
{
if (node[i].count > )
node[i].m = y[node[i].right] - y[node[i].left];
else if (node[i].right - node[i].left == )
node[i].m = ;
else
{
node[i].m = node[*i + ].m + node[*i + ].m;
}
} //更新line
void update_line(int i)
{
if (node[i].count > )
{
node[i].lbd = ;
node[i].rbd = ;
node[i].line = ;
}
else if (node[i].right - node[i].left == )
{
node[i].lbd = ;
node[i].rbd = ;
node[i].line = ;
}
else
{
node[i].lbd = node[*i + ].lbd;
node[i].rbd = node[*i + ].rbd;
node[i].line = node[*i + ].line + node[*i + ].line
- node[*i + ].rbd*node[*i + ].lbd;
}
} void insert(int l, int r, int i)//l和r分别是这条扫描线的下端点和上端点的纵坐标
//相当于区间修改
{
//在这里要取离散化之前的原值进行比较
if (y[node[i].left] >= l && y[node[i].right] <= r)
//如果这个区间内最下的点的纵坐标大于insert扫描线的上端点的纵坐标
//且这个区间内最上的点的纵坐标小于insert扫描线的下端点的纵坐标
(node[i].count)++;//这个区间包含于这条线段,一定覆盖了这个区间内的扫描线
else if (node[i].right - node[i].left == )return;//因为坐标都是整数
else
{
int middle = (node[i].left + node[i].right)/;
if (r <= y[middle])//这条扫描线完全包含于这个区间的中点到最下
insert(l, r, *i + );
else if (l >= y[middle])//完全包含于这个区间的最上到中点
insert(l, r, *i + );
else//穿过中点
{
insert(l, y[middle], *i + );
insert(y[middle], r, *i + );
}
}
update_m(i);
update_line(i);
} void remove(int l, int r, int i)
{
//在这里要取离散化之前的原值进行比较
if (y[node[i].left] >= l && y[node[i].right] <= r)
(node[i].count)--;//完全被包含就删去这条边
else if (node[i].right - node[i].left == )
return;
else
{
int middle = (node[i].left + node[i].right)/;
if (r <= y[middle])
remove(l, r, *i + );
else if (l >= y[middle])
remove(l, r, *i + );
else
{
remove(l, y[middle], *i + );
remove(y[middle], r, *i + );
}
}
update_m(i);
update_line(i);
} bool cmp(struct ScanLine line1, struct ScanLine line2)
{
if (line1.x == line2.x)
return line1.flag > line2.flag;
return (line1.x < line2.x);
} int main()
{
int n;
scanf("%d", &n);//输入有几个矩形
int x1, y1, x2, y2;
int i = ;
while (n--)
{
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);//(x1,y1)左下,(x2,y2)右上
scan[i].x = x1;//加扫描线
scan[i].y1 = y1;
scan[i].y2 = y2;
scan[i].flag = ;
y[i++] = y1;//y[]记录所有点的纵坐标
scan[i].x = x2;
scan[i].y1 = y1;
scan[i].y2 = y2;
scan[i].flag = ;
y[i++] = y2;
}
sort(y, y + i);//所有纵坐标从下到上排序
sort(scan, scan + i, cmp);//所有扫描线从左到右排序
int unique_count = unique(y, y + i) - y;//y数组中不重复的个数
build(, unique_count - , );//离散化,建立线段树 int perimeter = ;
int now_m = ;
int now_line = ; for (int j = ; j < i; j++)//枚举每条扫描线
{
if (scan[j].flag)//如果是左边
insert(scan[j].y1, scan[j].y2, );
else//如果是右边
remove(scan[j].y1, scan[j].y2, );
if (j >= )
perimeter += *now_line*(scan[j].x - scan[j-].x);
perimeter += abs(node[].m - now_m);
now_m = node[].m;
now_line = node[].line;
} printf("%d\n", perimeter);
return ;
}
Picture poj1177的更多相关文章
- [POJ1177]Picture
[POJ1177]Picture 试题描述 A number of rectangular posters, photographs and other pictures of the same sh ...
- 【poj1177】 Picture
http://poj.org/problem?id=1177 (题目链接) 题意 求矩形周长并. Solution 转自:http://www.cnblogs.com/Booble/archive/2 ...
- poj1177 Picture 矩形周长并
地址:http://poj.org/problem?id=1177 题目: Picture Time Limit: 2000MS Memory Limit: 10000K Total Submis ...
- 【HDOJ1828&&POJ1177】Picture(线段树,扫描线)
题意:给定n个矩形,求他们的并的周长 n<=5e3,abs(x[i])<=1e4 思路:From https://www.cnblogs.com/kuangbin/archive/2013 ...
- POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...
- CQOI2005 三角形面积并 和 POJ1177 Picture
1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 1664 Solved: 443[Submit][Stat ...
- POJ1177 Picture 线段树+离散化+扫描线
求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远 /* Problem: 1177 User: 96655 Memory: 348K Time: 32MS Language: C++ Resu ...
- POJ-1177 Picture 矩形覆盖周长并
题目链接:http://poj.org/problem?id=1177 比矩形面积并麻烦点,需要更新竖边的条数(平行于x轴扫描)..求横边的时候,保存上一个结果,加上当前长度与上一个结果差的绝对值就行 ...
- IOI1998 hdu1828 poj1177 Picture
写了一发扫描线竟然狂WA不止,hdu死活过不了,poj和当时IOI的数据(还花了我1dsdn积分..)都过了. 然后看到谋篇blog里有评论,把数据拿下来发现WA了. 数据是 20 0 1 11 0 ...
随机推荐
- 扫盲--.net 程序集
前言:用了几天的时间把高级编程里面程序集一章看完了,原来自己只知道写代码,右键添加引用,从来也不知道操作的实质是什么,微软总是这个套路,鼠标点点就能把任务完成,这对新手友好但是对要通透了解程序执行和内 ...
- 第三届蓝桥杯决赛c++b组
1.星期几 [结果填空] (满分5分) 1949年的国庆节(10月1日)是星期六. 今年(2012)的国庆节是星期一. 那么,从建国到现在,有几次国庆节正好是星期日呢? 只要 ...
- 英特尔® Software Guard Extensions 教程系列:第一部分,英特尔® SGX 基础
英特尔® Software Guard Extensions 教程系列:第一部分,英特尔® SGX 基础 from:https://software.intel.com/zh-cn/articles/ ...
- SpringBoot_01_正确、安全地停止SpringBoot应用服务
二.参考资料 1.正确.安全地停止SpringBoot应用服务
- linux命令学习笔记(45):free 命令
free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的 工具中,free命令是最经常使用的命令之一. .命令格式: free [参 ...
- 重学JAVA基础(二):Java反射
看一下百度的解释: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息 ...
- C# Linq 取得两个列表的交集
我们经常会用到linq 来查询 一个数组中和另一个数组中相同的项, 这个时候就会用到IEqualityComparer接口. public class StudyInfoModel { pu ...
- .NETFramework:DateTimeOffset
ylbtech-.NETFramework:DateTimeOffset 表示一个时间点,通常相对于协调世界时(UTC)的日期和时间来表示. 1.程序集 mscorlib, Version=4.0.0 ...
- 资源:template
ylbtech-资源: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech.cnbl ...
- SoapUI登录测试(2)-- 断言
SoapUI登录测试(1)的结果为: 可以看到只有第2步是成功的,1.3的结果是unKnown,这里并没有对1.3两步添加断言,判断testCase中的这2步是否通过. 一.添加断言 1. /logi ...