#include <bits/stdc++.h>
using namespace std;
const int BSZ = 500;
struct DS {
int n;
vector<int> x, y;
vector<vector<int>> blocks;
DS(vector<pair<int, int>>& p) {
n = p.size();
x.resize(n), y.resize(n);
for(int i = 0; i < n; i++) x[i] = p[i].first, y[i] = p[i].second;
blocks.resize((n - 1) / BSZ + 1, vector<int>(n, n));
for(int i = 0; i < n; i++) {
blocks[i / BSZ][x[i]] = min(blocks[i / BSZ][x[i]], y[i]);
}
for(int i = 0; i < blocks.size(); i++) {
for(int j = 1; j < n; j++) {
blocks[i][j] = min(blocks[i][j], blocks[i][j - 1]);
}
}
}
int find_prev(int i, int lx, int ly) {
while(i--) {
if(i % BSZ == BSZ - 1) {
if(blocks[i / BSZ][lx] > ly) {
i -= BSZ - 1;
continue;
}
}
if(x[i] <= lx && y[i] <= ly) return i;
}
return i;
}
};
int main() {
int N, K; cin >> N >> K;
vector<pair<int, int>> p(N);
for(int i = 0; i < N; i++) cin >> p[i].first >> p[i].second;
vector<int> sx(N), sy(N);
for(int i = 0; i < N; i++) sx[i] = sy[i] = i;
sort(sx.begin(), sx.end(), [&] (int a, int b) { return p[a].first == p[b].first ? p[a].second < p[b].second : p[a].first < p[b].first; });
sort(sy.begin(), sy.end(), [&] (int a, int b) { return p[a].second == p[b].second ? p[a].first < p[b].first : p[a].second < p[b].second; });
vector<int> cx(N), cy(N);
for(int i = 0; i < N; i++) cx[sx[i]] = cy[sy[i]] = i;
vector<int> o1(N), o2(N);
for(int i = 0; i < N; i++) o1[i] = o2[i] = i;
sort(o1.begin(), o1.end(), [&] (int a, int b) { return p[a].first + p[a].second < p[b].first + p[b].second; });
sort(o2.begin(), o2.end(), [&] (int a, int b) { return p[a].first - p[a].second < p[b].first - p[b].second; });
vector<pair<int, int>> c1(N), c2(N);
for(int i = 0; i < N; i++) c1[i] = {cx[o1[i]], cy[o1[i]]}, c2[i] = {cx[o2[i]], N - 1 - cy[o2[i]]};
DS ds1(c1), ds2(c2);
priority_queue<pair<int, pair<int, int>>> pq1;
priority_queue<pair<int, pair<int, int>>> pq2;
auto diff1 = [&] (int i, int j) {
return (p[o1[i]].first + p[o1[i]].second) - (p[o1[j]].first + p[o1[j]].second);
};
auto diff2 = [&] (int i, int j) {
return (p[o2[i]].first - p[o2[i]].second) - (p[o2[j]].first - p[o2[j]].second);
};
for(int i = 0; i < N; i++) {
int n1 = ds1.find_prev(i, c1[i].first, c1[i].second);
int n2 = ds2.find_prev(i, c2[i].first, c2[i].second);
if(n1 != -1)
pq1.push({-diff1(i, n1), {i, n1}});
if(n2 != -1)
pq2.push({-diff2(i, n2), {i, n2}});
}
while(K--) {
if(pq2.size() == 0 || pq1.top().first > pq2.top().first) {
auto [v, ij] = pq1.top();
cout << -v << '\n';
pq1.pop();
int n1 = ds1.find_prev(ij.second, c1[ij.first].first, c1[ij.first].second);
if(n1 != -1)
pq1.push({-diff1(ij.first, n1), {ij.first, n1}});
} else {
auto [v, ij] = pq2.top();
cout << -v << '\n';
pq2.pop();
int n2 = ds2.find_prev(ij.second, c2[ij.first].first, c2[ij.first].second);
if(n2 != -1)
pq2.push({-diff2(ij.first, n2), {ij.first, n2}});
}
}
}
Compilation message
road_construction.cpp: In constructor 'DS::DS(std::vector<std::pair<int, int> >&)':
road_construction.cpp:16:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i = 0; i < blocks.size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
2900 KB |
Output is correct |
2 |
Correct |
48 ms |
3148 KB |
Output is correct |
3 |
Incorrect |
43 ms |
3156 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1635 ms |
1002376 KB |
Output is correct |
2 |
Correct |
1645 ms |
1002772 KB |
Output is correct |
3 |
Correct |
49 ms |
2960 KB |
Output is correct |
4 |
Correct |
1578 ms |
1002012 KB |
Output is correct |
5 |
Correct |
1585 ms |
1002132 KB |
Output is correct |
6 |
Correct |
1671 ms |
1002180 KB |
Output is correct |
7 |
Correct |
1588 ms |
1001652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1955 ms |
1003508 KB |
Output is correct |
2 |
Correct |
1951 ms |
1008288 KB |
Output is correct |
3 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1955 ms |
1003508 KB |
Output is correct |
2 |
Correct |
1951 ms |
1008288 KB |
Output is correct |
3 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
2900 KB |
Output is correct |
2 |
Correct |
48 ms |
3148 KB |
Output is correct |
3 |
Incorrect |
43 ms |
3156 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
2900 KB |
Output is correct |
2 |
Correct |
48 ms |
3148 KB |
Output is correct |
3 |
Incorrect |
43 ms |
3156 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |