Submission #943289

#TimeUsernameProblemLanguageResultExecution timeMemory
943289shoryu386Road Construction (JOI21_road_construction)C++17
5 / 100
10100 ms1203600 KiB
#include <bits/stdc++.h> using namespace std; #define int long long struct node{ int s, e, m; int v; node *l = nullptr, *r; node(int _s, int _e){ s = _s, e = _e, m = s + (e-s)/2; v = 0; } void del(){ if (l != nullptr) l->del(), r->del(); delete this; } void make_child(){ if (s != e && l == nullptr) l = new node(s, m), r = new node(m+1, e); } int query(int qs, int qe){ if (qs == s && e == qe) return v; make_child(); if (qe <= m) return l->query(qs, qe); else if (qs > m) return r->query(qs, qe); else return l->query(qs, m) + r->query(m+1, qe); } void update(int x, int k){ if (s == e) {v += k; return;} make_child(); if (x <= m) l->update(x, k); else r->update(x, k); v = l->v + r->v; } }; main(){ int n, k; cin >> n >> k; pair<int, int> arr[n]; for (int x = 0; x < n; x++){ cin >> arr[x].first >> arr[x].second; } //consider manhattan distance trick? for (int x = 0; x < n; x++){ arr[x] = {arr[x].first + arr[x].second, arr[x].first - arr[x].second}; } sort(arr, arr+n); //sorted by x-coord //now chebyshev distance, dist = max of difference //I want to bsearch, but bsearch only gives location of boundary; I need sum of boundary int l = 0, r = 4'000'000'000LL, ans = 4'000'000'000LL; while (l <= r){ int m = (l+r)/2; int cnt = 0; node *seg = new node(-8'000'000'007LL, 8'000'000'007LL); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; for (int x = 0; x < n; x++){ while (!pq.empty() && pq.top().first <= arr[x].first){ seg->update(arr[ pq.top().second ].second, -1); pq.pop(); } cnt += seg->query(arr[x].second - m, arr[x].second + m); seg->update(arr[x].second, 1); pq.push({arr[x].first+m+1, x}); } if (cnt < k){ l = m+1; } else{ ans = m; r = m-1; } seg->del(); } vector<int> clown; for (int x = 0; x < n; x++){ for (int y = x+1; y <= min(x+k+2, n-1); y++){ clown.push_back( max( abs(arr[x].first - arr[y].first), abs(arr[x].second - arr[y].second) ) ); } } sort(clown.begin(), clown.end()); for (int x = 0; x < k; x++) cout << clown[x] << '\n'; assert(clown[k-1] == ans); }

Compilation message (stderr)

road_construction.cpp:48:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   48 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...