Submission #943321

#TimeUsernameProblemLanguageResultExecution timeMemory
943321shoryu386Road Construction (JOI21_road_construction)C++17
65 / 100
10102 ms1082412 KiB
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define int long long template <typename T> using pbds_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; #define N 250007 int fw[N]; void update(int x, int v){ x++; for (; x < N; x+= x&(-x)) fw[x] += v; } int sum(int x) { x++; int res = 0; for(; x; x-=x&(-x)) res += fw[x]; return res; } inline int range_sum(int x, int y) { //inclusive return sum(y)-sum(x-1); } 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 set<int> pts; for (int x = 0; x < n; x++){ pts.insert(arr[x].second); } unordered_map<int, int> disc; vector<int> discPts; int ptr = 0; for (auto pt : pts){ disc[pt] = ptr; ptr++; discPts.push_back(pt); } int l = 0, r = 4'000'000'000LL, ans = 4'000'000'000LL; while (l <= r){ int m = (l+r)/2; int cnt = 0; memset(fw, 0, sizeof(fw)); 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){ update(disc[arr[ pq.top().second ].second], -1); pq.pop(); } //we want sum <= arr[x].second-m-1 //we want sum <= arr[x].second+m int high = 0; auto upper = lower_bound(discPts.begin(), discPts.end(), arr[x].second+m+1); if (upper != discPts.begin()){ high = disc[ *prev(upper) ]; } int low = 0; auto lower = lower_bound(discPts.begin(), discPts.end(), arr[x].second-m); if (lower != discPts.begin()){ low = disc[ *prev(lower) ]; } cnt += sum(high) - sum(low); update(disc[arr[x].second], 1); pq.push({arr[x].first+m+1, x}); } if (cnt < k){ l = m+1; } else{ ans = m; r = m-1; } } vector<int> clown; int ptrr = 0; for (int x = 0; x < n; x++){ while (ptrr != n && arr[ptrr].first - arr[x].first <= ans){ ptrr++; } for (int y = x+1; y < ptrr; y++){ clown.push_back( max( arr[y].first - arr[x].first, abs(arr[x].second - arr[y].second) ) ); } } sort(clown.begin(), clown.end()); for (int x = 0; x < k; x++) cout << clown[x] << '\n'; }

Compilation message (stderr)

road_construction.cpp:30:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   30 | 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...