我是靠谱客的博主 爱笑往事,这篇文章主要介绍POJ1177----Picture,现在分享给大家,希望可以做个参考。

Picture
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 10727 Accepted: 5662

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

复制代码
1
2
3
4
5
6
7
8
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

复制代码
1
228

Source

IOI 1998

矩形周长并,  和面积并差不多, 每新插入一条边,周长的增量就是上一次总长和这一次总长的差值的绝对值, 我是先算横边,再算纵边的, 在纸上画一下图就知道了

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/************************************************************************* > File Name: POJ1177.cpp > Author: ALex > Mail: 405045132@qq.com > Created Time: 2015年01月15日 星期四 15时13分56秒 ************************************************************************/ #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 20110; struct node { int l, r; int len; int add; }tree[N << 2]; struct seg { int l, r; int h; int flag; }lines[N << 1], lines2[N << 1]; int cmp (seg a, seg b) { if (a.h != b.h) { return a.h < b.h; } return a.flag > b.flag; } void build (int p, int l, int r) { tree[p].l = l; tree[p].r = r; tree[p].len = 0; tree[p].add = 0; if (l == r) { return; } int mid = (l + r) >> 1; build (p << 1, l, mid); build (p << 1 | 1, mid + 1, r); } void pushup (int p) { if (tree[p].add) { tree[p].len = tree[p].r - tree[p].l + 1; } else if (tree[p].l == tree[p].r) { tree[p].len = 0; } else { tree[p].len = tree[p << 1].len + tree[p << 1 | 1].len; } } void update (int p, int l, int r, int val) { if (l == tree[p].l && r == tree[p].r) { tree[p].add += val; pushup (p); return; } int mid = (tree[p].l + tree[p].r) >> 1; if (r <= mid) { update (p << 1, l, r, val); } else if (l > mid) { update (p << 1 | 1, l, r, val); } else { update (p << 1, l, mid, val); update (p << 1 | 1, mid + 1, r, val); } pushup (p); } int main() { int n; int x1, y1, x2, y2; while (~scanf("%d", &n)) { build (1, 1, N); for (int i = 1; i <= n; ++i) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); x1 += 10001; x2 += 10001; lines[i].flag = 1; lines[i].l = x1; lines[i].r = x2; lines[i].h = y1; lines[i + n].flag = -1; lines[i + n].l = x1; lines[i + n].r = x2; lines[i + n].h = y2; y1 += 10001; y2 += 10001; lines2[i].flag = 1; lines2[i].l = y1; lines2[i].r = y2; lines2[i].h = x1; lines2[i + n].flag = -1; lines2[i + n].l = y1; lines2[i + n].r = y2; lines2[i + n].h = x2; } sort (lines + 1, lines + 2 * n + 1, cmp); sort (lines2 + 1, lines2 + 2 * n + 1, cmp); int ans = 0; for (int i = 1; i <= 2 * n; ++i) { int last = tree[1].len; update (1, lines[i].l, lines[i].r - 1, lines[i].flag); ans += abs(last - tree[1].len); } build (1, 1, N); for (int i = 1; i <= 2 * n; ++i) { int last = tree[1].len; update (1, lines2[i].l, lines2[i].r - 1, lines2[i].flag); ans += abs(last - tree[1].len); } printf("%dn", ans); } return 0; }


最后

以上就是爱笑往事最近收集整理的关于POJ1177----Picture的全部内容,更多相关POJ1177----Picture内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部