#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; _ < 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) {
x[i] *= -1;
}
go(1, 0);
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
621 ms |
182392 KB |
Output is correct |
2 |
Correct |
633 ms |
182496 KB |
Output is correct |
3 |
Correct |
295 ms |
89908 KB |
Output is correct |
4 |
Correct |
314 ms |
95756 KB |
Output is correct |
5 |
Correct |
513 ms |
165568 KB |
Output is correct |
6 |
Correct |
6 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2126 ms |
648272 KB |
Output is correct |
2 |
Correct |
2232 ms |
648352 KB |
Output is correct |
3 |
Correct |
525 ms |
176152 KB |
Output is correct |
4 |
Correct |
2069 ms |
648060 KB |
Output is correct |
5 |
Correct |
1905 ms |
648316 KB |
Output is correct |
6 |
Correct |
2089 ms |
648312 KB |
Output is correct |
7 |
Correct |
2008 ms |
647604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1736 ms |
346656 KB |
Output is correct |
2 |
Correct |
1787 ms |
346584 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
982 ms |
346632 KB |
Output is correct |
5 |
Correct |
1315 ms |
346304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1736 ms |
346656 KB |
Output is correct |
2 |
Correct |
1787 ms |
346584 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
982 ms |
346632 KB |
Output is correct |
5 |
Correct |
1315 ms |
346304 KB |
Output is correct |
6 |
Correct |
1750 ms |
346624 KB |
Output is correct |
7 |
Correct |
1625 ms |
346572 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1643 ms |
346648 KB |
Output is correct |
11 |
Correct |
871 ms |
346556 KB |
Output is correct |
12 |
Correct |
1119 ms |
346472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
621 ms |
182392 KB |
Output is correct |
2 |
Correct |
633 ms |
182496 KB |
Output is correct |
3 |
Correct |
295 ms |
89908 KB |
Output is correct |
4 |
Correct |
314 ms |
95756 KB |
Output is correct |
5 |
Correct |
513 ms |
165568 KB |
Output is correct |
6 |
Correct |
6 ms |
2644 KB |
Output is correct |
7 |
Correct |
2089 ms |
415684 KB |
Output is correct |
8 |
Correct |
2029 ms |
415540 KB |
Output is correct |
9 |
Correct |
270 ms |
95664 KB |
Output is correct |
10 |
Correct |
1723 ms |
414812 KB |
Output is correct |
11 |
Correct |
1716 ms |
414716 KB |
Output is correct |
12 |
Correct |
1303 ms |
415496 KB |
Output is correct |
13 |
Correct |
1430 ms |
413776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
621 ms |
182392 KB |
Output is correct |
2 |
Correct |
633 ms |
182496 KB |
Output is correct |
3 |
Correct |
295 ms |
89908 KB |
Output is correct |
4 |
Correct |
314 ms |
95756 KB |
Output is correct |
5 |
Correct |
513 ms |
165568 KB |
Output is correct |
6 |
Correct |
6 ms |
2644 KB |
Output is correct |
7 |
Correct |
2126 ms |
648272 KB |
Output is correct |
8 |
Correct |
2232 ms |
648352 KB |
Output is correct |
9 |
Correct |
525 ms |
176152 KB |
Output is correct |
10 |
Correct |
2069 ms |
648060 KB |
Output is correct |
11 |
Correct |
1905 ms |
648316 KB |
Output is correct |
12 |
Correct |
2089 ms |
648312 KB |
Output is correct |
13 |
Correct |
2008 ms |
647604 KB |
Output is correct |
14 |
Correct |
1736 ms |
346656 KB |
Output is correct |
15 |
Correct |
1787 ms |
346584 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
982 ms |
346632 KB |
Output is correct |
18 |
Correct |
1315 ms |
346304 KB |
Output is correct |
19 |
Correct |
1750 ms |
346624 KB |
Output is correct |
20 |
Correct |
1625 ms |
346572 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1643 ms |
346648 KB |
Output is correct |
24 |
Correct |
871 ms |
346556 KB |
Output is correct |
25 |
Correct |
1119 ms |
346472 KB |
Output is correct |
26 |
Correct |
2089 ms |
415684 KB |
Output is correct |
27 |
Correct |
2029 ms |
415540 KB |
Output is correct |
28 |
Correct |
270 ms |
95664 KB |
Output is correct |
29 |
Correct |
1723 ms |
414812 KB |
Output is correct |
30 |
Correct |
1716 ms |
414716 KB |
Output is correct |
31 |
Correct |
1303 ms |
415496 KB |
Output is correct |
32 |
Correct |
1430 ms |
413776 KB |
Output is correct |
33 |
Correct |
3309 ms |
647476 KB |
Output is correct |
34 |
Correct |
3234 ms |
647564 KB |
Output is correct |
35 |
Correct |
2865 ms |
647004 KB |
Output is correct |
36 |
Correct |
2127 ms |
647312 KB |
Output is correct |
37 |
Correct |
2137 ms |
647588 KB |
Output is correct |
38 |
Correct |
2178 ms |
645976 KB |
Output is correct |