Submission #897242

# Submission time Handle Problem Language Result Execution time Memory
897242 2024-01-02T19:34:31 Z juliany2 Road Construction (JOI21_road_construction) C++17
100 / 100
7872 ms 30288 KB
#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

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 time Memory Grader output
1 Correct 1771 ms 5032 KB Output is correct
2 Correct 1764 ms 5028 KB Output is correct
3 Correct 1721 ms 5252 KB Output is correct
4 Correct 1446 ms 5252 KB Output is correct
5 Correct 1717 ms 4008 KB Output is correct
6 Correct 18 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3094 ms 27180 KB Output is correct
2 Correct 3226 ms 27268 KB Output is correct
3 Correct 945 ms 4992 KB Output is correct
4 Correct 2839 ms 27008 KB Output is correct
5 Correct 2968 ms 27156 KB Output is correct
6 Correct 2946 ms 27520 KB Output is correct
7 Correct 2838 ms 27004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3122 ms 26088 KB Output is correct
2 Correct 3522 ms 26088 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1041 ms 24160 KB Output is correct
5 Correct 2606 ms 26328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3122 ms 26088 KB Output is correct
2 Correct 3522 ms 26088 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1041 ms 24160 KB Output is correct
5 Correct 2606 ms 26328 KB Output is correct
6 Correct 3487 ms 26312 KB Output is correct
7 Correct 3380 ms 26088 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 3521 ms 26204 KB Output is correct
11 Correct 1049 ms 24012 KB Output is correct
12 Correct 2562 ms 26464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1771 ms 5032 KB Output is correct
2 Correct 1764 ms 5028 KB Output is correct
3 Correct 1721 ms 5252 KB Output is correct
4 Correct 1446 ms 5252 KB Output is correct
5 Correct 1717 ms 4008 KB Output is correct
6 Correct 18 ms 604 KB Output is correct
7 Correct 4770 ms 14708 KB Output is correct
8 Correct 4818 ms 14612 KB Output is correct
9 Correct 1445 ms 5096 KB Output is correct
10 Correct 3445 ms 14188 KB Output is correct
11 Correct 3392 ms 13928 KB Output is correct
12 Correct 2979 ms 14680 KB Output is correct
13 Correct 3448 ms 13524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1771 ms 5032 KB Output is correct
2 Correct 1764 ms 5028 KB Output is correct
3 Correct 1721 ms 5252 KB Output is correct
4 Correct 1446 ms 5252 KB Output is correct
5 Correct 1717 ms 4008 KB Output is correct
6 Correct 18 ms 604 KB Output is correct
7 Correct 3094 ms 27180 KB Output is correct
8 Correct 3226 ms 27268 KB Output is correct
9 Correct 945 ms 4992 KB Output is correct
10 Correct 2839 ms 27008 KB Output is correct
11 Correct 2968 ms 27156 KB Output is correct
12 Correct 2946 ms 27520 KB Output is correct
13 Correct 2838 ms 27004 KB Output is correct
14 Correct 3122 ms 26088 KB Output is correct
15 Correct 3522 ms 26088 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1041 ms 24160 KB Output is correct
18 Correct 2606 ms 26328 KB Output is correct
19 Correct 3487 ms 26312 KB Output is correct
20 Correct 3380 ms 26088 KB Output is correct
21 Correct 0 ms 600 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 3521 ms 26204 KB Output is correct
24 Correct 1049 ms 24012 KB Output is correct
25 Correct 2562 ms 26464 KB Output is correct
26 Correct 4770 ms 14708 KB Output is correct
27 Correct 4818 ms 14612 KB Output is correct
28 Correct 1445 ms 5096 KB Output is correct
29 Correct 3445 ms 14188 KB Output is correct
30 Correct 3392 ms 13928 KB Output is correct
31 Correct 2979 ms 14680 KB Output is correct
32 Correct 3448 ms 13524 KB Output is correct
33 Correct 7872 ms 29980 KB Output is correct
34 Correct 7813 ms 30088 KB Output is correct
35 Correct 6731 ms 29312 KB Output is correct
36 Correct 4623 ms 30288 KB Output is correct
37 Correct 4794 ms 30084 KB Output is correct
38 Correct 5691 ms 29348 KB Output is correct