제출 #1325131

#제출 시각아이디문제언어결과실행 시간메모리
1325131anteknneRoad Construction (JOI21_road_construction)C++20
100 / 100
3839 ms29900 KiB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false

const int MAXN = 250 * 1000 + 17;
pll a[MAXN];
int n;
ordered_set os;
deque<pair<ll, pll>> vec;
vector<ll> v;

ll ile (ll d) {
    os.clear();
    vec.clear();
    ll wyn = 0;
    int cnt = 0;
    for (int i = 0; i < n; i ++) {
        while (!vec.empty() && vec.front().st < a[i].st) {
            os.erase(vec.front().nd);
            vec.pop_front();
        }
        ll ile = (os.order_of_key({a[i].nd + d + 1LL, 0}) - os.order_of_key({a[i].nd - d, -1}));
        wyn += ile;
        cnt ++;
        os.insert({a[i].nd, cnt});
        vec.pb({a[i].st + d, {a[i].nd, cnt}});
    }
    return wyn;
}

int main () {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    ll K;
    cin >> n >> K;

    int x, y;
    for (int i = 0; i < n; i ++) {
        cin >> x >> y;
        a[i].st = x - y;
        a[i].nd = x + y;
    }

    sort(a, a + n);

    ll p = 0, k = ll(2000 * 1000 * 1000) * ll(2), w;
    while (p <= k) {
        ll sr = (p + k)/ 2;
        if (ile(sr) >= K) {
            w = sr;
            k = sr - 1;
        } else {
            p = sr + 1;
        }
    }


    ll d = w - 1;
    os.clear();
    vec.clear();
    for (int i = 0; i < n; i ++) {
        while (!vec.empty() && vec.front().st < a[i].st) {
            os.erase(vec.front().nd);
            vec.pop_front();
        }
        auto it = os.lower_bound({a[i].nd - d, -ll(2000 * 1000 * 1000)});
        while (it != os.end() && (*it).st <= a[i].nd + d) {
            v.pb(max(abs(a[i].nd - (*it).st), abs(a[i].st - (*it).nd)));
            it ++;
        }

        os.insert({a[i].nd, a[i].st});
        vec.pb({a[i].st + d, {a[i].nd, a[i].st}});
    }


    while (int(v.size()) < K) {
        v.pb(w);
    }
    sort(v.begin(), v.end());
    for (auto x : v) {
        cout << x << "\n";
    }

    return 0;
}

#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...