CSES - Aalto Competitive Programming 2024 - wk10 - Wed - Results
Submission details
Task:Closest points
Sender:aalto2024k_001
Submission time:2024-11-13 16:48:56 +0200
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.16 sdetails
#3ACCEPTED0.23 sdetails
#4ACCEPTED0.25 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.24 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.12 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.13 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.25 sdetails
#17ACCEPTED0.12 sdetails
#18ACCEPTED0.00 sdetails

Code

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <limits>
#include <set>

using namespace std;

class Point {
public:
    int x, y;
    Point(long long x=0, long long y=0) : x(x), y(y) {}

    Point operator-(const Point& other) const {
        return Point(x - other.x, y - other.y);
    }

    bool operator==(const Point& other) const {
        return x == other.x && y == other.y;
    }

    bool operator<(const Point& other) const {
        return x < other.x || (x == other.x && y <= other.y);
    }
};

long long cross_product(const Point& p, const Point& q) {
    return static_cast<long long>(p.x) * q.y - static_cast<long long>(p.y) * q.x;
}

long long dist(const Point& p1, const Point& p2) {
    return static_cast<long long>(p1.x - p2.x) * (p1.x - p2.x) +
           static_cast<long long>(p1.y - p2.y) * (p1.y - p2.y);
}

int main() {
    int n;
    cin >> n;
    vector<Point> points;
    set<pair<long long, long long>> s;

    for (int i = 0; i < n; ++i) {
        int x, y;
        cin >> x >> y;
        points.emplace_back(x, y);
    }

    sort(points.begin(), points.end());
    // for (int i = 0; i < n; i ++) {
    //     cout << points[i].x << points[i].y << endl;
    // }

    long long d = numeric_limits<long long>::max();
    int j = 0;
    for (int i = 0; i < n; ++i) {
        long long dd = ceil(sqrt(d));
        while (points[i].x - points[j].x >= dd) {
            s.erase({points[j].y, points[j].x});
            j += 1;
        }

        auto start = s.lower_bound({ points[i].y - dd, points[i].x });
        auto end = s.upper_bound({ points[i].y + dd, points[i].x});

        for (auto it = start; it != end; ++it) {
            long long dx = points[i].x - it->second;
            long long dy = points[i].y - it->first;
            d = min(d, 1LL * dx * dx + 1LL * dy * dy);
            // cout << i << d << endl;
        }

        s.insert({ points[i].y, points[i].x });
    }

    cout << d << endl;
    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
100
58 36
81 -7
46 49
87 -58
...

correct output
1

user output
1

Test 2

Verdict: ACCEPTED

input
200000
-222 -705
277 680
-436 561
528 -516
...

correct output
1

user output
1

Test 3

Verdict: ACCEPTED

input
200000
-464738043 865360844
465231470 129093134
-276549869 -21946314
111055008 -48821736
...

correct output
25413170

user output
25413170

Test 4

Verdict: ACCEPTED

input
200000
1 513001000
2 689002000
3 785003000
4 799004000
...

correct output
1000000

user output
1000000

Test 5

Verdict: ACCEPTED

input
4
0 0
0 3
3 0
1 1

correct output
2

user output
2

Test 6

Verdict: ACCEPTED

input
200000
1 0
1 1
1 2
1 3
...

correct output
1

user output
1

Test 7

Verdict: ACCEPTED

input
4
1 2
10 3
3 5
8 5

correct output
8

user output
8

Test 8

Verdict: ACCEPTED

input
4
10 6
4 10
8 3
2 3

correct output
13

user output
13

Test 9

Verdict: ACCEPTED

input
2
-999999999 -999999999
999999999 999999999

correct output
7999999984000000008

user output
7999999984000000008

Test 10

Verdict: ACCEPTED

input
200000
0 1
1 1
2 1
3 1
...

correct output
1

user output
1

Test 11

Verdict: ACCEPTED

input
8
1 10000
-1 -10000
2 0
-2 0
...

correct output
16

user output
16

Test 12

Verdict: ACCEPTED

input
3
-1000000000 -1000000000
1000000000 1000000000
0 0

correct output
2000000000000000000

user output
2000000000000000000

Test 13

Verdict: ACCEPTED

input
199999
1 1
2 1
3 1
4 1
...

correct output
1

user output
1

Test 14

Verdict: ACCEPTED

input
4
0 0
5 8
6 1
10000 0

correct output
37

user output
37

Test 15

Verdict: ACCEPTED

input
435
-842 -199
-480 798
-176 -406
792 608
...

correct output
2

user output
2

Test 16

Verdict: ACCEPTED

input
200000
1 0
1 2
1 4
1 6
...

correct output
4

user output
4

Test 17

Verdict: ACCEPTED

input
200000
0 1
2 1
4 1
6 1
...

correct output
4

user output
4

Test 18

Verdict: ACCEPTED

input
3
-1000000000 -1000000000
1000000000 1000000000
1000000000 -1000000000

correct output
4000000000000000000

user output
4000000000000000000