Submission #996292

#TimeUsernameProblemLanguageResultExecution timeMemory
996292hotboy2703Road Construction (JOI21_road_construction)C++14
100 / 100
4377 ms17232 KiB
#include<bits/stdc++.h> using ll = long long; using namespace std; #define pll pair <ll,ll> #define fi first #define se second #define MP make_pair #define sz(a) (ll((a).size())) #define BIT(mask,i) (((mask) >> (i))&1) #define MASK(i) (1LL << (i)) const ll MAXN = 2.5e5+100; ll n,k; pll a[MAXN]; ll BIT[MAXN]; void update(ll i,ll v){ // cout<<"UPD "<<i<<' '<<v<<endl; for (;i <= n;i += i &-i)BIT[i]+=v; } ll get(ll i){ // cout<<"GET "<<i<<endl; ll res = 0; for (;i > 0;i -= i &-i)res += BIT[i]; return res; } ll query(ll l,ll r){ return get(r) - get(l-1); } vector <ll> val; ll solve(ll x){ ll res = 0; ll ptr = 1; for (ll i = 1;i <= n;i ++){ // cout<<i<<endl; while (a[ptr].fi + x < a[i].fi){ update(lower_bound(val.begin(),val.end(),a[ptr].se) - val.begin() + 1,-1); ptr++; } res += query(lower_bound(val.begin(),val.end(),a[i].se - x) - val.begin()+1,upper_bound(val.begin(),val.end(),a[i].se + x)-val.begin()); update(lower_bound(val.begin(),val.end(),a[i].se) - val.begin() + 1,+1); } for (;ptr <= n;ptr ++){ update(lower_bound(val.begin(),val.end(),a[ptr].se) - val.begin() + 1,-1); } return res; } ll dist(ll x,ll y){ return max(abs(a[x].fi-a[y].fi),abs(a[x].se-a[y].se)); } void trace(ll x){ vector <ll> ans; multiset <pll> s; for (ll i = 1,ptr = 1;i <= n;i ++){ while (a[ptr].fi + x < a[i].fi){ s.erase(MP(a[ptr].se,ptr)); ptr++; } for (auto it = s.lower_bound(MP(a[i].se-x,0));it != s.end() && (*it).fi <= a[i].se + x;it++){ // cout<<(*it).se<<' '<<i<<endl; ans.push_back(dist((*it).se,i)); } s.insert(MP(a[i].se,i)); } while (sz(ans) < k)ans.push_back(x+1); sort(ans.begin(),ans.end()); for (auto x:ans)cout<<x<<'\n'; cout<<'\n'; } int main(){ ios_base::sync_with_stdio(0);cin.tie(nullptr); cin>>n>>k; for (ll i = 1;i <= n;i ++){ ll x,y; cin>>x>>y; a[i] = MP(x+y,x-y); val.push_back(a[i].se); } sort(val.begin(),val.end()); val.resize(unique(val.begin(),val.end())-val.begin()); sort(a+1,a+1+n); // for (ll i= 1 ;i <= n;i ++)cout<<a[i].fi<<' '<<a[i].se<<'\n'; // cout<<"SOLVE(2) "<<solve(2)<<endl; ll l = 1; ll r = 5e9; ll ans = 0; while (l <= r){ ll mid = (l + r) >> 1; // cout<<l<<' '<<r<<' '<<mid<<endl; if (solve(mid) < k){ ans = mid; l = mid + 1; } else{ r = mid - 1; } } // cout<<"ANS "<<ans<<endl; trace(ans); return 0; }
#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...