#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 = 2.5e5 + 5;
struct FenwickTree {
int t[N];
void update(int x, int k) {
for(int i = x; i < N; i += i & -i)
t[i] += k;
}
int query(int x, int k = 0) {
for(int i = x; i; i -= i & -i)
k += t[i];
return k;
}
} tree;
int n, k;
int y[N], y_ord[N], l[N], r[N];
pii A[N];
vector<long> coord;
int get(long x) {
return lower_bound(coord.begin(), coord.end(), x) - coord.begin() + 1;
}
int main() {
iota(y_ord, y_ord + N, 0);
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(coord.begin(), coord.end());
coord.resize(unique(coord.begin(), coord.end()) - coord.begin());
sort(A + 1, A + n + 1);
for(int i = 1; i <= n; i++)
y[i] = get(A[i].y);
sort(y_ord + 1, y_ord + n + 1, [&](int a, int b) {
return A[a].y < A[b].y;
});
long dist = 0;
for(int bit = 31; ~bit; bit--) {
long now = dist | (1ll << bit), cnt = 0;
for(int i = 1; i <= n; i++) {
int lhs = l[y_ord[i - 1]];
int rhs = r[y_ord[i - 1]];
while(rhs + 1 <= coord.size() && coord[rhs] <= A[y_ord[i]].y + now)
++rhs;
while(lhs + 1 <= coord.size() && coord[lhs] < A[y_ord[i]].y - now)
++lhs;
l[y_ord[i]] = lhs, r[y_ord[i]] = rhs;
}
int j = 1;
for(int i = 1; i <= n; i++) {
while(j < i && A[i].x - A[j].x > now) {
tree.update(y[j], -1);
++j;
}
cnt += tree.query(r[i]) - tree.query(l[i]);
tree.update(y[i], 1);
}
for(; j <= n; j++) tree.update(y[j], -1);
if(cnt < k) dist = now;
}
vector<long> ans;
set<pii> st;
for(int i = 1, j = 1; i <= n; i++) {
while(j < i && A[i].x - A[j].x > dist) {
st.erase(pii(A[j].y, j));
++j;
}
auto it = st.lower_bound(pii(A[i].y - dist, -1e18));
while(it != st.end() && it->x <= A[i].y + dist) {
long ta = A[i].x - A[it->y].x;
long tb = A[i].y - A[it->y].y;
ans.emplace_back(abs((ta + tb) / 2) + abs((ta - tb) / 2));
++it;
}
st.emplace(pii(A[i].y, i));
}
while(ans.size() < k) ans.emplace_back(dist + 1);
sort(ans.begin(), ans.end());
for(long x : ans) printf("%lld\n", x);
return 0;
}
Compilation message
road_construction.cpp: In function 'int main()':
road_construction.cpp:66:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | while(rhs + 1 <= coord.size() && coord[rhs] <= A[y_ord[i]].y + now)
| ~~~~~~~~^~~~~~~~~~~~~~~
road_construction.cpp:68:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | while(lhs + 1 <= coord.size() && coord[lhs] < A[y_ord[i]].y - now)
| ~~~~~~~~^~~~~~~~~~~~~~~
road_construction.cpp:111:22: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
111 | while(ans.size() < k) ans.emplace_back(dist + 1);
| ~~~~~~~~~~~^~~
road_construction.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
road_construction.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%lld %lld", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
5960 KB |
Output is correct |
2 |
Correct |
60 ms |
6016 KB |
Output is correct |
3 |
Correct |
59 ms |
6068 KB |
Output is correct |
4 |
Correct |
59 ms |
6116 KB |
Output is correct |
5 |
Correct |
56 ms |
4912 KB |
Output is correct |
6 |
Correct |
5 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
951 ms |
14668 KB |
Output is correct |
2 |
Correct |
969 ms |
17316 KB |
Output is correct |
3 |
Correct |
53 ms |
5932 KB |
Output is correct |
4 |
Correct |
867 ms |
17236 KB |
Output is correct |
5 |
Correct |
755 ms |
17432 KB |
Output is correct |
6 |
Correct |
750 ms |
17412 KB |
Output is correct |
7 |
Correct |
766 ms |
17280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1282 ms |
11516 KB |
Output is correct |
2 |
Correct |
1318 ms |
16248 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
707 ms |
14004 KB |
Output is correct |
5 |
Correct |
916 ms |
15636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1282 ms |
11516 KB |
Output is correct |
2 |
Correct |
1318 ms |
16248 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
707 ms |
14004 KB |
Output is correct |
5 |
Correct |
916 ms |
15636 KB |
Output is correct |
6 |
Correct |
1344 ms |
16156 KB |
Output is correct |
7 |
Correct |
1325 ms |
16164 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
1290 ms |
16268 KB |
Output is correct |
11 |
Correct |
698 ms |
14000 KB |
Output is correct |
12 |
Correct |
890 ms |
15448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
5960 KB |
Output is correct |
2 |
Correct |
60 ms |
6016 KB |
Output is correct |
3 |
Correct |
59 ms |
6068 KB |
Output is correct |
4 |
Correct |
59 ms |
6116 KB |
Output is correct |
5 |
Correct |
56 ms |
4912 KB |
Output is correct |
6 |
Correct |
5 ms |
1356 KB |
Output is correct |
7 |
Correct |
566 ms |
9292 KB |
Output is correct |
8 |
Correct |
568 ms |
11332 KB |
Output is correct |
9 |
Correct |
57 ms |
6064 KB |
Output is correct |
10 |
Correct |
533 ms |
10676 KB |
Output is correct |
11 |
Correct |
461 ms |
10420 KB |
Output is correct |
12 |
Correct |
364 ms |
10996 KB |
Output is correct |
13 |
Correct |
370 ms |
9548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
5960 KB |
Output is correct |
2 |
Correct |
60 ms |
6016 KB |
Output is correct |
3 |
Correct |
59 ms |
6068 KB |
Output is correct |
4 |
Correct |
59 ms |
6116 KB |
Output is correct |
5 |
Correct |
56 ms |
4912 KB |
Output is correct |
6 |
Correct |
5 ms |
1356 KB |
Output is correct |
7 |
Correct |
951 ms |
14668 KB |
Output is correct |
8 |
Correct |
969 ms |
17316 KB |
Output is correct |
9 |
Correct |
53 ms |
5932 KB |
Output is correct |
10 |
Correct |
867 ms |
17236 KB |
Output is correct |
11 |
Correct |
755 ms |
17432 KB |
Output is correct |
12 |
Correct |
750 ms |
17412 KB |
Output is correct |
13 |
Correct |
766 ms |
17280 KB |
Output is correct |
14 |
Correct |
1282 ms |
11516 KB |
Output is correct |
15 |
Correct |
1318 ms |
16248 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 KB |
Output is correct |
17 |
Correct |
707 ms |
14004 KB |
Output is correct |
18 |
Correct |
916 ms |
15636 KB |
Output is correct |
19 |
Correct |
1344 ms |
16156 KB |
Output is correct |
20 |
Correct |
1325 ms |
16164 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
1 ms |
1228 KB |
Output is correct |
23 |
Correct |
1290 ms |
16268 KB |
Output is correct |
24 |
Correct |
698 ms |
14000 KB |
Output is correct |
25 |
Correct |
890 ms |
15448 KB |
Output is correct |
26 |
Correct |
566 ms |
9292 KB |
Output is correct |
27 |
Correct |
568 ms |
11332 KB |
Output is correct |
28 |
Correct |
57 ms |
6064 KB |
Output is correct |
29 |
Correct |
533 ms |
10676 KB |
Output is correct |
30 |
Correct |
461 ms |
10420 KB |
Output is correct |
31 |
Correct |
364 ms |
10996 KB |
Output is correct |
32 |
Correct |
370 ms |
9548 KB |
Output is correct |
33 |
Correct |
1447 ms |
20156 KB |
Output is correct |
34 |
Correct |
1449 ms |
20132 KB |
Output is correct |
35 |
Correct |
1353 ms |
19276 KB |
Output is correct |
36 |
Correct |
911 ms |
19332 KB |
Output is correct |
37 |
Correct |
922 ms |
19296 KB |
Output is correct |
38 |
Correct |
959 ms |
18520 KB |
Output is correct |