-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrect1.cpp
88 lines (75 loc) · 2.22 KB
/
rect1.cpp
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
/*
ID: paulius10
PROG: rect1
LANG: C++
*/
#include <fstream>
#include <list>
#include <map>
using namespace std;
map<int, int> areas;
struct Rectangle {
int llx, lly, urx, ury, color;
};
void add(int color, int area) {
map<int, int>::iterator it = areas.find(color);
if (it == areas.end())
areas[color] = area;
else
it->second += area;
}
int main() {
int A, B, N;
ifstream fin("rect1.in");
fin >> A >> B >> N;
list<Rectangle> rectangles;
for (int i = 0; i < N; ++i) {
Rectangle rectangle;
fin >> rectangle.llx >> rectangle.lly >> rectangle.urx >> rectangle.ury
>> rectangle.color;
for (list<Rectangle>::iterator it = rectangles.begin();
it != rectangles.end();) {
// if the rectangles overlap
if (rectangle.llx < it->urx && rectangle.urx > it->llx &&
rectangle.lly < it->ury && rectangle.ury > it->lly) {
// top
if (it->ury > rectangle.ury)
rectangles.push_back({max(it->llx, rectangle.llx), rectangle.ury,
min(it->urx, rectangle.urx), it->ury, it->color});
// left
if (it->llx < rectangle.llx) {
Rectangle left = *it;
left.urx = rectangle.llx;
rectangles.push_back(left);
}
// right
if (it->urx > rectangle.urx) {
Rectangle right = *it;
right.llx = rectangle.urx;
rectangles.push_back(right);
}
// bottom
if (it->lly < rectangle.lly)
rectangles.push_back({max(it->llx, rectangle.llx), it->lly,
min(it->urx, rectangle.urx), rectangle.lly, it->color});
rectangles.erase(it++);
} else
++it;
}
rectangles.push_back(rectangle);
}
fin.close();
// sum up the areas
int sum = 0;
for (list<Rectangle>::iterator rectangle = rectangles.begin();
rectangle != rectangles.end(); ++rectangle) {
int area = (rectangle->urx - rectangle->llx) * (rectangle->ury - rectangle->lly);
sum += area;
add(rectangle->color, area);
}
add(1, A * B - sum);
ofstream fout("rect1.out");
for (map<int, int>::iterator it = areas.begin(); it != areas.end(); ++it)
fout << it->first << " " << it->second << endl;
fout.close();
}