Picture

Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 12265   Accepted: 6484

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

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的更多相关文章

  1. [POJ1177]Picture

    [POJ1177]Picture 试题描述 A number of rectangular posters, photographs and other pictures of the same sh ...

  2. 【poj1177】 Picture

    http://poj.org/problem?id=1177 (题目链接) 题意 求矩形周长并. Solution 转自:http://www.cnblogs.com/Booble/archive/2 ...

  3. poj1177 Picture 矩形周长并

    地址:http://poj.org/problem?id=1177 题目: Picture Time Limit: 2000MS   Memory Limit: 10000K Total Submis ...

  4. 【HDOJ1828&&POJ1177】Picture(线段树,扫描线)

    题意:给定n个矩形,求他们的并的周长 n<=5e3,abs(x[i])<=1e4 思路:From https://www.cnblogs.com/kuangbin/archive/2013 ...

  5. POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...

  6. CQOI2005 三角形面积并 和 POJ1177 Picture

    1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 1664  Solved: 443[Submit][Stat ...

  7. POJ1177 Picture 线段树+离散化+扫描线

    求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远 /* Problem: 1177 User: 96655 Memory: 348K Time: 32MS Language: C++ Resu ...

  8. POJ-1177 Picture 矩形覆盖周长并

    题目链接:http://poj.org/problem?id=1177 比矩形面积并麻烦点,需要更新竖边的条数(平行于x轴扫描)..求横边的时候,保存上一个结果,加上当前长度与上一个结果差的绝对值就行 ...

  9. IOI1998 hdu1828 poj1177 Picture

    写了一发扫描线竟然狂WA不止,hdu死活过不了,poj和当时IOI的数据(还花了我1dsdn积分..)都过了. 然后看到谋篇blog里有评论,把数据拿下来发现WA了. 数据是 20 0 1 11 0 ...

随机推荐

  1. 登录令牌 Token 介绍

     Token值介绍 token 值: 登录令牌.利用 token 值来判断用户的登录状态.类似于 MD5 加密之后的长字符串. 用户登录成功之后,在后端(服务器端)会根据用户信息生成一个唯一的值.这个 ...

  2. debian7 amd64版本添加对x86包的支持

    dpkg --add-architecture i386apt-get updateapt-get install ia32-libs ia32-libs-gtk

  3. zabbix增加服务器tcp监控

     zabbix server web界面,需要导入 tcp 监控模板 操作步骤:     Configuration --> Templates --> Import ,选择 本地的 zb ...

  4. (4)获取servlet常用api

    *五)与ServletAPI解耦 方式1 AddAction public String execute() throws Exception, IOException{ //获取请求对象reques ...

  5. svn安装以及汉化过程

    第一步 下载svn地址 https://tortoisesvn.net/downloads.html 第二步一路next 最后finish 完成 就是完成svn的流程 第三步就是安装语言包 安装包语言 ...

  6. Java中内部类中使用外面变量为什么final修饰?

    所以final从语法上约束了实际上两个不同变量的一致性(表现为同一变量). 我的理解: 例如这样的代码: public void test(String str){ str="abc&quo ...

  7. SSH中的Hibernate

    SSH中的Hibernate 就是DAO连接数据库对数据进行实际操作,做了架构简化,对数据库的操作.

  8. OpenCV——花环生成函数

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  9. hdu3739 Anti LIS[最小割]

    长度为 n≤1000 的数列 ai,其中最长上升子序列的长度为 s.至少删去多少数使得最长上升子序列的长度小于 s. 其实这题和那个求有多少不重叠LIS是一样答案的. 先放个图. 图丑别说我. 原网络 ...

  10. 【Lintcode】 035.Reverse Linked List

    题目: Reverse a linked list. Example For linked list 1->2->3, the reversed linked list is 3-> ...