#include <bits/stdc++.h>
#define long long long
#define pii pair<long, long>
#define x first
#define y second
using namespace std;
const int N = 1 << 18;
unordered_set<int> leaf[N];
int t[N << 1];
void update(int x, int k, int idx, bool add, int p = 1, int l = 1, int r = N) {
if(l > x || x > r) return;
if(l == r) {
t[p] += k;
if(add) {
if(k == 1) leaf[l].emplace(idx);
else leaf[l].erase(idx);
}
return;
}
int mid = (l + r) >> 1;
update(x, k, idx, add, p << 1, l, mid), update(x, k, idx, add, p << 1 | 1, mid + 1, r);
t[p] = t[p << 1] + t[p << 1 | 1];
}
int query(int x, int y, int p = 1, int l = 1, int r = N) {
if(x > r || l > y) return 0;
if(x <= l && r <= y) return t[p];
int mid = (l + r) >> 1;
return query(x, y, p << 1, l, mid) + query(x, y, p << 1 | 1, mid + 1, r);
}
void get_answer(int x, int y, vector<int> &vec, int p = 1, int l = 1, int r = N) {
if(x > r || l > y || !t[p]) return;
if(l == r) {
for(int x : leaf[l]) vec.emplace_back(x);
return;
}
int mid = (l + r) >> 1;
get_answer(x, y, vec, p << 1, l, mid), get_answer(x, y, vec, p << 1 | 1, mid + 1, r);
}
int n, k;
pii A[N];
vector<long> coord;
int get(long x) {
return lower_bound(coord.begin(), coord.end(), x) - coord.begin() + 1;
}
int main() {
scanf("%d %d", &n, &k);
for(int i = 1; i <= n; i++) {
long a, b;
scanf("%lld %lld", &a, &b);
A[i] = pii(a + b, a - b);
coord.emplace_back(A[i].y);
}
sort(A + 1, A + n + 1);
sort(coord.begin(), coord.end());
coord.resize(unique(coord.begin(), coord.end()) - coord.begin());
long l = 0, r = 4e9;
while(l < r) {
long mid = (l + r) >> 1;
memset(t, 0, sizeof t);
int cnt = 0;
for(int i = 1, j = 1; i <= n; i++) {
while(j < i && A[i].x - A[j].x > mid) {
update(get(A[j].y), -1, j, 0);
++j;
}
cnt += query(get(A[i].y - mid), get(A[i].y + mid + 1) - 1);
update(get(A[i].y), 1, i, 0);
}
if(cnt >= k) r = mid;
else l = mid + 1;
}
vector<long> ans;
memset(t, 0, sizeof t);
for(int i = 1, j = 1; i <= n; i++) {
while(j < i && A[i].x - A[j].x > r - 1) {
update(get(A[j].y), -1, j, 1);
++j;
}
vector<int> idx;
get_answer(get(A[i].y - r + 1), get(A[i].y + r) - 1, idx);
for(int x : idx) {
long ta = A[i].x - A[x].x;
long tb = A[i].y - A[x].y;
ans.emplace_back(abs((ta + tb) / 2) + abs((ta - tb) / 2));
}
update(get(A[i].y), 1, i, 1);
}
while(ans.size() < k) ans.emplace_back(r);
sort(ans.begin(), ans.end());
for(int i = 0; i < k; i++) printf("%lld\n", ans[i]);
return 0;
}
Compilation message
road_construction.cpp: In function 'int main()':
road_construction.cpp:110:22: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
110 | while(ans.size() < k) ans.emplace_back(r);
| ~~~~~~~~~~~^~~
road_construction.cpp:56:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
road_construction.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%lld %lld", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
21380 KB |
Output is correct |
2 |
Correct |
107 ms |
21316 KB |
Output is correct |
3 |
Correct |
85 ms |
21424 KB |
Output is correct |
4 |
Correct |
86 ms |
21452 KB |
Output is correct |
5 |
Correct |
105 ms |
20400 KB |
Output is correct |
6 |
Correct |
38 ms |
16736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
10088 ms |
1075080 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
10024 ms |
22576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
10024 ms |
22576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
21380 KB |
Output is correct |
2 |
Correct |
107 ms |
21316 KB |
Output is correct |
3 |
Correct |
85 ms |
21424 KB |
Output is correct |
4 |
Correct |
86 ms |
21452 KB |
Output is correct |
5 |
Correct |
105 ms |
20400 KB |
Output is correct |
6 |
Correct |
38 ms |
16736 KB |
Output is correct |
7 |
Execution timed out |
10047 ms |
1071348 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
21380 KB |
Output is correct |
2 |
Correct |
107 ms |
21316 KB |
Output is correct |
3 |
Correct |
85 ms |
21424 KB |
Output is correct |
4 |
Correct |
86 ms |
21452 KB |
Output is correct |
5 |
Correct |
105 ms |
20400 KB |
Output is correct |
6 |
Correct |
38 ms |
16736 KB |
Output is correct |
7 |
Execution timed out |
10088 ms |
1075080 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |