#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 3e5 + 100;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll block = 230;
ll n, k;
pll a[N];
vector<ll>ans;
struct segment_tree{
ll n;
vector<vector<pll>>st;
void init(ll _n){
n = _n;
st.resize(4 * n + 10);
build(1,1,n);
}
vector<pll>merge(const vector<pll>&a, const vector<pll>&b){
ll i = 0, j = 0;
vector<pll>res;
while(i < a.size() && j < b.size()){
if(a[i].F <= b[j].F) res.pb(a[i++]);
else res.pb(b[j++]);
}
while(i < a.size()) res.pb(a[i++]);
while(j < b.size()) res.pb(b[j++]);
return res;
}
void build(ll id, ll l, ll r){
if(l == r){
st[id].pb({a[l].S, l});
return;
}
ll mid = (l + r) / 2;
build(2 * id, l, mid);
build(2 * id + 1, mid + 1, r);
st[id] = merge(st[2 * id], st[2 * id + 1]);
}
ll get(ll id, ll l, ll r, ll left, ll right, ll L, ll R){
if(l > right || r < left) return 0;
if(left <= l && r <= right){
ll i = upper_bound(all(st[id]), make_pair(R, inf)) - st[id].begin() - 1;
ll j = lower_bound(all(st[id]), make_pair(L, -inf)) - st[id].begin();
if(j >= (ll)st[id].size()) return 0;
return i - j + 1;
}
ll mid = (l + r) / 2;
return (get(2 * id, l, mid, left, right, L, R) + get(2 * id + 1, mid + 1, r, left, right, L, R));
}
void get_order(ll id, ll l, ll r, ll left, ll right, ll L, ll R, ll idx){
if(l > right || r < left) return;
if(left <= l && r <= right){
ll i = upper_bound(all(st[id]), make_pair(R, inf)) - st[id].begin() - 1;
ll j = lower_bound(all(st[id]), make_pair(L, -inf)) - st[id].begin();
if(j >= (ll)st[id].size()) return;
for(int x = j; x <= i;x++){
ll cur = st[id][x].S;
ans.pb(max(abs(a[cur].F - a[idx].F), abs(a[cur].S - a[idx].S)));
}
return;
}
ll mid = (l + r) / 2;
get_order(2 * id, l, mid, left, right, L, R, idx);
get_order(2 * id + 1, mid + 1, r, left, right, L, R, idx);
}
ll get(ll l, ll r, ll L, ll R){return get(1,1,n,l,r,L,R);}
} st;
bool check(ll val){
ll j = 1, cnt = 0;
for(int i = 1; i <= n;i++){
while(j < i && a[i].F - a[j].F > val) j++;
if(j < i) cnt += st.get(j, i - 1, a[i].S - val, a[i].S + val);
}
return cnt >= k;
}
void add_order(ll val){
ll j = 1;
for(int i = 1; i <= n;i++){
while(j < i && a[i].F - a[j].F > val) j++;
if(j < i) {
st.get_order(1,1,n,j, i - 1, a[i].S - val, a[i].S + val, i);
}
}
}
void to_thic_cau(){
cin >> n >> k;
for(int i = 1; i <= n;i++){
ll x,y; cin >> x >> y;
a[i].F = x - y, a[i].S = x + y;
}
sort(a + 1, a + n + 1);
st.init(n);
ll l = 0, r = 5e9, res = -1;
while(l <= r){
ll mid = (l + r) / 2;
if(check(mid)) res = mid, r = mid - 1;
else l = mid + 1;
}
add_order(res);
sort(all(ans));
for(int i = 0; i < k;i++) cout << ans[i] << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
Compilation message
road_construction.cpp: In member function 'std::vector<std::pair<long long int, long long int> > segment_tree::merge(const std::vector<std::pair<long long int, long long int> >&, const std::vector<std::pair<long long int, long long int> >&)':
road_construction.cpp:29:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | while(i < a.size() && j < b.size()){
| ~~^~~~~~~~~~
road_construction.cpp:29:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | while(i < a.size() && j < b.size()){
| ~~^~~~~~~~~~
road_construction.cpp:33:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | while(i < a.size()) res.pb(a[i++]);
| ~~^~~~~~~~~~
road_construction.cpp:34:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while(j < b.size()) res.pb(b[j++]);
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
5312 KB |
Output is correct |
2 |
Correct |
66 ms |
5420 KB |
Output is correct |
3 |
Correct |
39 ms |
5384 KB |
Output is correct |
4 |
Correct |
34 ms |
5376 KB |
Output is correct |
5 |
Correct |
35 ms |
4300 KB |
Output is correct |
6 |
Correct |
6 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1915 ms |
118196 KB |
Output is correct |
2 |
Correct |
1835 ms |
118184 KB |
Output is correct |
3 |
Correct |
35 ms |
5324 KB |
Output is correct |
4 |
Correct |
1676 ms |
120796 KB |
Output is correct |
5 |
Correct |
1800 ms |
122304 KB |
Output is correct |
6 |
Correct |
1774 ms |
122312 KB |
Output is correct |
7 |
Correct |
1811 ms |
121268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3302 ms |
115940 KB |
Output is correct |
2 |
Correct |
3509 ms |
115172 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1742 ms |
116924 KB |
Output is correct |
5 |
Correct |
3115 ms |
121004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3302 ms |
115940 KB |
Output is correct |
2 |
Correct |
3509 ms |
115172 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1742 ms |
116924 KB |
Output is correct |
5 |
Correct |
3115 ms |
121004 KB |
Output is correct |
6 |
Correct |
3635 ms |
114880 KB |
Output is correct |
7 |
Correct |
3575 ms |
115652 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
3494 ms |
115936 KB |
Output is correct |
11 |
Correct |
1816 ms |
116928 KB |
Output is correct |
12 |
Correct |
3242 ms |
114652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
5312 KB |
Output is correct |
2 |
Correct |
66 ms |
5420 KB |
Output is correct |
3 |
Correct |
39 ms |
5384 KB |
Output is correct |
4 |
Correct |
34 ms |
5376 KB |
Output is correct |
5 |
Correct |
35 ms |
4300 KB |
Output is correct |
6 |
Correct |
6 ms |
860 KB |
Output is correct |
7 |
Correct |
2044 ms |
53936 KB |
Output is correct |
8 |
Correct |
2075 ms |
53944 KB |
Output is correct |
9 |
Correct |
38 ms |
5388 KB |
Output is correct |
10 |
Correct |
1389 ms |
52528 KB |
Output is correct |
11 |
Correct |
1187 ms |
53208 KB |
Output is correct |
12 |
Correct |
1138 ms |
55216 KB |
Output is correct |
13 |
Correct |
1219 ms |
54208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
5312 KB |
Output is correct |
2 |
Correct |
66 ms |
5420 KB |
Output is correct |
3 |
Correct |
39 ms |
5384 KB |
Output is correct |
4 |
Correct |
34 ms |
5376 KB |
Output is correct |
5 |
Correct |
35 ms |
4300 KB |
Output is correct |
6 |
Correct |
6 ms |
860 KB |
Output is correct |
7 |
Correct |
1915 ms |
118196 KB |
Output is correct |
8 |
Correct |
1835 ms |
118184 KB |
Output is correct |
9 |
Correct |
35 ms |
5324 KB |
Output is correct |
10 |
Correct |
1676 ms |
120796 KB |
Output is correct |
11 |
Correct |
1800 ms |
122304 KB |
Output is correct |
12 |
Correct |
1774 ms |
122312 KB |
Output is correct |
13 |
Correct |
1811 ms |
121268 KB |
Output is correct |
14 |
Correct |
3302 ms |
115940 KB |
Output is correct |
15 |
Correct |
3509 ms |
115172 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1742 ms |
116924 KB |
Output is correct |
18 |
Correct |
3115 ms |
121004 KB |
Output is correct |
19 |
Correct |
3635 ms |
114880 KB |
Output is correct |
20 |
Correct |
3575 ms |
115652 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
3494 ms |
115936 KB |
Output is correct |
24 |
Correct |
1816 ms |
116928 KB |
Output is correct |
25 |
Correct |
3242 ms |
114652 KB |
Output is correct |
26 |
Correct |
2044 ms |
53936 KB |
Output is correct |
27 |
Correct |
2075 ms |
53944 KB |
Output is correct |
28 |
Correct |
38 ms |
5388 KB |
Output is correct |
29 |
Correct |
1389 ms |
52528 KB |
Output is correct |
30 |
Correct |
1187 ms |
53208 KB |
Output is correct |
31 |
Correct |
1138 ms |
55216 KB |
Output is correct |
32 |
Correct |
1219 ms |
54208 KB |
Output is correct |
33 |
Correct |
5512 ms |
118968 KB |
Output is correct |
34 |
Correct |
5610 ms |
118964 KB |
Output is correct |
35 |
Correct |
3915 ms |
118332 KB |
Output is correct |
36 |
Correct |
2932 ms |
126632 KB |
Output is correct |
37 |
Correct |
3026 ms |
126908 KB |
Output is correct |
38 |
Correct |
3711 ms |
120764 KB |
Output is correct |