forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ.cpp
More file actions
103 lines (85 loc) · 2.66 KB
/
J.cpp
File metadata and controls
103 lines (85 loc) · 2.66 KB
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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
#define MN 100111
struct Point {
ll x, y;
int k;
int id;
Point() {}
Point(ll x, ll y) : x(x), y(y) {}
Point operator - (const Point& a) const { return Point(x-a.x, y-a.y); }
ll sqlen() { return x*x + y*y; }
} a[MN];
ll mindist; // will be the result
int savei, savej, savek1, savek2;
void upd_ans(Point a, Point b) {
ll cur = (a - b).sqlen();
if (cur < mindist) {
mindist = cur;
savei = a.id;
savej = b.id;
savek1 = a.k;
savek2 = 5 - b.k;
}
}
bool cmpx(const Point& a, const Point& b) {
if (a.x != b.x) return a.x < b.x;
return a.y < b.y;
}
bool cmpy(const Point& a, const Point& b) {
if (a.y != b.y) return a.y < b.y;
return a.x < b.x;
}
void rec(int l, int r, Point a[]) {
if (r - l <= 3) {
for (int i=l; i<=r; ++i)
for (int j=i+1; j<=r; ++j)
upd_ans(a[i], a[j]);
sort(a+l, a+r+1, cmpy); // compare by y
return;
}
int m = (l + r) >> 1;
int midx = a[m].x;
rec(l, m, a), rec(m+1, r, a);
static Point t[MN];
merge(a+l, a+m+1, a+m+1, a+r+1, t, cmpy); // compare by y
copy(t, t+r-l+1, a+l);
int tsz = 0;
for (int i=l; i<=r; ++i)
if (fabs(a[i].x - midx) < mindist) {
for (int j=tsz-1; j>=0 && a[i].y - t[j].y < mindist; --j)
upd_ans(a[i], t[j]);
t[tsz++] = a[i];
}
}
int n;
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
while (scanf("%d", &n) == 1) {
FOR(i,1,n) {
int x, y; scanf("%d%d", &x, &y);
a[i].x = abs(x);
a[i].y = abs(y);
a[i].id = i;
if (x >= 0 && y >= 0) a[i].k = 1;
else if (x < 0 && y >= 0) a[i].k = 2;
else if (x >= 0 && y < 0) a[i].k = 3;
else a[i].k = 4;
}
mindist = 1e12;
sort(a+1, a+n+1, cmpx);
rec(1, n, a);
cout << savei << ' ' << savek1 << ' ' << savej << ' ' << savek2 << endl;
}
}