-
Notifications
You must be signed in to change notification settings - Fork 0
/
discrete2.cpp
160 lines (145 loc) · 4.06 KB
/
discrete2.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
#include <iostream>
#include <algorithm>
#include <fstream>
#include <complex>
#include <vector>
using namespace std;
#define point complex<double>
#define segment pair<complex<double>,complex<double> >
#define eps 1e-12
//1+epsilon factor:
#define steps 50
#define sample 50
double minx, maxx, miny, maxy;
double Distance(point a)
{
double ratio = (maxy - miny) / (maxx - minx);
return max(ratio*fabs(a.real()) , fabs(a.imag()));
}
point inrange(point a)
{
a = point(max(minx, min(a.real(), maxx)), max(miny, min(a.imag(), maxy)));
return a;
}
segment query(point center, double r)
{
double ratio = fabs((maxy-miny)/(maxx - minx));
point a, b;
a = center - point(r, r*ratio);
b = center + point(r, r*ratio);
return make_pair(inrange(a), inrange(b));
}
bool comp(point a, point b)
{
pair<double, double> aa(a.real(), a.imag());
pair<double, double> bb(b.real(), b.imag());
return aa < bb;
}
bool compf(double a, double b)
{
if (a < b && fabs(a-b)>eps)
return true;
return false;
}
bool compx(point a, point b)
{
return compf(a.real(), b.real());
}
bool compy(point a, point b)
{
return compf(a.imag() , b.imag());
}
bool compp(point a, point b)
{
if (compx(a, b) && !compx(b,a))
return true;
if (!compx(a, b) && !compx(b, a) && compy(a, b) && !compy(b,a))
return true;
return false;
}
bool comps(segment a, segment b)
{
if (compp(a.first, b.first) && !compp(b.first, a.first))
return true;
if (!compp(a.first, b.first) && !compp(b.first, a.first) && compp(a.second, b.second) && !compp(b.second, a.second))
return true;
return false;
}
bool eqp(point a, point b)
{
if (abs(a - b) < eps)
return true;
return false;
}
bool eqs(segment a, segment b)
{
if (eqp(a.first, b.first) && eqp(a.second, b.second))
return true;
return false;
}
int main()
{
vector<point> xs, ys;
vector<segment> queries;
point temp;
while (cin >> temp)
{
xs.push_back(temp);
ys.push_back(point(temp.imag(), temp.real()));
}
sort(xs.begin(), xs.end(),comp);
sort(ys.begin(), ys.end(),comp);
minx = xs[0].real();
maxx = xs.back().real();
miny = ys[0].real();
maxy = ys.back().real();
for (int i = 0; i < xs.size(); i+=sample)
{
//initialize
int ind = lower_bound(ys.begin(), ys.end(), xs[i].imag(), comp) - ys.begin();
if (ind == ys.size())
continue;
double r_min=1;
bool assigned = false;
if (i>0 && (assigned == false || r_min > Distance(xs[i - 1] - xs[i])))
r_min = Distance(xs[i - 1] - xs[i]);
if (i<xs.size() - 1 && (assigned == false || r_min > Distance(xs[i + 1] - xs[i])))
r_min = Distance(xs[i + 1] - xs[i]);
if (ind > 0 && (assigned == false || r_min > Distance(ys[ind] - ys[ind - 1])))
r_min = Distance(ys[ind] - ys[ind - 1]);
if (ind<ys.size() - 1 && (assigned == false || r_min > Distance(ys[ind + 1] - ys[ind])))
r_min = Distance(ys[ind + 1] - ys[ind]);
//find radii
double r = r_min;
for (int j = i + 1; j < xs.size();r*=steps)
{
queries.push_back(query(xs[i], Distance(xs[i] - xs[j])));
j = max(j+1,lower_bound(xs.begin()+i, xs.end(), point(r,0)+xs[i], compx) - xs.begin());
}
r = r_min;
for (int j = i-1; j >=0; r *= steps)
{
queries.push_back(query(xs[i], Distance(xs[i] - xs[j])));
j = min(j-1,lower_bound(xs.begin(), xs.begin()+i, xs[i]-point(r,0), compx) - xs.begin());
}
r = r_min;
for (int j = ind + 1; j < ys.size(); r *= steps)
{
queries.push_back(query(xs[i], Distance(ys[ind] - ys[j])));
j = max(j+1,lower_bound(ys.begin() + ind, ys.end(), ys[ind]+point(r,0), compx) - ys.begin());
}
r = r_min;
for (int j = ind-1; j >=0; r *= steps)
{
queries.push_back(query(xs[i], Distance(ys[ind]-ys[j])));
j = min(j-1,lower_bound(ys.begin() , ys.begin()+ind, ys[ind]-point(r,0), compx) - ys.begin());
}
}
sort(queries.begin(), queries.end(), comps);
queries.resize(unique(queries.begin(), queries.end(), eqs)-queries.begin());
for (int i = 0; i < queries.size(); i++)
{
cout << queries[i].first << " " << queries[i].second << endl;
}
return 0;
}