POJ-1177 Picture (线段树 求矩形周长)
来源:互联网 发布:淘宝对新店的扶持政策 编辑:程序博客网 时间:2024/06/05 05:01
Picture
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 12999 Accepted: 6871
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.
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.
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 2515 -4 24 140 -6 16 42 15 10 2230 10 36 2034 0 40 16
Sample Output
228
#include <stdio.h>#include <algorithm>using namespace std;const int maxn = 5001;struct Xpoint{int x, l, r, v;bool operator < (const Xpoint& e)const {return x < e.x;}}line[maxn << 1];struct tree{int segN, cover, len, l, r;}c[maxn << 3];int y[maxn << 2];void build(int o, int l, int r){c[o].segN = c[o].cover = c[o].len = c[o].l = c[o].r = 0;if(l == r - 1) return;int mid = l + r >> 1;build(o << 1, l, mid);build(o << 1 | 1, mid, r);}void maintain(int o, int l, int r){if(c[o].cover > 0){c[o].segN = 1;c[o].len = y[r] - y[l];c[o].l = c[o].r = 1;return;}if(l == r - 1){c[o].segN = c[o].cover = c[o].len = c[o].l = c[o].r = 0;return;}c[o].len = c[o << 1].len + c[o << 1 | 1].len;c[o].l = c[o << 1].l;c[o].r = c[o << 1 | 1].r;c[o].segN = c[o << 1].segN + c[o << 1 | 1].segN;if(c[o << 1].r == 1 && c[o << 1 | 1].l == 1){c[o].segN--;}}void add(int o, int l, int r, int L, int R, int v){if(l >= R || r <= L) return;if(l >= L && r <= R){c[o].cover += v;maintain(o, l, r);return;}int mid = l + r >> 1;if(mid >= L) add(o << 1, l, mid, L, R, v);if(mid < R) add(o << 1 | 1, mid, r, L, R, v);maintain(o, l, r);}int main(){int n, x1, x2, y1, y2;while(scanf("%d", &n) != EOF){int tot = 0;for(int i = 1; i <= n; ++i){scanf("%d %d %d %d", &x1, &y1, &x2, &y2);x1 += 10000;x2 += 10000;y1 += 10000;y2 += 10000;line[++tot].x = x1;line[tot].l = y1;line[tot].r = y2;line[tot].v = 1;y[tot] = y1;line[++tot].x = x2;line[tot].l = y1;line[tot].r = y2;line[tot].v = -1;y[tot] = y2;}build(1, 1, tot);sort(y + 1, y + 1 + tot);sort(line + 1, line + 1 + tot);long long ans = 0;int last = 0;for(int i = 1; i < tot; ++i){y1 = lower_bound(y + 1, y + 1 + tot, line[i].l) - y;y2 = lower_bound(y + 1, y + 1 + tot, line[i].r) - y;add(1, 1, tot, y1, y2, line[i].v);ans += c[1].segN * 2 * (line[i + 1].x - line[i].x);ans += abs(last - c[1].len);last = c[1].len;}ans += c[1].len;printf("%lld\n", ans);}}/*题意:5000个矩阵,求这些矩形组成的图形的周长。思路:类似于线段树求面积并,将y轴离散化后映射到线段树上,然后分段求,矩形的左边的表示进入一个覆盖区域,后边表示走出一个覆盖区域。用cover维护一下当前是否被覆盖。由于在分段统计x轴方向的边长时需要看现在有多少个不连续段,需要注意分类讨论一下。y轴方向前后计算时只计算多出的或者减少的,防止重复统计。*/
阅读全文
0 0
- poj 1177 Picture(线段树求矩形周长并)
- POJ-1177 Picture (线段树 求矩形周长)
- hdu 1828 / poj/pku 1177(Picture)(线段树求矩形覆盖面周长)
- HDU 1828 / POJ 1177 Picture --线段树求矩形周长并
- poj 1177 Picture(线段树+矩形周长并)
- poj 1177 Picture 【线段树 扫描线 求轮廓周长】
- POJ 1177 Picture(线段树+离散化 求周长)
- POJ1177 HDU1828 Picture,线段树求矩形并周长
- 求矩形并的周长 poj 1177 picture
- poj-1177 Picture(矩形周长并,线段树+扫描线)
- poj 1151 Atlantis(多矩形面积) + poj 1177Picture(多矩形周长) 线段树进阶
- HUD 1828 Picture 矩形周长/线段树
- 线段树 矩形并周长 picture
- 【HDU1828】Picture【线段树】【矩形周长并】
- POJ 1177 Picture 矩形周长并
- POJ 1177 Picture 求多个矩形周长 -
- poj 1177 || HDU 1828 Picture (线段树扫描线求 图形并的周长)
- poj1177[IOI1998]Picture (扫描线+离散化+线段树,求矩形周长并)
- centos 7下安装java环境说明
- andriod——Retrofit+Fresco+MVP商品分类
- angularjs 总结
- 16EFish
- AC自动机
- POJ-1177 Picture (线段树 求矩形周长)
- BookManager(ios)
- 7.4 编写插件-编程世界控制
- zufe17届程序设计竞赛总结
- 利用array_multisort()给二维数组排序
- EasyPlayer RTSP Windows播放器D3D,GDI的几种渲染方式的选择区别
- Java NIO
- 舵机S3010
- 剑指offer — 数据流中的中位数