-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.cpp
203 lines (169 loc) · 6.22 KB
/
4.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
#include <bits/stdc++.h>
using namespace std;
double EPS = 1e-12;
struct Point {
double x;
double y;
Point () {}
Point (double x, double y) : x(x), y(y) {}
Point (const Point &p) : x(p.x), y(p.y) {}
Point operator - (const Point &p) {return Point(x - p.x, y - p.y);}
Point operator + (const Point &p) {return Point(x + p.x, y + p.y);}
Point operator * (double c) {return Point(x * c, y * c);}
Point operator / (double c) {return Point(x / c, y / c);}
};
Point p1, p2, p3, p4;
double dot(Point a, Point b) {
return a.x * b.x + a.y * b.y;
}
double dist2(Point a, Point b) {
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
double cross(Point a, Point b) {
return a.x * b.y - a.y - b.x;
}
bool lines_parallel(Point a, Point b, Point c, Point d) {
return fabs(cross(b-a, c-d)) < EPS;
}
bool lines_collinear(Point a, Point b, Point c, Point d) {
return lines_parallel(a, b, c, d) && fabs(cross(a-b, a-c)) < EPS && fabs(cross(c-d, c-a)) < EPS;
}
bool segments_intersect(Point a, Point b, Point c, Point d) {
if(lines_collinear(a, b, c, d)) {
if(dist2(a, c) < EPS || dist2(a, d) < EPS || dist2(b, c) < EPS || dist2(b, d) < EPS) {
return true;
}
if(dot(c-a, c-b) > 0 && dot(d-a, d-b) > 0 && dot(c-b, d-b) > 0) {
return false;
}
return true;
}
if(cross(d-a, b-a) * cross(c-a, b-a) > 0) return false;
if(cross(a-c, d-c) * cross(b-c, d-c) > 0) return false;
return true;
}
Point segment_intersection(Point a, Point b, Point c, Point d) {
b = b-a;
d = c-d;
c = c-a;
return a + b * (cross(c, d) / cross(b, d));
}
vector<double> quad_intersection(double x) {
Point bottom(x, -1000006);
Point top(x, 1000006);
vector<double> ys;
if(segments_intersect(bottom, top, p1, p2) && !lines_collinear(bottom, top, p1, p2)) {
Point intersection = segment_intersection(bottom, top, p1, p2);
assert(intersection.x == x);
ys.push_back(intersection.y);
//cout << p1.x << " " << p1.y << " " << p2.x << " " << p2.y << endl;
//cout << intersection.x << " " << intersection.y << endl;
}
if(segments_intersect(bottom, top, p2, p3) && !lines_collinear(bottom, top, p2, p3)) {
Point intersection = segment_intersection(bottom, top, p2, p3);
assert(intersection.x == x);
ys.push_back(intersection.y);
//cout << p2.x << " " << p2.y << " " << p3.x << " " << p3.y << endl;
//cout << intersection.x << " " << intersection.y << endl;
}
if(segments_intersect(bottom, top, p3, p4) && !lines_collinear(bottom, top, p3, p4)) {
Point intersection = segment_intersection(bottom, top, p3, p4);
assert(intersection.x == x);
ys.push_back(intersection.y);
//cout << p3.x << " " << p3.y << " " << p4.x << " " << p4.y << endl;
//cout << intersection.x << " " << intersection.y << endl;
}
if(segments_intersect(bottom, top, p4, p1) && !lines_collinear(bottom, top, p4, p1)) {
Point intersection = segment_intersection(bottom, top, p4, p1);
assert(intersection.x == x);
ys.push_back(intersection.y);
//cout << p4.x << " " << p4.y << " " << p1.x << " " << p1.y << endl;
//cout << intersection.x << " " << intersection.y << endl;
}
return ys;
}
double min(double a, double b) {
if (a < b) {
return a;
}
return b;
}
double max(double a, double b) {
if (a > b) {
return a;
}
return b;
}
int main() {
long long N;
while(cin >> N) {
cin >> p1.x >> p1.y;
cin >> p2.x >> p2.y;
cin >> p3.x >> p3.y;
cin >> p4.x >> p4.y;
vector<long long> num_squares;
while(N) {
long long squares = 0;
for(long long i = -1000006; i <= 1000006; ++i) {
vector<double> ys1 = quad_intersection(i);
vector<double> ys2 = quad_intersection(i+1);
if(ys1.size() == 0 || ys2.size() == 0) continue;
double y1bottom = min(ys1[0], ys1[1]);
double y1top = max(ys1[0], ys1[1]);
double y2bottom = min(ys2[0], ys2[1]);
double y2top = max(ys2[0], ys2[1]);
if(i == 10) cout << y1bottom << " " << y1top << endl;
if(i == 10) cout << y2bottom << " " << y2top << endl;
double bottom, top;
if (y2bottom >= y1bottom && y2bottom <= y1top) {
bottom = y2bottom;
top = min(y1top, y2top);
} else if (y2top >= y1bottom && y2top <= y1top) {
top = y2top;
bottom = max(y1bottom, y2bottom);
} else {
/* Ranges don't intersect. */
continue;
}
if(i == 0) cout << top << " " << bottom << endl;
if (floor(top + 1e-8) - ceil(bottom - 1e-8) > 0) {
squares += floor(top + 1e-8) - ceil(bottom - 1e-8);
}
/*
int y1bottom = ceil(min(ys1[0], ys1[1]) - 1e-8);
int y1top = floor(max(ys1[0], ys1[1]) + 1e-8);
int y2bottom = ceil(min(ys2[0], ys2[1]) - 1e-8);
int y2top = floor(max(ys2[0], ys2[1]) + 1e-8);
int range_bottom = max(y1bottom, y2bottom);
int range_top = min(y1top, y2top);
if(range_bottom <= range_top) {
cout << y1bottom << " " << y1top << " " << y2bottom << " " << y2top << endl;
cout << range_top << " " << range_bottom << endl;
cout << i << endl;
)
squares += max(range_top - range_bottom, 0);
*/
}
num_squares.push_back(squares);
p1.x /= 2;
p1.y /= 2;
p2.x /= 2;
p2.y /= 2;
p3.x /= 2;
p3.y /= 2;
p4.x /= 2;
p4.y /= 2;
N /= 2;
}
/*for(int i = 0; i<num_squares.size(); ++i) {
cout << num_squares[i] << endl;
)*/
long long prev = 0;
for(long long i = num_squares.size()-1; i >= 0; --i) {
cout << num_squares[i] - 4 * prev;
if(i != 0) cout << " ";
prev = num_squares[i];
}
cout << endl;
}
}