#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
const int SZ=1<<18;
vector<int> P[250000];
vector<pair<int,int>> U[250000];
vector<long long> ans, x, y;
int tree[2*SZ];
long long X[250000], Y[250000];
void update(int n, int v)
{
for(++n;n<=250000;n+=n&-n) tree[n]+=v;
}
int get_cnt(int n)
{
int ret=0;
for(;n;n-=n&-n) ret+=tree[n];
return ret;
}
long long solve(long long K)
{
int j=0;
long long ret=0;
memset(tree,0,sizeof(tree));
for(int i=0;i<x.size();i++) {
for(;x[j]+K<x[i];j++) for(auto[u,k]: U[j]) update(u,-1);
for(auto[u,k]: U[i]) update(u,1);
for(auto[u,k]: U[i]) ret+=get_cnt(upper_bound(y.begin(),y.end(),y[u]+K)-y.begin())-get_cnt(lower_bound(y.begin(),y.end(),y[u]-K)-y.begin())-1;
}
return ret;
}
void add_tree(int n, int i)
{
P[n].push_back(i);
for(tree[n+=SZ]++;n>>=1;) tree[n]=tree[2*n]+tree[2*n+1];
}
void get_ans(int n1, int n2, int i, long long K, int bit=1, int s=0, int e=SZ-1)
{
int m=(s+e)>>1;
if(n2<n1 || n2<s || e<n1 || tree[bit]==0) return;
if(s==e) {
for(int j=0;j<P[m].size();j++) {
while(j<P[m].size() && abs(X[P[m][j]]-X[i])+abs(Y[P[m][j]]-Y[i])>K) {
swap(P[m][j],P[m].back());
P[m].pop_back();
}
if(j<P[m].size() && i!=P[m][j]) ans.push_back(abs(X[P[m][j]]-X[i])+abs(Y[P[m][j]]-Y[i]));
}
tree[bit]=P[m].size();
return;
}
get_ans(n1,n2,i,K,2*bit,s,m);
get_ans(n1,n2,i,K,2*bit+1,m+1,e);
tree[bit]=tree[2*bit]+tree[2*bit+1];
}
void solve2(long long K)
{
memset(tree,0,sizeof(tree));
for(int i=0;i<x.size();i++) {
for(auto[u,k]: U[i]) add_tree(u,k);
for(auto[u,k]: U[i]) get_ans((lower_bound(y.begin(),y.end(),y[u]-K)-y.begin()),(upper_bound(y.begin(),y.end(),y[u]+K)-y.begin()-1),k,K);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
int N, K;
long long s=0, e=4000000000LL;
cin>>N>>K;
for(int i=0;i<N;i++) {
cin>>X[i]>>Y[i];
x.push_back(X[i]+Y[i]);
y.push_back(X[i]-Y[i]);
}
sort(x.begin(),x.end()); x.resize(unique(x.begin(),x.end())-x.begin());
sort(y.begin(),y.end()); y.resize(unique(y.begin(),y.end())-y.begin());
for(int i=0;i<N;i++) U[lower_bound(x.begin(),x.end(),X[i]+Y[i])-x.begin()].emplace_back((lower_bound(y.begin(),y.end(),X[i]-Y[i])-y.begin()),i);
while(s<=e) {
long long m=(s+e)>>1;
if(solve(m)<K) s=m+1;
else e=m-1;
}
solve2(e);
sort(ans.begin(),ans.end());
assert(ans.size()==solve(e));
while(ans.size()<K) ans.push_back(s);
for(auto a: ans) cout<<a<<'\n';
return 0;
}
Compilation message
road_construction.cpp: In function 'long long int solve(long long int)':
road_construction.cpp:37:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i=0;i<x.size();i++) {
| ~^~~~~~~~~
road_construction.cpp: In function 'void get_ans(int, int, int, long long int, int, int, int)':
road_construction.cpp:56:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int j=0;j<P[m].size();j++) {
| ~^~~~~~~~~~~~
road_construction.cpp:57:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | while(j<P[m].size() && abs(X[P[m][j]]-X[i])+abs(Y[P[m][j]]-Y[i])>K) {
| ~^~~~~~~~~~~~
road_construction.cpp:61:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | if(j<P[m].size() && i!=P[m][j]) ans.push_back(abs(X[P[m][j]]-X[i])+abs(Y[P[m][j]]-Y[i]));
| ~^~~~~~~~~~~~
road_construction.cpp: In function 'void solve2(long long int)':
road_construction.cpp:74:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int i=0;i<x.size();i++) {
| ~^~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from road_construction.cpp:1:
road_construction.cpp: In function 'int main()':
road_construction.cpp:105:19: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
105 | assert(ans.size()==solve(e));
| ~~~~~~~~~~^~~~~~~~~~
road_construction.cpp:106:18: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
106 | while(ans.size()<K) ans.push_back(s);
| ~~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
18756 KB |
Output is correct |
2 |
Correct |
85 ms |
18768 KB |
Output is correct |
3 |
Correct |
74 ms |
18880 KB |
Output is correct |
4 |
Correct |
71 ms |
18864 KB |
Output is correct |
5 |
Incorrect |
75 ms |
17628 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3058 ms |
40864 KB |
Output is correct |
2 |
Correct |
3098 ms |
40772 KB |
Output is correct |
3 |
Correct |
67 ms |
18696 KB |
Output is correct |
4 |
Correct |
3017 ms |
40528 KB |
Output is correct |
5 |
Correct |
3048 ms |
40776 KB |
Output is correct |
6 |
Correct |
3138 ms |
40772 KB |
Output is correct |
7 |
Correct |
3064 ms |
39960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5674 ms |
37636 KB |
Output is correct |
2 |
Correct |
5726 ms |
37568 KB |
Output is correct |
3 |
Correct |
12 ms |
14028 KB |
Output is correct |
4 |
Correct |
2930 ms |
37492 KB |
Output is correct |
5 |
Correct |
1974 ms |
24992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5674 ms |
37636 KB |
Output is correct |
2 |
Correct |
5726 ms |
37568 KB |
Output is correct |
3 |
Correct |
12 ms |
14028 KB |
Output is correct |
4 |
Correct |
2930 ms |
37492 KB |
Output is correct |
5 |
Correct |
1974 ms |
24992 KB |
Output is correct |
6 |
Correct |
5743 ms |
37604 KB |
Output is correct |
7 |
Correct |
5573 ms |
37576 KB |
Output is correct |
8 |
Correct |
12 ms |
14028 KB |
Output is correct |
9 |
Correct |
12 ms |
14044 KB |
Output is correct |
10 |
Correct |
5491 ms |
36544 KB |
Output is correct |
11 |
Correct |
2973 ms |
37572 KB |
Output is correct |
12 |
Correct |
1953 ms |
25012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
18756 KB |
Output is correct |
2 |
Correct |
85 ms |
18768 KB |
Output is correct |
3 |
Correct |
74 ms |
18880 KB |
Output is correct |
4 |
Correct |
71 ms |
18864 KB |
Output is correct |
5 |
Incorrect |
75 ms |
17628 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
18756 KB |
Output is correct |
2 |
Correct |
85 ms |
18768 KB |
Output is correct |
3 |
Correct |
74 ms |
18880 KB |
Output is correct |
4 |
Correct |
71 ms |
18864 KB |
Output is correct |
5 |
Incorrect |
75 ms |
17628 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |