Submission #788592

#TimeUsernameProblemLanguageResultExecution timeMemory
788592Ahmed57Cake 3 (JOI19_cake3)C++17
0 / 100
6 ms12848 KiB
#include <bits/stdc++.h> using namespace std; #define int long long 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[800001]; void build(int p,int l,int r){ if(l==r){ seg[p].cnt = 0;seg[p].sum = 0; return ; } int md = (l+r)/2; build(p*2,l,md);build(p*2+1,md+1,r); seg[p].cnt = seg[p*2].cnt+seg[p*2+1].cnt; seg[p].sum = seg[p*2].sum+seg[p*2+1].sum; } 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[l-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)-2LL*(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); } signed main() { ios_base::sync_with_stdio(0);cin.tie(0); long long n; cin>>n>>k; for(int i = 0;i<n;i++){ long long 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; } build(1,1,sz); 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...