#include <bits/stdc++.h>
bool home = 1;
using namespace std;
typedef long long ll;
const int N=250000+7;
struct T{
ll x;
ll y;
int y_norm;
};
bool cmpTx(T a,T b) {
return a.x<b.x;
}
int n;
int k;
T a[N];
vector<ll> ys;
int aib[N];
void clr() {
for(int i=1;i<=n;i++) aib[i]=0;
}
void add(int i,int x) {
while (i<=n) {
aib[i]+=x;
i+=i&(-i);
}
}
int get(ll val) {
int low=0,high=(int)ys.size()-1,i=0,sol=0;
while (low<=high) {
int mid=(low+high)/2;
if(ys[mid]<=val) {
i=mid+1;
low=mid+1;
}else{
high=mid-1;
}
}
while(i) {
sol+=aib[i];
i-=i&(-i);
}
return sol;
}
ll how_many_under(ll d) {
clr();
ll sol=0;
int pt=1;
for (int i=1;i<=n;i++) {
while (a[i].x-a[pt].x>d) add(a[pt++].y_norm,-1);
sol+=get(a[i].y+d)-get(a[i].y-d-1);
add(a[i].y_norm,+1);
}
return sol;
}
signed main() {
#ifdef ONLINE_JUDGE
home = 0;
#endif
home=0;
if (home) {
freopen("I_am_iron_man", "r", stdin);
}
else {
ios::sync_with_stdio(0); cin.tie(0);
}
cin>>n>>k;
for (int i=1;i<=n;i++) {
int x,y;
cin>>x>>y;
a[i].x=x+y;
a[i].y=x-y;
ys.push_back(a[i].y);
}
sort(ys.begin(), ys.end());
ys.resize(unique(ys.begin(),ys.end())-ys.begin());
for (int i=1;i<=n;i++) {
a[i].y_norm=lower_bound(ys.begin(),ys.end(),a[i].y)-ys.begin()+1;
}
sort(a+1,a+n+1,cmpTx);
ll low=0,high=(ll)1e10,sol=-1;
while (low<=high) {
ll mid=(low+high)/2;
if(how_many_under(mid)<=k) {
sol=mid;
low=mid+1;
}else{
high=mid-1;
}
}
vector<ll> v;
multiset<pair<ll, int>> s;
int pt=1;
for (int i=1;i<=n;i++) {
while (a[i].x-a[pt].x>sol) s.erase(s.find({a[pt].y, pt++}));
ll low=a[i].y-sol;
ll high=a[i].y+sol;
auto it=s.lower_bound({low, -1});
while (it!=s.end()&&it->first<=high) {
int j=it->second;
v.push_back(max(a[i].x-a[j].x,abs(a[i].y-a[j].y)));
it++;
}
s.insert({a[i].y, i});
}
sort(v.begin(),v.end());
assert((int)v.size()<=k);
v.resize(k, sol+1);
for (auto &x: v) {
cout<<x<<"\n";
}
}
Compilation message
road_construction.cpp: In function 'int main()':
road_construction.cpp:117:59: warning: operation on 'pt' may be undefined [-Wsequence-point]
117 | while (a[i].x-a[pt].x>sol) s.erase(s.find({a[pt].y, pt++}));
| ~~^~
road_construction.cpp:76:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen("I_am_iron_man", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
5024 KB |
Output is correct |
2 |
Correct |
44 ms |
5032 KB |
Output is correct |
3 |
Correct |
40 ms |
5076 KB |
Output is correct |
4 |
Correct |
51 ms |
5048 KB |
Output is correct |
5 |
Correct |
42 ms |
3888 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1475 ms |
12504 KB |
Output is correct |
2 |
Correct |
1419 ms |
15412 KB |
Output is correct |
3 |
Correct |
36 ms |
4932 KB |
Output is correct |
4 |
Correct |
1411 ms |
15264 KB |
Output is correct |
5 |
Correct |
1369 ms |
15396 KB |
Output is correct |
6 |
Correct |
1431 ms |
15424 KB |
Output is correct |
7 |
Correct |
1364 ms |
15360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2979 ms |
9256 KB |
Output is correct |
2 |
Correct |
2883 ms |
14276 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1413 ms |
12092 KB |
Output is correct |
5 |
Correct |
1403 ms |
14516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2979 ms |
9256 KB |
Output is correct |
2 |
Correct |
2883 ms |
14276 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1413 ms |
12092 KB |
Output is correct |
5 |
Correct |
1403 ms |
14516 KB |
Output is correct |
6 |
Correct |
3008 ms |
14268 KB |
Output is correct |
7 |
Correct |
2939 ms |
14268 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2998 ms |
14340 KB |
Output is correct |
11 |
Correct |
1344 ms |
12096 KB |
Output is correct |
12 |
Correct |
1374 ms |
14572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
5024 KB |
Output is correct |
2 |
Correct |
44 ms |
5032 KB |
Output is correct |
3 |
Correct |
40 ms |
5076 KB |
Output is correct |
4 |
Correct |
51 ms |
5048 KB |
Output is correct |
5 |
Correct |
42 ms |
3888 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
1150 ms |
7984 KB |
Output is correct |
8 |
Correct |
1118 ms |
9976 KB |
Output is correct |
9 |
Correct |
40 ms |
5104 KB |
Output is correct |
10 |
Correct |
1077 ms |
9368 KB |
Output is correct |
11 |
Correct |
984 ms |
9128 KB |
Output is correct |
12 |
Correct |
514 ms |
9848 KB |
Output is correct |
13 |
Correct |
480 ms |
8572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
5024 KB |
Output is correct |
2 |
Correct |
44 ms |
5032 KB |
Output is correct |
3 |
Correct |
40 ms |
5076 KB |
Output is correct |
4 |
Correct |
51 ms |
5048 KB |
Output is correct |
5 |
Correct |
42 ms |
3888 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
1475 ms |
12504 KB |
Output is correct |
8 |
Correct |
1419 ms |
15412 KB |
Output is correct |
9 |
Correct |
36 ms |
4932 KB |
Output is correct |
10 |
Correct |
1411 ms |
15264 KB |
Output is correct |
11 |
Correct |
1369 ms |
15396 KB |
Output is correct |
12 |
Correct |
1431 ms |
15424 KB |
Output is correct |
13 |
Correct |
1364 ms |
15360 KB |
Output is correct |
14 |
Correct |
2979 ms |
9256 KB |
Output is correct |
15 |
Correct |
2883 ms |
14276 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1413 ms |
12092 KB |
Output is correct |
18 |
Correct |
1403 ms |
14516 KB |
Output is correct |
19 |
Correct |
3008 ms |
14268 KB |
Output is correct |
20 |
Correct |
2939 ms |
14268 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
2998 ms |
14340 KB |
Output is correct |
24 |
Correct |
1344 ms |
12096 KB |
Output is correct |
25 |
Correct |
1374 ms |
14572 KB |
Output is correct |
26 |
Correct |
1150 ms |
7984 KB |
Output is correct |
27 |
Correct |
1118 ms |
9976 KB |
Output is correct |
28 |
Correct |
40 ms |
5104 KB |
Output is correct |
29 |
Correct |
1077 ms |
9368 KB |
Output is correct |
30 |
Correct |
984 ms |
9128 KB |
Output is correct |
31 |
Correct |
514 ms |
9848 KB |
Output is correct |
32 |
Correct |
480 ms |
8572 KB |
Output is correct |
33 |
Correct |
3065 ms |
18228 KB |
Output is correct |
34 |
Correct |
3170 ms |
18212 KB |
Output is correct |
35 |
Correct |
2807 ms |
17584 KB |
Output is correct |
36 |
Correct |
1262 ms |
18356 KB |
Output is correct |
37 |
Correct |
1339 ms |
18312 KB |
Output is correct |
38 |
Correct |
1426 ms |
17516 KB |
Output is correct |