Submission #897242

#TimeUsernameProblemLanguageResultExecution timeMemory
897242juliany2Road Construction (JOI21_road_construction)C++17
100 / 100
7872 ms30288 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()

template<class T> struct ST {
    static constexpr T ID = {(ll) 1e18, 0}; // or whatever ID
    inline T comb(T a, T b) { return min(a, b); } // or whatever function

    int sz;
    vector<T> t;

    void init(int _sz, T val = ID) {
        t.assign((sz = _sz) * 2, ID);
    }
    void init(vector<T> &v) {
        t.resize((sz = v.size()) * 2);
        for (int i = 0; i < sz; ++i)
            t[i + sz] = v[i];
        for (int i = sz - 1; i; --i)
            t[i] = comb(t[i * 2], t[(i * 2) | 1]);
    }
    void upd(int i, T x) {
        for (t[i += sz] = x; i > 1; i >>= 1)
            t[i >> 1] = comb(t[i], t[i ^ 1]);
    }
    T query(int l, int r) {
        T ql = ID, qr = ID;
        for (l += sz, r += sz + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) ql = comb(ql, t[l++]);
            if (r & 1) qr = comb(t[--r], qr);
        }
        return comb(ql, qr);
    }
};

int main() {
    cin.tie(0)->sync_with_stdio(false);

    int n, k;
    cin >> n >> k;

    vector<array<int, 2>> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i][0] >> a[i][1];

    sort(all(a));

    vector<array<int, 2>> srt;
    for (int i = 0; i < n; i++)
        srt.push_back({a[i][1], i});

    sort(all(srt));

    vector<int> pos(n);
    for (int i = 0; i < n; i++)
        pos[srt[i][1]] = i;

    ST<array<ll, 2>> L, R;

    ll lo = 1, hi = 1e10;
    while (lo < hi) {
        ll mid = (lo + hi) / 2;

        L.init(n + 1);
        R.init(n + 1);

        ll tot = 0;
        for (int i = 0; i < n; i++) {
            auto &[x, y] = a[i];

            auto cnt = [&](ST<array<ll, 2>> &st, int l, int r, ll x) {
                vector<array<ll, 2>> b;
                while (tot < k && st.query(l, r)[0] + x <= mid) {
                    array<ll, 2> j = st.query(l, r);
                    b.push_back(j);
                    tot++;
                    st.upd(j[1], {(ll) 1e18, 0});
                }

                for (array<ll, 2> &j : b)
                    st.upd(j[1], {j[0], j[1]});
            };

            cnt(L, 0, pos[i], x + y);
            cnt(R, pos[i], n - 1, x - y);

            L.upd(pos[i], {-x - y, pos[i]});
            R.upd(pos[i], {-x + y, pos[i]});
        }

        if (tot == k)
            hi = mid;
        else
            lo = mid + 1;
    }

    vector<ll> ans;

    L.init(n + 1);
    R.init(n + 1);

    for (int i = 0; i < n; i++) {
        auto &[x, y] = a[i];

        auto cnt = [&](ST<array<ll, 2>> &st, int l, int r, ll x) {
            vector<array<ll, 2>> b;
            while (st.query(l, r)[0] + x < lo) {
                array<ll, 2> j = st.query(l, r);
                ans.push_back(j[0] + x);
                b.push_back(j);
                st.upd(j[1], {(ll) 1e18, 0});
            }

            for (array<ll, 2> &j : b)
                st.upd(j[1], {j[0], j[1]});
        };

        cnt(L, 0, pos[i], x + y);
        cnt(R, pos[i], n - 1, x - y);

        L.upd(pos[i], {-x - y, pos[i]});
        R.upd(pos[i], {-x + y, pos[i]});
    }

    while (ans.size() < k)
        ans.push_back(lo);

    sort(all(ans));

    for (ll i : ans)
        cout << i << '\n';

    return 0;
}

Compilation message (stderr)

road_construction.cpp: In function 'int main()':
road_construction.cpp:126:23: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  126 |     while (ans.size() < k)
      |            ~~~~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...