106142024-04-06 15:19:10Valaki2Drónfutárcpp17Time limit exceeded 15/100564ms55292 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 1e5;

struct edge {
    int a, b;
    int wei;
    bool operator < (const edge & other) const {
        return wei < other.wei;
    }
};

int n;
int x[1 + maxn], y[1 + maxn];

vector<pii > endpoints;

void add(int a, int b) {
    endpoints.pb(mp(min(a, b), max(a, b)));
}

struct point {
    int x, y, idx;
    bool operator < (const point &other) const {
        return x + y < other.x + other.y;
    }
};

map<int, int> m;

void find_batch() {
    vector<point> points;
    for(int i = 1; i <= n; i++) {
        points.pb(point{x[i], y[i], i});
    }
    sort(points.begin(), points.end());
    for(point cur : points) {
        auto it = m.lower_bound(cur.x - cur.y);
        while(it != m.end()) {
            int other = it->se;
            if(x[other] > x[cur.idx]) {
                break;
            }
            it = m.erase(it);
            add(other, cur.idx);
        }
        m[cur.x - cur.y] = cur.idx;
    }
}

void find_relevant_edges() {
    find_batch();
    for(int i = 1; i <= n; i++) {
        y[i] *= -1;
    }
    find_batch();
    for(int i = 1; i <= n; i++) {
        swap(x[i], y[i]);
    }
    find_batch();
    for(int i = 1; i <= n; i++) {
        y[i] *= -1;
    }
    find_batch();
    for(int i = 1; i <= n; i++) {
        x[i] *= -1;
        y[i] *= -1;
    }
    find_batch();
    for(int i = 1; i <= n; i++) {
        x[i] *= -1;
    }
    find_batch();
    for(int i = 1; i <= n; i++) {
        swap(x[i], y[i]);
    }
    find_batch();
    for(int i = 1; i <= n; i++) {
        x[i] *= -1;
    }
    find_batch();
    //sort(endpoints.begin(), endpoints.end());
    //endpoints.resize(unique(endpoints.begin(), endpoints.end()) - endpoints.begin());
}

int par[1 + maxn];
int sz[1 + maxn];

void create_set(int a) {
    par[a] = a;
    sz[a] = 1;
}

int find_set(int a) {
    if(a == par[a]) {
        return a;
    }
    return find_set(par[a]);
}

bool union_sets(int a, int b) {
    a = find_set(a), b = find_set(b);
    if(a == b) {
        return false;
    }
    if(sz[a] > sz[b]) {
        swap(a, b);
    }
    sz[b] += sz[a];
    par[a] = b;
    return true;
}

void solve() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> x[i] >> y[i];
    }
    vector<edge> edges;
    find_relevant_edges();
    for(const pii &p : endpoints) {
        edges.pb(edge {p.fi, p.se, abs(x[p.fi] - x[p.se]) + abs(y[p.fi] - y[p.se])});
    }
    /*for(int i = 1; i <= n; i++) {
        for(int j = i + 1; j <= n; j++) {
            edges.pb(edge {i, j, abs(x[i] - x[j]) + abs(y[i] - y[j])});
        }
    }*/
    sort(edges.begin(), edges.end());
    for(int i = 1; i <= n; i++) {
        create_set(i);
    }
    int ans = 0;
    for(edge e : edges) {
        if(union_sets(e.a, e.b)) {
            ans = max(ans, e.wei);
        }
    }
    cout << ans << "\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1852 KiB
2Time limit exceeded564ms53612 KiB
subtask215/15
3Accepted7ms3164 KiB
4Accepted7ms3332 KiB
5Accepted7ms3272 KiB
6Accepted7ms3320 KiB
7Accepted4ms3324 KiB
subtask30/15
8Accepted7ms3164 KiB
9Accepted7ms3332 KiB
10Accepted7ms3272 KiB
11Accepted7ms3320 KiB
12Accepted4ms3324 KiB
13Accepted4ms3564 KiB
14Accepted7ms3844 KiB
15Accepted7ms4052 KiB
16Accepted7ms4012 KiB
17Accepted7ms4016 KiB
subtask40/35
18Accepted7ms3164 KiB
19Accepted7ms3332 KiB
20Accepted7ms3272 KiB
21Accepted7ms3320 KiB
22Accepted4ms3324 KiB
23Accepted4ms3564 KiB
24Accepted7ms3844 KiB
25Accepted7ms4052 KiB
26Accepted7ms4012 KiB
27Accepted7ms4016 KiB
28Accepted7ms4164 KiB
29Accepted103ms15468 KiB
30Accepted153ms17164 KiB
31Accepted153ms17184 KiB
32Accepted119ms29752 KiB
subtask50/35
33Accepted7ms3164 KiB
34Accepted7ms3332 KiB
35Accepted7ms3272 KiB
36Accepted7ms3320 KiB
37Accepted4ms3324 KiB
38Accepted4ms3564 KiB
39Accepted7ms3844 KiB
40Accepted7ms4052 KiB
41Accepted7ms4012 KiB
42Accepted7ms4016 KiB
43Accepted7ms4164 KiB
44Accepted103ms15468 KiB
45Accepted153ms17164 KiB
46Accepted153ms17184 KiB
47Accepted119ms29752 KiB
48Accepted264ms29584 KiB
49Time limit exceeded550ms28648 KiB
50Time limit exceeded542ms28700 KiB
51Time limit exceeded556ms55292 KiB
52Time limit exceeded550ms28984 KiB