Submission #788599

#TimeUsernameProblemLanguageResultExecution timeMemory
788599Ahmed57Cake 3 (JOI19_cake3)C++17
100 / 100
1281 ms21632 KiB
#include <bits/stdc++.h> using namespace std; #define int long long vector<long long> pref; 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 inf = 1e18; signed main() { ios_base::sync_with_stdio(0);cin.tie(0); long long n; cin>>n>>k; vector<pair<long long,long long>> v; 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.end()); pref.erase(unique(pref.begin(),pref.end()),pref.end()); sz = pref.size();sort(v.begin(),v.end()); for(int i = 0;i<n;i++){ v[i].second = lower_bound(pref.begin(),pref.end(),v[i].second)-pref.begin()+1; } build(1,1,sz); auto 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); }; int ans = -inf; function<void(int,int,int,int)> dnc = [&](int l,int r,int optl,int optr){ if(l>r) return; int sum=0,mid=(l+r)>>1; pair<int,int> opt={-inf,-1}; for(int i=min(optr,mid-k+1);i>=optl;i--){ int res=cost(i,mid);ans=max(ans,res); opt=max(opt,{res,i}); } dnc(l,mid-1,optl,opt.second); dnc(mid+1,r,opt.second,optr); }; dnc(k-1,n-1,0,n-k); cout<<ans<<endl; return 0; }

Compilation message (stderr)

cake3.cpp: In lambda function:
cake3.cpp:71:13: warning: unused variable 'sum' [-Wunused-variable]
   71 |         int sum=0,mid=(l+r)>>1;
      |             ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...