#include <bits/stdc++.h>
using namespace std;
const long long BSZ = 500;
struct DS {
long long n;
vector<long long> x, y;
vector<vector<long long>> blocks;
DS(vector<pair<long long, long long>>& p) {
n = p.size();
x.resize(n), y.resize(n);
for(long long i = 0; i < n; i++) x[i] = p[i].first, y[i] = p[i].second;
blocks.resize((n - 1) / BSZ + 1, vector<long long>(n, n));
for(long long i = 0; i < n; i++) {
blocks[i / BSZ][x[i]] = min(blocks[i / BSZ][x[i]], y[i]);
}
for(long long i = 0; i < blocks.size(); i++) {
for(long long j = 1; j < n; j++) {
blocks[i][j] = min(blocks[i][j], blocks[i][j - 1]);
}
}
}
long long find_prev(long long i, long long lx, long long 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() {
long long N, K; cin >> N >> K;
vector<pair<long long, long long>> p(N);
for(long long i = 0; i < N; i++) cin >> p[i].first >> p[i].second;
vector<long long> sx(N), sy(N);
for(long long i = 0; i < N; i++) sx[i] = sy[i] = i;
sort(sx.begin(), sx.end(), [&] (long long a, long long 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(), [&] (long long a, long long b) { return p[a].second == p[b].second ? p[a].first < p[b].first : p[a].second < p[b].second; });
vector<long long> cx(N), cy(N);
for(long long i = 0; i < N; i++) cx[sx[i]] = cy[sy[i]] = i;
vector<long long> o1(N), o2(N);
for(long long i = 0; i < N; i++) o1[i] = o2[i] = i;
sort(o1.begin(), o1.end(), [&] (long long a, long long b) { return p[a].first + p[a].second < p[b].first + p[b].second; });
sort(o2.begin(), o2.end(), [&] (long long a, long long b) { return p[a].first - p[a].second < p[b].first - p[b].second; });
vector<pair<long long, long long>> c1(N), c2(N);
for(long long 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<long long, pair<long long, long long>>> pq1;
priority_queue<pair<long long, pair<long long, long long>>> pq2;
auto diff1 = [&] (long long i, long long j) {
return (p[o1[i]].first + p[o1[i]].second) - (p[o1[j]].first + p[o1[j]].second);
};
auto diff2 = [&] (long long i, long long j) {
return (p[o2[i]].first - p[o2[i]].second) - (p[o2[j]].first - p[o2[j]].second);
};
for(long long i = 0; i < N; i++) {
long long n1 = ds1.find_prev(i, c1[i].first, c1[i].second);
long long 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(pq1.size() != 0 && (pq2.size() == 0 || pq1.top().first > pq2.top().first)) {
auto [v, ij] = pq1.top();
cout << -v << '\n';
pq1.pop();
long long 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();
long long 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<long long int, long long int> >&)':
road_construction.cpp:16:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(long long i = 0; i < blocks.size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
3156 KB |
Output is correct |
2 |
Correct |
46 ms |
3268 KB |
Output is correct |
3 |
Correct |
42 ms |
3152 KB |
Output is correct |
4 |
Correct |
41 ms |
3156 KB |
Output is correct |
5 |
Correct |
45 ms |
1996 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2116 ms |
2000616 KB |
Output is correct |
2 |
Correct |
2065 ms |
2004092 KB |
Output is correct |
3 |
Correct |
38 ms |
2896 KB |
Output is correct |
4 |
Correct |
1995 ms |
2004780 KB |
Output is correct |
5 |
Correct |
1981 ms |
2003772 KB |
Output is correct |
6 |
Correct |
2012 ms |
2004800 KB |
Output is correct |
7 |
Correct |
2024 ms |
2004460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2388 ms |
2011152 KB |
Output is correct |
2 |
Correct |
2400 ms |
2011024 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
1983 ms |
2004192 KB |
Output is correct |
5 |
Correct |
2420 ms |
2011396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2388 ms |
2011152 KB |
Output is correct |
2 |
Correct |
2400 ms |
2011024 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
1983 ms |
2004192 KB |
Output is correct |
5 |
Correct |
2420 ms |
2011396 KB |
Output is correct |
6 |
Correct |
2344 ms |
2011464 KB |
Output is correct |
7 |
Correct |
2388 ms |
2011364 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2335 ms |
2011176 KB |
Output is correct |
11 |
Correct |
1969 ms |
2003140 KB |
Output is correct |
12 |
Correct |
2371 ms |
2011372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
3156 KB |
Output is correct |
2 |
Correct |
46 ms |
3268 KB |
Output is correct |
3 |
Correct |
42 ms |
3152 KB |
Output is correct |
4 |
Correct |
41 ms |
3156 KB |
Output is correct |
5 |
Correct |
45 ms |
1996 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
795 ms |
336312 KB |
Output is correct |
8 |
Correct |
771 ms |
336992 KB |
Output is correct |
9 |
Correct |
42 ms |
3156 KB |
Output is correct |
10 |
Correct |
577 ms |
335548 KB |
Output is correct |
11 |
Correct |
583 ms |
335640 KB |
Output is correct |
12 |
Correct |
689 ms |
336832 KB |
Output is correct |
13 |
Correct |
662 ms |
335380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
3156 KB |
Output is correct |
2 |
Correct |
46 ms |
3268 KB |
Output is correct |
3 |
Correct |
42 ms |
3152 KB |
Output is correct |
4 |
Correct |
41 ms |
3156 KB |
Output is correct |
5 |
Correct |
45 ms |
1996 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2116 ms |
2000616 KB |
Output is correct |
8 |
Correct |
2065 ms |
2004092 KB |
Output is correct |
9 |
Correct |
38 ms |
2896 KB |
Output is correct |
10 |
Correct |
1995 ms |
2004780 KB |
Output is correct |
11 |
Correct |
1981 ms |
2003772 KB |
Output is correct |
12 |
Correct |
2012 ms |
2004800 KB |
Output is correct |
13 |
Correct |
2024 ms |
2004460 KB |
Output is correct |
14 |
Correct |
2388 ms |
2011152 KB |
Output is correct |
15 |
Correct |
2400 ms |
2011024 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Correct |
1983 ms |
2004192 KB |
Output is correct |
18 |
Correct |
2420 ms |
2011396 KB |
Output is correct |
19 |
Correct |
2344 ms |
2011464 KB |
Output is correct |
20 |
Correct |
2388 ms |
2011364 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
2335 ms |
2011176 KB |
Output is correct |
24 |
Correct |
1969 ms |
2003140 KB |
Output is correct |
25 |
Correct |
2371 ms |
2011372 KB |
Output is correct |
26 |
Correct |
795 ms |
336312 KB |
Output is correct |
27 |
Correct |
771 ms |
336992 KB |
Output is correct |
28 |
Correct |
42 ms |
3156 KB |
Output is correct |
29 |
Correct |
577 ms |
335548 KB |
Output is correct |
30 |
Correct |
583 ms |
335640 KB |
Output is correct |
31 |
Correct |
689 ms |
336832 KB |
Output is correct |
32 |
Correct |
662 ms |
335380 KB |
Output is correct |
33 |
Correct |
2824 ms |
2013212 KB |
Output is correct |
34 |
Correct |
2852 ms |
2013212 KB |
Output is correct |
35 |
Correct |
2424 ms |
2012312 KB |
Output is correct |
36 |
Correct |
2858 ms |
2013296 KB |
Output is correct |
37 |
Correct |
2839 ms |
2013728 KB |
Output is correct |
38 |
Correct |
2631 ms |
2011928 KB |
Output is correct |