#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2.5e5 + 11;
const int INF = (1LL << 60);
const int SINF = (1LL << 32);
struct point{
int a, b;
};
vector<point> P;
int cx;
set<int> sx;
map<int, int> mx;
int d(point x, point y){
return max(abs(x.a - y.a), abs(x.b - y.b));
}
struct BIT2{
vector<int> Sp[N];
vector<int> Bi[N];
void build(const vector<point>& P){
for(auto p : P){
int x = mx[p.a], y = p.b;
for(int i = x; i < N; i += i & -i){
Sp[i].push_back(y);
}
}
for(int i = 0; i < N; i++){
Sp[i].push_back(-INF);
Sp[i].push_back(INF);
sort(Sp[i].begin(), Sp[i].end());
Sp[i].resize(unique(Sp[i].begin(), Sp[i].end()) - Sp[i].begin());
}
for(int i = 0; i < N; i++){
Bi[i].resize(Sp[i].size());
}
}
void add(int a, int b, int v){
int x = (mx.lower_bound(a))->second;
for(int i = x; i < N; i += i & -i){
int y = lower_bound(Sp[i].begin(), Sp[i].end(), b) - Sp[i].begin();
for(int j = y; j < Sp[i].size(); j += j & -j){
Bi[i][j] += v;
}
}
}
int qry(int a, int b){
int res = 0;
int x = (--mx.upper_bound(a))->second;
for(int i = x; i; i -= i & -i){
int y = (--upper_bound(Sp[i].begin(), Sp[i].end(), b)) - Sp[i].begin();
for(int j = y; j; j -= j & -j){
res += Bi[i][j];
}
}
return res;
}
int search(int a, int b, int r){
return qry(a + r, b + r)
- qry(a - r - 1, b + r)
- qry(a + r, b - r - 1)
+ qry(a - r - 1, b - r - 1);
}
} bit2;
int u[N];
int32_t main(){
int n, k; cin >> n >> k;
for(int i = 0; i < n; i++){
int x, y; cin >> x >> y;
P.push_back({x + y, x - y});
}
for(int i = 0; i < n; i++){
sx.insert(P[i].a);
}
mx[-INF] = cx++; for(auto i : sx) mx[i] = cx++; mx[INF] = cx++;
bit2.build(P);
for(int i = 0; i < n; i++){
bit2.add(P[i].a, P[i].b, 1);
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
fill(u, u + N, 1);
auto find_next = [&](int i){
int l = 0, r = SINF;
while(l != r){
int mid = (l + r) / 2;
int s = bit2.search(P[i].a, P[i].b, mid);
if(s > u[i]){
r = mid;
}else{
l = mid + 1;
}
}
pq.push({l, i});
};
for(int i = 0; i < n; i++) find_next(i);
while(k){
auto [a1, i1] = pq.top(); pq.pop();
auto [a2, i2] = pq.top(); pq.pop();
assert(a1 == a2);
cout << a1 << endl;
u[i1]++; u[i2]++; k--;
find_next(i1);
find_next(i2);
}
}
Compilation message
road_construction.cpp: In member function 'void BIT2::add(long long int, long long int, long long int)':
road_construction.cpp:48:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int j = y; j < Sp[i].size(); j += j & -j){
| ~~^~~~~~~~~~~~~~
road_construction.cpp: In function 'int32_t main()':
road_construction.cpp:115:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
115 | auto [a1, i1] = pq.top(); pq.pop();
| ^
road_construction.cpp:116:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
116 | auto [a2, i2] = pq.top(); pq.pop();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8626 ms |
32728 KB |
Output is correct |
2 |
Correct |
8406 ms |
32572 KB |
Output is correct |
3 |
Correct |
6040 ms |
32752 KB |
Output is correct |
4 |
Correct |
5497 ms |
32720 KB |
Output is correct |
5 |
Runtime error |
179 ms |
60792 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
10028 ms |
112564 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
10078 ms |
115000 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
10078 ms |
115000 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8626 ms |
32728 KB |
Output is correct |
2 |
Correct |
8406 ms |
32572 KB |
Output is correct |
3 |
Correct |
6040 ms |
32752 KB |
Output is correct |
4 |
Correct |
5497 ms |
32720 KB |
Output is correct |
5 |
Runtime error |
179 ms |
60792 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8626 ms |
32728 KB |
Output is correct |
2 |
Correct |
8406 ms |
32572 KB |
Output is correct |
3 |
Correct |
6040 ms |
32752 KB |
Output is correct |
4 |
Correct |
5497 ms |
32720 KB |
Output is correct |
5 |
Runtime error |
179 ms |
60792 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |