#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll n,k,b[750005],x[750005],y[750005],Nu[250005],Fi[250005],Se[250005];
unordered_map<ll,ll>M1,M2;
vector<int>beg[750005],er[750005];
vector<long long>A;
multiset<pair<ll,int> >st;
multiset<pair<ll,int>>::iterator it;
void add(int y,int x){
while(y < 750005){
b[y] += x;
y += y&-y;
}
}
int sum(int y){
int x = 0;
while(y > 0){
x += b[y];
y -= y&-y;
}
return x;
}
ll calc(ll t, bool flag){
vector<ll>v,g;
M1.clear();
M2.clear();
for(int i=1; i<=3 * n; i++){
beg[i].clear();
er[i].clear();
}
for(int i=1; i<=n; i++){
v.pb(x[i] + y[i]);
v.pb(x[i] + y[i] + t + 1);
g.pb(x[i] - y[i] - t);
g.pb(x[i] - y[i]);
g.pb(x[i] - y[i] + t);
}
sort(v.begin() , v.end());
sort(g.begin() , g.end());
int p = 0;
for(int i=0; i<v.size(); i++){
if(!i || v[i] != v[i - 1])p++;
M1[v[i]] = p;
}
p = 0;
for(int i=0; i<g.size(); i++){
if(!i || g[i] != g[i - 1])p++;
M2[g[i]] = p;
}
vector<pair<ll,ll> >f;
for(int i=1; i<=n; i++){
er[M1[x[i] + y[i] + t + 1]].pb(i);
beg[M1[x[i] + y[i]]].pb(i);
f.pb({x[i] + y[i] , i});
Nu[i] = M2[x[i] - y[i]];
Fi[i] = M2[x[i] - y[i] - t];
Se[i] = M2[x[i] - y[i] + t];
}
sort(f.begin() , f.end());
ll ret = 0;
for(int i=1; i<=2 * n; i++){
for(auto ind : er[i]){
add(Nu[ind] , -1);
if(flag)st.erase(st.find({Nu[ind] , ind}));
}
for(auto ind : beg[i]){
ret += sum(Se[ind]) - sum(Fi[ind] - 1);
if(flag)it = st.lower_bound({Fi[ind] , 0});
if(flag)
while(it != st.end() && (*it).f <= Se[ind]){
A.pb(abs(x[ind] - x[(*it).s]) + abs(y[ind] - y[(*it).s]));
it++;
}
add(Nu[ind] , 1);
if(flag)st.insert({Nu[ind] , ind});
}
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin >> n >> k;
for(int i=1; i<=n; i++){
cin >> x[i] >> y[i];
}
ll l=0,r = 4000000000,mid,ans = 0;
while(r >= l){
mid = (l + r) / 2;
if(calc(mid , 0) >= k){
ans = mid;
r = mid - 1;
}
else{
l = mid + 1;
}
}
calc(ans - 1 , 1);
sort(A.begin() , A.end());
for(auto F : A)
cout << F << '\n';
for(int i=0; i<k - (int)A.size(); i++)
cout << ans << '\n';
return 0;
}
Compilation message
road_construction.cpp: In function 'long long int calc(long long int, bool)':
road_construction.cpp:46:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i=0; i<v.size(); i++){
| ~^~~~~~~~~
road_construction.cpp:51:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i=0; i<g.size(); i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
40736 KB |
Output is correct |
2 |
Correct |
116 ms |
40684 KB |
Output is correct |
3 |
Correct |
101 ms |
40716 KB |
Output is correct |
4 |
Correct |
106 ms |
40668 KB |
Output is correct |
5 |
Correct |
109 ms |
39600 KB |
Output is correct |
6 |
Correct |
45 ms |
35788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
10033 ms |
158664 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
10064 ms |
158636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
10064 ms |
158636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
40736 KB |
Output is correct |
2 |
Correct |
116 ms |
40684 KB |
Output is correct |
3 |
Correct |
101 ms |
40716 KB |
Output is correct |
4 |
Correct |
106 ms |
40668 KB |
Output is correct |
5 |
Correct |
109 ms |
39600 KB |
Output is correct |
6 |
Correct |
45 ms |
35788 KB |
Output is correct |
7 |
Execution timed out |
10044 ms |
85400 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
40736 KB |
Output is correct |
2 |
Correct |
116 ms |
40684 KB |
Output is correct |
3 |
Correct |
101 ms |
40716 KB |
Output is correct |
4 |
Correct |
106 ms |
40668 KB |
Output is correct |
5 |
Correct |
109 ms |
39600 KB |
Output is correct |
6 |
Correct |
45 ms |
35788 KB |
Output is correct |
7 |
Execution timed out |
10033 ms |
158664 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |