This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
const int mod = 998244353;
const double PI = acos(-1.0);
const double epsilon = 1e-6;
const int N = 2e5 + 5;
vector<pair<int, int>> a;
multiset<int> ans;
map<pair<pair<int,int>,pair<int,int> >,bool> vis;
int mindist = 1e18;
int n, k;
int dist(pair<int, int> f, pair<int, int> s){
return abs(f.ff - s.ff) + abs(f.ss - s.ss);
}
void update(pair<int, int> f, pair<int, int> s){
if(vis[{f,s}] || vis[{s,f}]) return;
ans.insert(dist(f, s));
vis[{f,s}] = 1;
if ((int)ans.size() == k) mindist = *prev(ans.end());
else if ((int)ans.size() > k) {
ans.erase(prev(ans.end()));
mindist = *prev(ans.end());
}
}
bool cmp(pair<int, int> f, pair<int, int> s){
return f.ss < s.ss;
}
void rec(int l, int r){
if (r - l <= 3) {
for (int i = l; i < r; i++) {
for (int j = i + 1; j <= r; j++) {
update(a[i], a[j]);
}
}
sort(a.begin() + l, a.begin() + r + 1, cmp);
return;
}
int m = (l + r) >> 1;
int midx = a[m].ff;
rec(l, m), rec(m + 1, r);
vector<pair<int, int>> t(r - l + 1);
merge(a.begin() + l, a.begin() + m + 1, a.begin() + m + 1, a.begin() + r + 1, t.begin(), cmp);
copy(t.begin(), t.begin() + r - l + 1, a.begin() + l);
int sz = 0;
for (int i = l; i <= r; i++) {
if (abs(a[i].ff - midx) < mindist) {
for (int j = sz - 1; j >= 0 && a[i].ss - t[j].ss < mindist; j--) {
update(a[i], t[j]);
}
t[sz++] = a[i];
}
}
}
void solve(){
cin >> n >> k;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a.pb({x, y});
}
sort(all(a));
rec(0, n - 1);
for (int i : ans) cout << i << '\n';
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
}
Compilation message (stderr)
road_construction.cpp:88:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
88 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |