Submission #788585

#TimeUsernameProblemLanguageResultExecution timeMemory
788585Ahmed57Cake 3 (JOI19_cake3)C++17
0 / 100
3 ms6588 KiB
#include <bits/stdc++.h> using namespace std; vector<long long> pref; vector<pair<long long,long long>> v; long long cl = 0 ,cr = -1 , sz; struct node{ long long cnt = 0 , sum =0; node():cnt(0),sum(0){}; }seg[400001]; void update(int p,int l,int r,int idx,long long val){ if(l==r){ seg[p].cnt+=val; seg[p].sum+=val*pref[idx-1]; return ; } int md = (l+r)/2; if(idx<=md)update(p*2,l,md,idx,val); else update(p*2+1,md+1,r,idx,val); seg[p].cnt = seg[p*2].cnt+seg[p*2+1].cnt; seg[p].sum = seg[p*2].sum+seg[p*2+1].sum; } long long query(int p,int l,int r,long long idx){ if(l==r){ return pref[l-1]*idx; } int md = (l+r)/2; if(seg[p*2+1].cnt<idx)return seg[p*2+1].sum+query(p*2,l,md,idx-seg[p*2+1].cnt); return query(p*2+1,md+1,r,idx); } int k; long long cost(int l,int r){ while(cl>l)update(1,1,sz,v[--cl].second,1); while(cr<r)update(1,1,sz,v[++cr].second,1); while(cl<l)update(1,1,sz,v[cl++].second,-1); while(cr>r)update(1,1,sz,v[cr--].second,-1); return query(1,1,sz,k)-2*(v[r].first-v[l].first); } long long ans = -1e18; void dc(int l,int r,int optl,int optr){ if(l>r)return ; int mid = (l+r)/2; pair<long long,int> best = {-1e18,-1}; for(int cut = min(optr,mid-k+1);cut>=optl;cut--){ long long x = cost(cut,mid);ans = max(x,ans); best = max(best,{x,cut}); } int opt = best.second; dc(l,mid-1,optl,opt); dc(mid+1,r,opt,optr); } int main() { ios_base::sync_with_stdio(0);cin.tie(0); int n; cin>>n>>k; for(int i = 0;i<n;i++){ int a,b; cin>>a>>b; pref.push_back(a); v.push_back({b,a}); } sort(pref.begin(),pref.begin()+n); pref.erase(unique(pref.begin(),pref.begin()+n),pref.begin()+n); sz = pref.size();sort(v.begin(),v.end()); for(int i = 0;i<n;i++){ v[i].second = lower_bound(pref.begin(),pref.begin()+n,v[i].second)-pref.begin()+1; } dc(k-1,n-1,0,n-k); cout<<ans<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...