-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP143.cpp
212 lines (202 loc) · 4.3 KB
/
P143.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <iomanip>
struct Real;
typedef std::pair<Real,Real> Point;
typedef std::pair<Point,Point> Line;
typedef long long ll;
#define XX first
#define YY second
#define p1 first
#define p2 second
ll gcd(ll a, ll b) {
ll c;
while(a != 0) {
c = a;
a = b%a;
b = c;
}
return b;
}
int sign(ll l) {
if(l < 0)
return -1;
return 1;
}
struct Real {
ll nom, denom;
Real() : nom(0), denom(0) {}
Real(ll i) : nom(i), denom(1) {}
Real(ll nom, ll denom) : nom(sign(denom)*nom), denom(std::abs(denom)) {}
Real(const std::string &rep) {
nom = 0;
denom = 1;
char c;
unsigned int i = 0;
while(((c = rep[i]) != '.') && i < rep.size()) {
nom = 10*nom + (c-'0');
++i;
}
++i;
while(i < rep.size()) {
nom = 10*nom + (rep[i]-'0');
denom*=10;
++i;
}
}
Real operator*(const Real &b) const {
return Real(nom*b.nom, denom*b.denom);
}
Real operator+(const Real &b) const {
return Real(nom*b.denom+b.nom*denom, denom*b.denom);
}
Real operator-(const Real &b) const {
return Real(nom*b.denom-b.nom*denom, denom*b.denom);
}
Real operator/(const Real &b) const {
return Real(nom*b.denom, denom*b.nom);
}
bool operator==(const Real &b) const {
return nom*b.denom == denom*b.nom;
}
bool operator<(const Real &b) const {
return nom*b.denom < b.nom*denom;
}
bool operator>(const Real &b) const {
return nom*b.denom > denom*b.nom;
}
bool operator<=(const Real &b) const {
return nom*b.denom <= denom*b.nom;
}
bool operator>=(const Real &b) const {
return nom*b.denom >= denom*b.nom;
}
bool operator<(const ll &b) const {
return nom < denom*b;
}
bool operator>(const ll &b) const {
return nom > denom*b;
}
bool operator<=(const ll &b) const {
return nom <= denom*b;
}
bool operator>=(const ll &b) const {
return nom >= denom*b;
}
bool operator==(const ll &b) const {
return nom == b*denom;
}
Real shorten() const {
Real ret(nom,denom);
ll g = gcd(nom,denom);
ret.nom /= g;
ret.denom /= g;
return ret;
}
};
std::ostream& operator<<(std::ostream& os, const Real& b) {
os << b.nom << "/" << b.denom;
//os << (b.nom/(double)b.denom);
return os;
}
std::ostream& operator<<(std::ostream& os, const Point& b) {
os << b.XX << "," << b.YY;
return os;
}
std::ostream& operator<<(std::ostream& os, const Line& b) {
os << b.p1 << "->" << b.p2;
return os;
}
Real getX(const Line &l, int y) {
if(l.p1.YY == y)
return l.p1.XX;
if(l.p2.YY == y)
return l.p2.XX;
Real a = ((l.p2.XX - l.p1.XX) / (l.p2.YY - l.p1.YY)).shorten();
return (l.p1.XX + a*y - a*l.p1.YY).shorten();
}
int get(Line *lines) {
int res = 0;
for(int y = 1; y <= 99; ++y) {
Real minX(100);
Real maxX(0);
for(int i = 0; i < 3; ++i) {
Line &l = lines[i];
if(l.p1.YY <= y && l.p2.YY >= y) {
if(l.p1.YY == l.p2.YY) {
{
Real x = l.p1.XX;
if(x < minX) {
minX = x;
}
if(x > maxX) {
maxX = x;
}
}
{
Real x = l.p2.XX;
if(x < minX) {
minX = x;
}
if(x > maxX) {
maxX = x;
}
}
continue;
}
// get x
Real x = getX(l, y);
// update minX, maxX
if(x < minX) {
minX = x;
}
if(x > maxX) {
maxX = x;
}
}
}
if(minX < 1)
minX = Real(1);
if(maxX > 99)
maxX = Real(99);
// update res
if(maxX >= minX) {
int max = maxX.nom / maxX.denom;
int min = (minX.nom / minX.denom) + (minX.nom % minX.denom == 0 ? 0 : 1);
int add = max-min+1;
res += add;
}
}
return res;
}
int main() {
Point points[3];
Line lines[3];
while(true) {
bool allZero = true;
for(int i = 0; i < 3; ++i) {
std::string s;
std::cin >> s;
points[i].XX = Real(s);
if(points[i].XX.nom != 0)
allZero = false;
std::cin >> s;
points[i].YY = Real(s);
if(points[i].YY.nom != 0)
allZero = false;
}
if(allZero)
return 0;
lines[0] = Line(points[0],points[1]);
lines[1] = Line(points[1],points[2]);
lines[2] = Line(points[2],points[0]);
for(int i = 0; i < 3; ++i) {
if(lines[i].p1.YY > lines[i].p2.YY) {
std::swap(lines[i].p1, lines[i].p2);
}
}
// compute trapped trees:
printf("%4i\n", get(lines));
}
}