#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)
const int MN = 750005;
int n, k, x, y, bit[MN];
pii a[MN];
void upd(int x, int val) {
for (int i = x; i < MN; i += i & -i) bit[i] += val;
}
int qry(int x) {
int ret = 0;
for (int i = x; i > 0; i -= i & -i) ret += bit[i];
return ret;
}
int cost(pii p, pii q) {
return max(abs(p.f - q.f), abs(p.s - q.s));
}
int count(int d) {
int ret = 0;
memset(bit, 0, sizeof(bit));
vector<int> ys;
for (int i = 1; i <= n; i++) {
ys.push_back(a[i].s);
ys.push_back(a[i].s - d);
ys.push_back(a[i].s + d);
}
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
map<int, int> mp;
for (int i = 0; i < ys.size(); i++) mp[ys[i]] = i + 1;
int id = 1;
for (int i = 1; i <= n; i++) {
while (a[i].f - a[id].f > d) upd(mp[a[id].s], -1), id++;
ret += qry(mp[a[i].s + d]) - qry(mp[a[i].s - d] - 1);
//printf("lol %lld %lld\n", mp[a[i].s + d], mp[a[i].s - d]);
upd(mp[a[i].s], 1);
}
return ret;
}
int32_t main() {
boost();
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> x >> y;
a[i] = {x + y, x - y};
//printf("%lld %lld\n", a[i].f, a[i].s);
}
sort(a + 1, a + n + 1);
int lo = 1, hi = 1e10, mid;
while (lo < hi) {
mid = (lo + hi) >> 1;
if (count(mid) >= k) hi = mid;
else lo = mid + 1;
}
//printf("cnt %lld\n", count(2));
printf("%lld\n", lo);
return 0;
}
Compilation message
road_construction.cpp: In function 'long long int count(long long int)':
road_construction.cpp:43:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i < ys.size(); i++) mp[ys[i]] = i + 1;
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
6404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
10023 ms |
66772 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
10058 ms |
65256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
10058 ms |
65256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
6404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
6404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |