제출 #1292012

#제출 시각아이디문제언어결과실행 시간메모리
1292012IcelastRoad Construction (JOI21_road_construction)C++20
100 / 100
2714 ms17240 KiB
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
struct point{
    ll x, y;
};
struct normalize{
    vector<ll> poi, pot;
    void add(ll x){
        poi.push_back(x);
    }
    void start(){
        sort(poi.begin(), poi.end());
        if(poi.size() > 0) pot.push_back(poi[0]);
        for(int i = 1; i < (int)poi.size(); i++){
            if(poi[i] != poi[i-1]){
                pot.push_back(poi[i]);
            }
        }
    }
    int encode(ll x){
        return lower_bound(pot.begin(), pot.end(), x) - pot.begin()+1;
    }
    int encode2(ll x){
        return upper_bound(pot.begin(), pot.end(), x) - pot.begin();
    }
};
template <class T>
struct Fenwick {
    int n, log;
    vector<T> bit;

    Fenwick(int n) : n(n), log(32 - __builtin_clz(n + 1)), bit(n + 1, 0) {}

    void add(int i, T delta) {
        for (; i <= n; i += i & -i) {
            bit[i] += delta;
        }
    }

    T sum(int i) {
        T res = 0;
        for (; i > 0; i -= i & -i) {
            res += bit[i];
        }
        return res;
    }
    T sum(int l, int r) {
        return sum(r)-sum(l-1);
    }
    int kth(T k) {
        T sum = 0;
        int pos = 0;
        for (int l = log - 1; l >= 0; l--) {
            if (pos + (1 << l) <= n && sum + bit[pos + (1 << l)] <= k) {
                pos += 1 << l;
                sum += bit[pos];
            }
        }
        return pos;
    }
};
void solve(){
    int n, k;
    cin >> n >> k;
    vector<point> a(n+1);
    for(int i = 1; i <= n; i++){
        ll x, y;
        cin >> x >> y;
        a[i].x = x+y; a[i].y = x-y;
    }
    normalize norm;
    for(int i = 1; i <= n; i++){
        norm.add(a[i].y);
    }
    norm.add(INF);
    norm.start();
    vector<int> ecy(n+1);
    sort(a.begin()+1, a.end(), [&](point a, point b){return a.x < b.x;});
    for(int i = 1; i <= n; i++){
        ecy[i] = norm.encode(a[i].y);
    }


    int N = norm.pot.size();
    auto check = [&](ll D) -> bool{
        ll cnt = 0;
        int j = 1;
        Fenwick<int> bit(N+1);
        for(int i = 1; i <= n; i++){
            while(j < i && a[i].x - a[j].x > D){
                bit.add(ecy[j], -1);
                j++;
            }
            cnt += bit.sum(norm.encode(a[i].y-D), norm.encode2(a[i].y+D));
            bit.add(ecy[i], 1);
        }
        return cnt >= k;
    };
    ll l = 1, r = 4e9, mid;
    while(l <= r){
        mid = (l+r)/2;
        if(check(mid)){
            r = mid-1;
        }else{
            l = mid+1;
        }
    }
    auto dist = [&](point a, point b) -> ll{
        return abs((a.x+a.y)/2 - (b.x+b.y)/2) + abs((a.x-a.y)/2 - (b.x-b.y)/2);
    };
    vector<ll> ans;
    auto get_ans = [&](ll D) -> void{
        int j = 1;
        set<pair<ll, ll>> s;
        for(int i = 1; i <= n; i++){
            while(j < i && a[i].x - a[j].x > D){
                s.erase({a[j].y, a[j].x});
                j++;
            }
            ll L = a[i].y-D, R = a[i].y+D;
            auto itl = s.lower_bound({L, -INF});
            if(s.empty() || itl == s.end()){
                s.insert({a[i].y, a[i].x});
                continue;
            }
            auto itr = s.upper_bound({R, INF});
            if(itr == s.begin() || (*prev(itr)) < (*itl)){
                s.insert({a[i].y, a[i].x});
                continue;
            }
            itr--;
            for(auto it = itl; it != itr; it++){
                point b = {(*it).second, (*it).first};
                ans.push_back(dist(a[i], b));
            }
            point b = {(*itr).second, (*itr).first};
            ans.push_back(dist(a[i], b));
            s.insert({a[i].y, a[i].x});
        }
    };
    get_ans(l);
    sort(ans.begin(), ans.end());
    while(ans.size() > k) ans.pop_back();
    for(ll x : ans){
        cout << x << "\n";
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
}
#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...