#include <bits/stdc++.h>
using namespace std;
const int N = 2.5e5 + 15;
const int INF = 2e9 + 1;
struct node {
node *l, *r;
pair<int, int> mn;
node(node *l_, node *r_, pair<int, int> mn_) : l(l_), r(r_), mn(mn_) {}
};
node *copy(node *t) {
return new node(t->l, t->r, t->mn);
}
node *build(int tl, int tr) {
if (tr - tl == 1) {
return new node(0, 0, {INF, INF});
}
int tm = (tl + tr) / 2;
return new node(build(tl, tm), build(tm, tr), {INF, INF});
}
pair<int, int> get(node *t, int tl, int tr, int l, int r) {
if (tl >= r || tr <= l) {
return {INF, INF};
}
if (tl >= l && tr <= r) {
return t->mn;
}
int tm = (tl + tr) / 2;
return min(get(t->l, tl, tm, l, r), get(t->r, tm, tr, l, r));
}
void update(node *&t, int tl, int tr, int pos, pair<int, int> mn) {
t = copy(t);
if (tr - tl == 1) {
t->mn = mn;
return;
}
int tm = (tl + tr) / 2;
if (pos < tm) {
update(t->l, tl, tm, pos, mn);
} else {
update(t->r, tm, tr, pos, mn);
}
t->mn = min(t->l->mn, t->r->mn);
}
int n, k;
int x[N], y[N];
vector<pair<long long, pair<int, int>>> D;
node *t[N];
int ordx[N], ordy[N];
int posx[N], posy[N];
int ry[N];
void go(int fx, int fy) {
// cout << "--- " << fx << " " << fy << " ---\n";
iota(ordx, ordx + n, 0);
iota(ordy, ordy + n, 0);
sort(ordx, ordx + n, [&](int i, int j) {
return x[i] < x[j] || (x[i] == x[j] && y[i] < y[j]) || (x[i] == x[j] && y[i] == y[j] && i < j);
});
sort(ordy, ordy + n, [&](int i, int j) {
return y[i] < y[j] || (y[i] == y[j] && x[i] < x[j]) || (y[i] == y[j] && x[i] == x[j] && i < j);
});
for (int i = 0; i < n; ++i) {
posx[ordx[i]] = i;
posy[ordy[i]] = i;
}
node *cur = build(0, n);
for (int i = 0, j = 0; i < n; ++i) {
while (x[ordx[j]] <= x[ordx[i]] - fx && j < i) {
int id = ordx[j];
update(cur, 0, n, posy[id], {-x[id] - y[id], posy[id]});
++j;
}
t[ordx[i]] = cur;
}
for (int i = 0, j = 0; i < n; ++i) {
while (y[ordy[j]] <= y[ordy[i]] - fy && j < i) {
++j;
}
ry[ordy[i]] = j;
}
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> q;
for (int i = 0; i < n; ++i) {
auto mn = get(t[i], 0, n, 0, ry[i]);
if (mn.first != INF) q.push({(long long)mn.first + x[i] + y[i], i});
}
for (int _ = 0; _ < 2 * k && !q.empty(); ++_) {
int i = q.top().second;
q.pop();
auto mn = get(t[i], 0, n, 0, ry[i]);
D.push_back({(long long)mn.first + x[i] + y[i], {min(i, ordy[mn.second]), max(i, ordy[mn.second])}});
update(t[i], 0, n, mn.second, {INF, INF});
mn = get(t[i], 0, n, 0, ry[i]);
if (mn.first != INF) q.push({(long long)mn.first + x[i] + y[i], i});
}
}
void solve() {
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> x[i] >> y[i];
}
go(0, 0);
for (int i = 0; i < n; ++i) {
y[i] *= -1;
}
go(0, 1);
for (int i = 0; i < n; ++i) {
x[i] *= -1;
y[i] *= -1;
}
go(1, 0);
for (int i = 0; i < n; ++i) {
y[i] *= -1;
}
go(1, 1);
sort(D.begin(), D.end());
D.resize(unique(D.begin(), D.end()) - D.begin());
D.resize(k);
for (int i = 0; i < k; ++i) {
cout << D[i].first << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1392 ms |
363564 KB |
Output is correct |
2 |
Correct |
1288 ms |
363524 KB |
Output is correct |
3 |
Correct |
595 ms |
176752 KB |
Output is correct |
4 |
Correct |
629 ms |
176804 KB |
Output is correct |
5 |
Correct |
1138 ms |
362452 KB |
Output is correct |
6 |
Correct |
18 ms |
7760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4046 ms |
1277896 KB |
Output is correct |
2 |
Correct |
3731 ms |
1277716 KB |
Output is correct |
3 |
Correct |
495 ms |
176684 KB |
Output is correct |
4 |
Correct |
3167 ms |
1277624 KB |
Output is correct |
5 |
Correct |
3278 ms |
1277760 KB |
Output is correct |
6 |
Correct |
3245 ms |
1277628 KB |
Output is correct |
7 |
Correct |
3358 ms |
1276880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3254 ms |
675372 KB |
Output is correct |
2 |
Correct |
3216 ms |
675664 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1466 ms |
667344 KB |
Output is correct |
5 |
Correct |
2126 ms |
674924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3254 ms |
675372 KB |
Output is correct |
2 |
Correct |
3216 ms |
675664 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1466 ms |
667344 KB |
Output is correct |
5 |
Correct |
2126 ms |
674924 KB |
Output is correct |
6 |
Correct |
3464 ms |
675516 KB |
Output is correct |
7 |
Correct |
3376 ms |
675396 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3466 ms |
675444 KB |
Output is correct |
11 |
Correct |
1698 ms |
667348 KB |
Output is correct |
12 |
Correct |
2354 ms |
674812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1392 ms |
363564 KB |
Output is correct |
2 |
Correct |
1288 ms |
363524 KB |
Output is correct |
3 |
Correct |
595 ms |
176752 KB |
Output is correct |
4 |
Correct |
629 ms |
176804 KB |
Output is correct |
5 |
Correct |
1138 ms |
362452 KB |
Output is correct |
6 |
Correct |
18 ms |
7760 KB |
Output is correct |
7 |
Correct |
8356 ms |
1393048 KB |
Output is correct |
8 |
Correct |
8170 ms |
1395048 KB |
Output is correct |
9 |
Correct |
696 ms |
176732 KB |
Output is correct |
10 |
Correct |
6861 ms |
1394432 KB |
Output is correct |
11 |
Correct |
6186 ms |
1394204 KB |
Output is correct |
12 |
Correct |
4523 ms |
1395116 KB |
Output is correct |
13 |
Correct |
5001 ms |
1393236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1392 ms |
363564 KB |
Output is correct |
2 |
Correct |
1288 ms |
363524 KB |
Output is correct |
3 |
Correct |
595 ms |
176752 KB |
Output is correct |
4 |
Correct |
629 ms |
176804 KB |
Output is correct |
5 |
Correct |
1138 ms |
362452 KB |
Output is correct |
6 |
Correct |
18 ms |
7760 KB |
Output is correct |
7 |
Correct |
4046 ms |
1277896 KB |
Output is correct |
8 |
Correct |
3731 ms |
1277716 KB |
Output is correct |
9 |
Correct |
495 ms |
176684 KB |
Output is correct |
10 |
Correct |
3167 ms |
1277624 KB |
Output is correct |
11 |
Correct |
3278 ms |
1277760 KB |
Output is correct |
12 |
Correct |
3245 ms |
1277628 KB |
Output is correct |
13 |
Correct |
3358 ms |
1276880 KB |
Output is correct |
14 |
Correct |
3254 ms |
675372 KB |
Output is correct |
15 |
Correct |
3216 ms |
675664 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1466 ms |
667344 KB |
Output is correct |
18 |
Correct |
2126 ms |
674924 KB |
Output is correct |
19 |
Correct |
3464 ms |
675516 KB |
Output is correct |
20 |
Correct |
3376 ms |
675396 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
3466 ms |
675444 KB |
Output is correct |
24 |
Correct |
1698 ms |
667348 KB |
Output is correct |
25 |
Correct |
2354 ms |
674812 KB |
Output is correct |
26 |
Correct |
8356 ms |
1393048 KB |
Output is correct |
27 |
Correct |
8170 ms |
1395048 KB |
Output is correct |
28 |
Correct |
696 ms |
176732 KB |
Output is correct |
29 |
Correct |
6861 ms |
1394432 KB |
Output is correct |
30 |
Correct |
6186 ms |
1394204 KB |
Output is correct |
31 |
Correct |
4523 ms |
1395116 KB |
Output is correct |
32 |
Correct |
5001 ms |
1393236 KB |
Output is correct |
33 |
Execution timed out |
10129 ms |
1892180 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |