#include <bits/stdc++.h>
#define fi first
#define se second
const int N = 500500;
const int mod = 1e9 + 7;
using namespace std;
int n;
int id[N];
int t[N];
pair<long long, long long> a[N];
vector<pair<long long, int>> v;
void upd(int x, int g)
{
while(x < N) {
t[x] += g;
x += x & -x;
}
}
int get_c(int x)
{
int res = 0;
while(x > 0) {
res += t[x];
x -= x & -x;
}
return res;
}
int get_f(int k)
{
int res = 0;
for (int i = 20; i >= 0; i--) {
if (res + (1 << i) >= N) {
continue;
} else if (t[res + (1 << i)] < k) {
res += (1 << i);
k -= t[res];
}
}
return res + 1;
}
long long get(long long D)
{
long long res = 0;
for(int i = 0; i < N; i++) t[i] = 0;
for(int i = 1, j = 1; i <= n; i++) {
if(i > 1){
upd(id[i - 1], 1);
}
while(j < i && a[i].fi - a[j].fi > D) {
upd(id[j], -1);
j += 1;
}
int l = lower_bound(v.begin(), v.end(), make_pair(a[i].se - D, 0)) - v.begin();
int r = lower_bound(v.begin(), v.end(), make_pair(a[i].se + D + 1, 0)) - v.begin() - 1;
if(l <= r) {
res += get_c(v[r].se) - get_c(v[l].se - 1);
}
}
return res;
}
queue<int> Q[N];
void add_res(vector<long long> &res, queue<int> q, int i)
{
while (!q.empty()) {
int j = q.front();
q.pop();
res.push_back(max(abs(a[i].fi - a[j].fi), abs(a[i].se - a[j].se)));
}
}
vector<long long> solve(long long D)
{
vector<long long> res;
for(int i = 0; i < N; i++) t[i] = 0;
for(int i = 1, j = 1; i <= n; i++) {
if(i > 1){
upd(id[i - 1], 1);
Q[id[i - 1]].push(i - 1);
}
while(j < i && a[i].fi - a[j].fi > D) {
upd(id[j], -1);
Q[id[j]].pop();
j += 1;
}
int l = lower_bound(v.begin(), v.end(), make_pair(a[i].se - D, 0)) - v.begin();
int r = lower_bound(v.begin(), v.end(), make_pair(a[i].se + D + 1, 0)) - v.begin() - 1;
if(l <= r) {
int tr = get_c(v[r].se);
int tl = get_c(v[l].se - 1);
while (tl < tr) {
int pos = get_f(tl + 1);
add_res(res, Q[pos], i);
tl += Q[pos].size();
}
}
}
return res;
}
int main() {
#ifdef zxc
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
long long k;
cin >> n >> k;
for(int i = 1; i <= n; i++) {
long long x, y;
cin >> x >> y;
a[i] = {x + y, x - y};
}
sort(a + 1, a + n + 1);
v.push_back({-1e11, 0});
v.push_back({1e11, 0});
for(int i = 1; i <= n; i++) {
v.push_back({a[i].se, i});
}
sort(v.begin(), v.end());
for(int i = 0, j = 0; i < v.size(); i++) {
if(i > 0 && v[i].fi > v[i - 1].fi) {
j += 1;
}
id[v[i].se] = j;
v[i].se = j;
}
long long l = 0, r = 4e9;
while(l < r) {
long long m = (l + r) / 2;
if(get(m + 1) < k) {
l = m + 1;
} else {
r = m;
}
}
auto res = solve(l);
while (res.size() < k) {
res.push_back(l + 1);
}
sort(res.begin(), res.end());
for (int x: res) {
cout << x << "\n";
}
}
Compilation message
road_construction.cpp: In function 'int main()':
road_construction.cpp:136:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | for(int i = 0, j = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
road_construction.cpp:155:27: 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]
155 | while (res.size() < k) {
| ~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
343864 KB |
Output is correct |
2 |
Correct |
237 ms |
343924 KB |
Output is correct |
3 |
Incorrect |
234 ms |
343936 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1500 ms |
353164 KB |
Output is correct |
2 |
Correct |
1495 ms |
353204 KB |
Output is correct |
3 |
Correct |
232 ms |
343836 KB |
Output is correct |
4 |
Correct |
1487 ms |
353012 KB |
Output is correct |
5 |
Correct |
1481 ms |
353276 KB |
Output is correct |
6 |
Correct |
1522 ms |
353268 KB |
Output is correct |
7 |
Correct |
1432 ms |
352536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2866 ms |
348048 KB |
Output is correct |
2 |
Correct |
2870 ms |
348208 KB |
Output is correct |
3 |
Incorrect |
172 ms |
339148 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2866 ms |
348048 KB |
Output is correct |
2 |
Correct |
2870 ms |
348208 KB |
Output is correct |
3 |
Incorrect |
172 ms |
339148 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
343864 KB |
Output is correct |
2 |
Correct |
237 ms |
343924 KB |
Output is correct |
3 |
Incorrect |
234 ms |
343936 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
343864 KB |
Output is correct |
2 |
Correct |
237 ms |
343924 KB |
Output is correct |
3 |
Incorrect |
234 ms |
343936 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |