Submission #916946

# Submission time Handle Problem Language Result Execution time Memory
916946 2024-01-26T20:09:27 Z makrav Road Construction (JOI21_road_construction) C++14
100 / 100
4745 ms 133136 KB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vei;
typedef vector<vei> vevei;

#define all(a) (a).begin(), (a).end()
#define sz(a) (int) a.size()
#define con cout << "NO\n"
#define coe cout << "YES\n";
#define str string
#define pb push_back
#define ff first
#define sc second
#define ss second
#define pii pair<int, int>
#define mxe max_element
#define mne min_element
#define stf shrink_to_fit
#define f(i, l, r) for (int i = (l); i < (r); i++)
#define double ld

struct fenwick {
    int n;
    vector<int> t;
    fenwick() = default;
    fenwick(int n_) {
        n = n_;
        t.assign(n + 1, 0);
    }

    void clear() {
        t.assign(n + 1, 0);
    }

    int sum(int x) {
        int ans = 0;
        for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
            ans += t[i];
        }
        return ans;
    }

    void upd(int pos, int delta) {
        for (int i = pos; i < n; i = i | (i + 1)) {
            t[i] += delta;
        }
    }
};

vector<pair<int, int>> pts;

struct mst {
    int n;
    vector<pair<int, int>> a;
    vector<vector<pair<int, int>>> t;
    mst() = default;
    mst(int n_, vector<pair<int, int>> a_) {
        n = n_;
        a = a_;
        t.resize(4 * n);
        build(1, 0, n);
    }

    void build(int v, int tl, int tr) {
        if (tl + 1 == tr) {
            t[v] = {a[tl]};
            return;
        }
        int tm = (tl + tr) / 2;
        build(v * 2, tl, tm);
        build(v * 2 + 1, tm, tr);
        t[v].resize(tr - tl);
        merge(all(t[v * 2]), all(t[v * 2 + 1]), t[v].begin());
    }

    void mark(int v, int tl, int tr, int l, int r, int l1, int r1, int I) {
        if (l <= tl && tr <= r) {
            int lf, rg;
            {
                int L = -1, R = sz(t[v]);
                while (R - L > 1) {
                    int M = (L + R) / 2;
                    if (t[v][M].ff >= l1) R = M;
                    else L = M;
                }
                lf = R;
            }
            {
                int L = -1, R = sz(t[v]);
                while (R - L > 1) {
                    int M = (L + R) / 2;
                    if (t[v][M].ff <= r1) L = M;
                    else R = M;
                }
                rg = L;
            }
            for (int j = lf; j <= rg; j++) {
                pts.pb({t[v][j].sc, I});
            }
            return;
        }
        if (tr <= l || tl >= r) return;
        int tm = (tl + tr) / 2;
        mark(v * 2, tl, tm, l, r, l1, r1, I);
        mark(v * 2 + 1, tm, tr, l, r, l1, r1, I);
    }
};

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, k; cin >> n >> k;
    vector<pair<ll, ll>> a(n);
    for (int i = 0; i < n; i++) cin >> a[i].ff >> a[i].sc;

    vector<pair<ll, ll>> b(n), xs(n), ys(n);
    for (int i = 0; i < n; i++) {
        b[i] = {a[i].ff - a[i].sc, a[i].ff + a[i].sc};
        xs[i] = {b[i].ff, i};
        ys[i] = {b[i].sc, i};
    }   
    vector<int> posx(n), posy(n);
    sort(all(xs));
    sort(all(ys));
    f (i, 0, n) {
        posy[ys[i].sc] = i;
        posx[xs[i].sc] = i;
    }

    ll L = -1, R = 4 * 1000ll * 1000ll * 1000ll;
    vector<int> nextx(n), prevx(n), nexty(n), prevy(n);
    vector<vector<int>> evs(n + 1);
    fenwick fw(n + 1);
    while (R - L > 1) {
        ll M = (L + R) / 2;
        ll sm = 0;

        int ind = 0;
        f (i, 0, n) {
            while (ind + 1 < n && xs[ind + 1].ff - xs[i].ff <= M) ind++;
            nextx[i] = ind;
        }
        ind = n - 1;
        for (int i = n - 1; i >= 0; i--) {
            while (ind - 1 >= 0 && xs[i].ff - xs[ind - 1].ff <= M) ind--;
            prevx[i] = ind;
        }
        ind = 0;
        f(i, 0, n) {
            while (ind + 1 < n && ys[ind + 1].ff - ys[i].ff <= M) ind++;
            nexty[i] = ind;
        }
        ind = n - 1;
        for (int i = n - 1; i >= 0; i--) {
            while (ind - 1 >= 0 && ys[i].ff - ys[ind - 1].ff <= M) ind--;
            prevy[i] = ind;
        }

        fw.clear();
        for (int i = 0; i < n; i++) {
            evs[prevx[posx[i]]].pb(i);
            evs[nextx[posx[i]] + 1].pb(i);
        }
        vector<int> ans(n);
        f (i, 1, n + 1) {
            fw.upd(posy[xs[i - 1].sc] + 1, 1);
            for (auto& u : evs[i]) {
                if (prevx[posx[u]] == i) {
                    ans[u] += fw.sum(prevy[posy[u]]);
                    ans[u] -= fw.sum(nexty[posy[u]] + 1);
                }
            }
            for (auto& u : evs[i]) {
                if (nextx[posx[u]] + 1 == i) {
                    ans[u] -= fw.sum(prevy[posy[u]]);
                    ans[u] += fw.sum(nexty[posy[u]] + 1);
                }
            }
        }   
        f (i, 0, n + 1) evs[i].clear();
        f (i, 0, n) sm += ans[i] - 1;

        if (sm / 2 >= k) R = M;
        else L = M;
    }
    //cout << R << '\n';

    auto dist = [&](int i, int j) {
        return abs(a[i].ff - a[j].ff) + abs(a[i].sc - a[j].sc);
    };

    vector<pair<int, int>> tomst;
    f (i, 0, n) {
        //cout << xs[i].sc << ' ' << b[xs[i].sc].sc << '\n';
        tomst.pb({posy[xs[i].sc], xs[i].sc});
    }
    mst MS(n, tomst);
    ll M = L;
    int ind = 0;
    f(i, 0, n) {
        while (ind + 1 < n && xs[ind + 1].ff - xs[i].ff <= M) ind++;
        nextx[i] = ind;
    }
    ind = n - 1;
    for (int i = n - 1; i >= 0; i--) {
        while (ind - 1 >= 0 && xs[i].ff - xs[ind - 1].ff <= M) ind--;
        prevx[i] = ind;
    }
    ind = 0;
    f(i, 0, n) {
        while (ind + 1 < n && ys[ind + 1].ff - ys[i].ff <= M) ind++;
        nexty[i] = ind;
    }
    ind = n - 1;
    for (int i = n - 1; i >= 0; i--) {
        while (ind - 1 >= 0 && ys[i].ff - ys[ind - 1].ff <= M) ind--;
        prevy[i] = ind;
    }

    f (i, 0, n) {
        //cout << prevx[posx[i]] << ' ' << nextx[posx[i]] + 1 <<  '\n';
        MS.mark(1, 0, n, prevx[posx[i]], nextx[posx[i]] + 1, prevy[posy[i]], nexty[posy[i]], i);
    }
    vector<ll> DS;
    for (auto &u : pts) {
        if (u.ff < u.sc && dist(u.ff, u.sc) > 0) {
            DS.pb(dist(u.ff, u.sc));
        }
    }

    sort(all(DS));
    f (i, 0, sz(DS)) cout << DS[i] << '\n';

    f (i, 0, k - sz(DS)) cout << R << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 45 ms 11192 KB Output is correct
