Submission #1294888

#TimeUsernameProblemLanguageResultExecution timeMemory
1294888BahaminRoad Construction (JOI21_road_construction)C++20
0 / 100
2033 ms10472 KiB
#include <bits/stdc++.h>

using namespace std;

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }

#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define sui cout.tie(NULL); cin.tie(NULL); ios_base::sync_with_stdio(false)
#define lid id << 1
#define rid id << 1 | 1
#define mid ((r + l) >> 1)
const int MAX_N = 2.5e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
const int LOG = 30;

int n, k;
pair<int, int> al[MAX_N];
vector<int> xs;
vector<int> ys;

int fen[MAX_N];

void add(int x, int y)
{
    x++;
    for (int i = x; i < MAX_N; i += i & (-i)) fen[i] += y;
}

int get(int x)
{
    x++;
    int ans = 0;
    for (int i = x; i > 0; i -= i & (-i)) ans += fen[i];
    return ans;
}

ll check(int x)
{
    fill(fen, fen + n + 1, 0);
    int l = -1;
    int r = -1;
    ll sum = 0;
    for (int i = 0; i < n; i++)
    {
        while (r + 1 < n && al[r + 1].first <= al[i].first + x) 
        {
            r++;
            add(lower_bound(all(ys), al[r].second) - ys.begin(), 1);
        }
        while (al[l + 1].first < al[i].first - x) 
        {
            l++;
            add(lower_bound(all(ys), al[l].second) - ys.begin(), -1);
        }
        sum += get(upper_bound(all(ys), al[i].second + x) - ys.begin() - 1) - get(lower_bound(all(ys), al[i].first - x) - ys.begin() - 1);
    }   
    return (sum - n) / 2;
}

void solve() {
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> al[i].first >> al[i].second;
        al[i].first = al[i].first - al[i].second;
        al[i].second = al[i].first + 2 * al[i].second;
        xs.push_back(al[i].first);
        ys.push_back(al[i].second);
    }
    sort(all(xs));
    sort(all(ys));
    xs.resize(unique(all(xs)) - xs.begin());
    ys.resize(unique(all(ys)) - ys.begin());
    sort(al, al + n);
    int l = 0;
    int r = 2e9;
    while (r - l > 1)
    {
        int mid1 = ((ll) r + l) >> 1;
        if (check(mid1) < k) l = mid1;
        else r = mid1;
    }
    int num = l;
    l = -1;
    r = -1;
    set<pair<int, int>> al1;
    vector<int> dis;    
    for (int i = 0; i < n; i++)
    {
        while (r + 1 < n && al[r + 1].first <= al[i].first + num) 
        {
            r++;
            al1.insert({lower_bound(all(ys), al[r].second) - ys.begin(), r});
        }
        while (al[l + 1].first < al[i].first - num) 
        {
            l++;
            al1.erase({lower_bound(all(ys), al[l].second) - ys.begin(), l});
        }
        vector<pair<int, int>> rem;
        int ind1 = lower_bound(all(ys), al[i].second - num) - ys.begin();
        int ind2 = upper_bound(all(ys), al[i].second + num) - ys.begin();
        while (al1.size())
        {
            auto ind = al1.lower_bound({ind1, -1});
            if (ind == al1.end() || (*ind).first >= ind2) break;
            int x = (*ind).second;
            rem.push_back(*ind);
            al1.erase(ind);
            // int x1 = (al[x].first + al[x].second) / 2;
            // int y1 = (al[x].second - al[x].first) / 2;
            // int x2 = (al[i].first + al[i].second) / 2;
            // int y2 = (al[i].second - al[i].first) / 2;
            if (x ^ i) dis.push_back(max(abs(al[x].first - al[i].first), abs(al[x].second - al[i].second)));
        }
        for (auto x : rem) al1.insert(x);
    }   
    vector<int> ans;
    sort(all(dis));
    for (int i = 0; i < dis.size(); i += 2) ans.push_back(dis[i]);
    while (ans.size() < k) ans.push_back(num + 1);
    for (int x : ans) cout << x << "\n";
}

int main() {
    sui;
    int tc = 1;
    //cin >> tc;
    for (int t = 1; t <= tc; t++) {
        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...