#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpl;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 250003;
vector<ll> cmpSum, cmpDiff;
pl a[mn];
ll n;
struct BIT {
vector<int> tr;
BIT (int sz) : tr(sz + 1) {}
int p (int k) { return k & -k; }
void update (int k, int val) {
for (; k < tr.size(); k += p(k)) tr[k] += val;
}
int preSum (int k, int ans = 0) {
for (; k; k -= p(k)) ans += tr[k];
return ans;
}
void refresh() { fill(all(tr), 0); }
} treeSum(mn), treeDiff(mn);
int getSum (ll targ) {
return lower_bound(all(cmpSum), targ) - cmpSum.begin() + 1;
}
int getDiff (ll targ) {
return lower_bound(all(cmpDiff), targ) - cmpDiff.begin() + 1;
}
int pointSum (int i) {
ll x, y; tie(x, y) = a[i];
return lower_bound(all(cmpSum), x + y) - cmpSum.begin() + 1;
}
int pointDiff (int i) {
ll x, y; tie(x, y) = a[i];
return lower_bound(all(cmpDiff), x - y) - cmpDiff.begin() + 1;
}
ll countClose (ll ub) {
ll cnt = 0;
treeSum.refresh(), treeDiff.refresh();
for (int L = 0, R = 0; R < n; R++) {
treeSum.update(pointSum(R), 1);
treeDiff.update(pointDiff(R), 1);
while (a[R].first - a[L].first > ub) {
treeSum.update(pointSum(L), -1);
treeDiff.update(pointDiff(L), -1);
L++;
}
cnt += L;
ll x, y; tie(x, y) = a[R];
cnt += treeSum.preSum(getSum(x + y - ub) - 1);
cnt += treeDiff.preSum(getDiff(x - y - ub) - 1);
}
return n * (n - 1) / 2 - cnt;
}
ll distance (ll x, ll y, ll i, ll j) {
return abs(x - i) + abs(y - j);
}
void getPoints (ll ub, vector<ll> &ans) {
set<pl> s;
for (int L = 0, R = 0; R < n; R++) {
ll x, y; tie(x, y) = a[R];
s.emplace(y, x);
while (a[R].first - a[L].first > ub) {
ll x, y; tie(x, y) = a[L++];
s.erase(make_pair(y, x));
}
for (auto it = s.lower_bound(make_pair(y - ub, 0)); it != s.end() && it->first <= y + ub; it++) {
ll cur = distance(it->second, it->first, x, y);
if (0 < cur && cur <= ub) ans.push_back(cur);
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
ll k; cin >> n >> k;
for (int i = 0; i < n; i++) {
ll x, y; cin >> x >> y;
cmpSum.push_back(x + y), cmpDiff.push_back(x - y);
a[i] = {x, y};
}
sort(a, a + n), sort(all(cmpSum)), sort(all(cmpDiff));
filter(cmpSum), filter(cmpDiff);
ll ans = 0, save = 0;
for (ll mask = 1LL << 31; mask > 0; mask >>= 1) {
ll cur = countClose(ans | mask);
if (cur < k) ans |= mask, save = cur;
}
vector<ll> vec;
getPoints(ans, vec);
sort(all(vec));
for (ll u : vec) cout << u << "\n";
for (int i = 0; i < k - save; i++) cout << ans + 1 << "\n";
return 0;
}
Compilation message
road_construction.cpp: In member function 'void BIT::update(int, int)':
road_construction.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (; k < tr.size(); k += p(k)) tr[k] += val;
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
7248 KB |
Output is correct |
2 |
Correct |
45 ms |
7224 KB |
Output is correct |
3 |
Correct |
40 ms |
7112 KB |
Output is correct |
4 |
Correct |
39 ms |
7112 KB |
Output is correct |
5 |
Correct |
42 ms |
5976 KB |
Output is correct |
6 |
Correct |
9 ms |
2556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2191 ms |
17512 KB |
Output is correct |
2 |
Correct |
2312 ms |
17324 KB |
Output is correct |
3 |
Correct |
36 ms |
7112 KB |
Output is correct |
4 |
Correct |
2265 ms |
17532 KB |
Output is correct |
5 |
Correct |
2129 ms |
17424 KB |
Output is correct |
6 |
Correct |
2237 ms |
17520 KB |
Output is correct |
7 |
Correct |
2127 ms |
17644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5889 ms |
15724 KB |
Output is correct |
2 |
Correct |
5748 ms |
15724 KB |
Output is correct |
3 |
Correct |
3 ms |
2384 KB |
Output is correct |
4 |
Correct |
2118 ms |
13556 KB |
Output is correct |
5 |
Correct |
2326 ms |
16044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5889 ms |
15724 KB |
Output is correct |
2 |
Correct |
5748 ms |
15724 KB |
Output is correct |
3 |
Correct |
3 ms |
2384 KB |
Output is correct |
4 |
Correct |
2118 ms |
13556 KB |
Output is correct |
5 |
Correct |
2326 ms |
16044 KB |
Output is correct |
6 |
Correct |
5879 ms |
15728 KB |
Output is correct |
7 |
Correct |
6052 ms |
15724 KB |
Output is correct |
8 |
Correct |
4 ms |
2384 KB |
Output is correct |
9 |
Correct |
4 ms |
2384 KB |
Output is correct |
10 |
Correct |
5818 ms |
15728 KB |
Output is correct |
11 |
Correct |
2244 ms |
13568 KB |
Output is correct |
12 |
Correct |
2251 ms |
15980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
7248 KB |
Output is correct |
2 |
Correct |
45 ms |
7224 KB |
Output is correct |
3 |
Correct |
40 ms |
7112 KB |
Output is correct |
4 |
Correct |
39 ms |
7112 KB |
Output is correct |
5 |
Correct |
42 ms |
5976 KB |
Output is correct |
6 |
Correct |
9 ms |
2556 KB |
Output is correct |
7 |
Correct |
2484 ms |
12440 KB |
Output is correct |
8 |
Correct |
2541 ms |
12452 KB |
Output is correct |
9 |
Correct |
38 ms |
7112 KB |
Output is correct |
10 |
Correct |
2203 ms |
11688 KB |
Output is correct |
11 |
Correct |
1958 ms |
11600 KB |
Output is correct |
12 |
Correct |
737 ms |
10240 KB |
Output is correct |
13 |
Correct |
854 ms |
11532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
7248 KB |
Output is correct |
2 |
Correct |
45 ms |
7224 KB |
Output is correct |
3 |
Correct |
40 ms |
7112 KB |
Output is correct |
4 |
Correct |
39 ms |
7112 KB |
Output is correct |
5 |
Correct |
42 ms |
5976 KB |
Output is correct |
6 |
Correct |
9 ms |
2556 KB |
Output is correct |
7 |
Correct |
2191 ms |
17512 KB |
Output is correct |
8 |
Correct |
2312 ms |
17324 KB |
Output is correct |
9 |
Correct |
36 ms |
7112 KB |
Output is correct |
10 |
Correct |
2265 ms |
17532 KB |
Output is correct |
11 |
Correct |
2129 ms |
17424 KB |
Output is correct |
12 |
Correct |
2237 ms |
17520 KB |
Output is correct |
13 |
Correct |
2127 ms |
17644 KB |
Output is correct |
14 |
Correct |
5889 ms |
15724 KB |
Output is correct |
15 |
Correct |
5748 ms |
15724 KB |
Output is correct |
16 |
Correct |
3 ms |
2384 KB |
Output is correct |
17 |
Correct |
2118 ms |
13556 KB |
Output is correct |
18 |
Correct |
2326 ms |
16044 KB |
Output is correct |
19 |
Correct |
5879 ms |
15728 KB |
Output is correct |
20 |
Correct |
6052 ms |
15724 KB |
Output is correct |
21 |
Correct |
4 ms |
2384 KB |
Output is correct |
22 |
Correct |
4 ms |
2384 KB |
Output is correct |
23 |
Correct |
5818 ms |
15728 KB |
Output is correct |
24 |
Correct |
2244 ms |
13568 KB |
Output is correct |
25 |
Correct |
2251 ms |
15980 KB |
Output is correct |
26 |
Correct |
2484 ms |
12440 KB |
Output is correct |
27 |
Correct |
2541 ms |
12452 KB |
Output is correct |
28 |
Correct |
38 ms |
7112 KB |
Output is correct |
29 |
Correct |
2203 ms |
11688 KB |
Output is correct |
30 |
Correct |
1958 ms |
11600 KB |
Output is correct |
31 |
Correct |
737 ms |
10240 KB |
Output is correct |
32 |
Correct |
854 ms |
11532 KB |
Output is correct |
33 |
Correct |
7130 ms |
19700 KB |
Output is correct |
34 |
Correct |
7052 ms |
19696 KB |
Output is correct |
35 |
Correct |
5878 ms |
19568 KB |
Output is correct |
36 |
Correct |
2064 ms |
17580 KB |
Output is correct |
37 |
Correct |
2073 ms |
17576 KB |
Output is correct |
38 |
Correct |
2243 ms |
19816 KB |
Output is correct |