2 Correct 44 ms 11192 KB Output is correct
3 Correct 41 ms 11196 KB Output is correct
4 Correct 40 ms 11596 KB Output is correct
5 Correct 40 ms 10172 KB Output is correct
6 Correct 4 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3094 ms 126832 KB Output is correct
2 Correct 3005 ms 128052 KB Output is correct
3 Correct 42 ms 11068 KB Output is correct
4 Correct 3110 ms 126512 KB Output is correct
5 Correct 2512 ms 125292 KB Output is correct
6 Correct 2852 ms 125000 KB Output is correct
7 Correct 2230 ms 124768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3937 ms 118276 KB Output is correct
2 Correct 3944 ms 118536 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2441 ms 116516 KB Output is correct
5 Correct 2447 ms 111644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3937 ms 118276 KB Output is correct
2 Correct 3944 ms 118536 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2441 ms 116516 KB Output is correct
5 Correct 2447 ms 111644 KB Output is correct
6 Correct 3930 ms 118420 KB Output is correct
7 Correct 3923 ms 118284 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 4029 ms 123144 KB Output is correct
11 Correct 2563 ms 119448 KB Output is correct
12 Correct 2212 ms 117116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 11192 KB Output is correct
2 Correct 44 ms 11192 KB Output is correct
3 Correct 41 ms 11196 KB Output is correct
4 Correct 40 ms 11596 KB Output is correct
5 Correct 40 ms 10172 KB Output is correct
6 Correct 4 ms 856 KB Output is correct
7 Correct 1140 ms 57848 KB Output is correct
8 Correct 1155 ms 58224 KB Output is correct
9 Correct 40 ms 11204 KB Output is correct
10 Correct 969 ms 58252 KB Output is correct
11 Correct 902 ms 58012 KB Output is correct
12 Correct 699 ms 47952 KB Output is correct
13 Correct 746 ms 55936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 11192 KB Output is correct
2 Correct 44 ms 11192 KB Output is correct
3 Correct 41 ms 11196 KB Output is correct
4 Correct 40 ms 11596 KB Output is correct
5 Correct 40 ms 10172 KB Output is correct
6 Correct 4 ms 856 KB Output is correct
7 Correct 3094 ms 126832 KB Output is correct
8 Correct 3005 ms 128052 KB Output is correct
9 Correct 42 ms 11068 KB Output is correct
10 Correct 3110 ms 126512 KB Output is correct
11 Correct 2512 ms 125292 KB Output is correct
12 Correct 2852 ms 125000 KB Output is correct
13 Correct 2230 ms 124768 KB Output is correct
14 Correct 3937 ms 118276 KB Output is correct
15 Correct 3944 ms 118536 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2441 ms 116516 KB Output is correct
18 Correct 2447 ms 111644 KB Output is correct
19 Correct 3930 ms 118420 KB Output is correct
20 Correct 3923 ms 118284 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 4029 ms 123144 KB Output is correct
24 Correct 2563 ms 119448 KB Output is correct
25 Correct 2212 ms 117116 KB Output is correct
26 Correct 1140 ms 57848 KB Output is correct
27 Correct 1155 ms 58224 KB Output is correct
28 Correct 40 ms 11204 KB Output is correct
29 Correct 969 ms 58252 KB Output is correct
30 Correct 902 ms 58012 KB Output is correct
31 Correct 699 ms 47952 KB Output is correct
32 Correct 746 ms 55936 KB Output is correct
33 Correct 4586 ms 132760 KB Output is correct
34 Correct 4745 ms 133136 KB Output is correct
35 Correct 3966 ms 130484 KB Output is correct
36 Correct 2145 ms 117860 KB Output is correct
37 Correct 2227 ms 118108 KB Output is correct
38 Correct 2276 ms 126204 KB Output is correct