一 原题
IOI 1998
A number, N (1 <= N < 5000), 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.
Figure 1 shows an example with seven rectangles:
Figure 1. A set of seven rectangles
The corresponding boundary is the whole set of line segments drawn in Figure 2:
Figure 2. The boundary of the set of rectangles
The vertices of all rectangles have integer coordinates. All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area. The numeric value of the result fits in a 32-bit signed representation.
PROGRAM NAME: picture
INPUT FORMAT
Line 1: | N, the number of rectangles pasted on the wall. |
Lines 2..N+1 | 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. |
SAMPLE INPUT (file picture.in)
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
OUTPUT FORMAT
A single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
SAMPLE OUTPUT (file picture.out)
228
二 分析
三 代码
USER: Qi Shen [maxkibb3] TASK: picture LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.011 secs, 5276 KB] Test 2: TEST OK [0.011 secs, 5276 KB] Test 3: TEST OK [0.000 secs, 5276 KB] Test 4: TEST OK [0.000 secs, 5276 KB] Test 5: TEST OK [0.000 secs, 5276 KB] Test 6: TEST OK [0.000 secs, 5276 KB] Test 7: TEST OK [0.011 secs, 5276 KB] Test 8: TEST OK [0.000 secs, 5276 KB] Test 9: TEST OK [0.011 secs, 5276 KB] Test 10: TEST OK [0.000 secs, 5276 KB] Test 11: TEST OK [0.097 secs, 5276 KB] All tests OK.
YOUR PROGRAM ('picture') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.
/*
ID:maxkibb3
LANG:C++
PROB:picture
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX = 1e5 + 5;
const int MAXR = 5005;
int N, Ans;
int Level[MAX * 2];
struct Seg {
bool f;
int a, s, t;
Seg() {}
Seg(bool _f, int _a, int _s, int _t)
: f(_f), a(_a), s(_s), t(_t) {}
bool operator < (const Seg &_o) const {
if(a == _o.a) return f < _o.f;
return a < _o.a;
}
}Sx[MAXR * 2], Sy[MAXR * 2];
void init() {
freopen("picture.in", "r", stdin);
freopen("picture.out", "w", stdout);
scanf("%d", &N);
int a, b, c, d;
for(int i = 0; i < N; i++) {
scanf("%d%d%d%d", &a, &b, &c, &d);
a += MAX, b += MAX, c += MAX, d += MAX;
Seg sx1(false, b, a, c),
sx2(true, d, a, c),
sy1(false, a, b, d),
sy2(true, c, b, d);
Sx[i * 2] = sx1, Sx[i * 2 + 1] = sx2;
Sy[i * 2] = sy1, Sy[i * 2 + 1] = sy2;
}
sort(Sx, Sx + 2 * N);
sort(Sy, Sy + 2 * N);
}
void solve() {
for(int i = 0; i < 2 * N; i++) {
if(!Sx[i].f) {
for(int j = Sx[i].s; j < Sx[i].t; j++) {
Level[j]++;
if(Level[j] == 1) Ans++;
}
}
else {
for(int j = Sx[i].s; j < Sx[i].t; j++) {
Level[j]--;
if(Level[j] == 0) Ans++;
}
}
}
for(int i = 0; i < MAX * 2; i++) Level[i] = 0;
for(int i = 0; i < 2 * N; i++) {
if(!Sy[i].f) {
for(int j = Sy[i].s; j < Sy[i].t; j++) {
Level[j]++;
if(Level[j] == 1) Ans++;
}
}
else {
for(int j = Sy[i].s; j < Sy[i].t; j++) {
Level[j]--;
if(Level[j] == 0) Ans++;
}
}
}
printf("%d\n", Ans);
}
int main() {
init();
solve();
return 0;
}