Submission #748185

#TimeUsernameProblemLanguageResultExecution timeMemory
748185snpmrnhlolCake 3 (JOI19_cake3)C++17
100 / 100
1177 ms208720 KiB
#include <bits/stdc++.h> //#pragma GCC optimize("") using namespace std; typedef long long ll; const ll N = 2e5,logN = 32,maxa = 2e9; const ll inf64 = 1ll*1e9*1e9; const ll inf32 = 2e9; struct numbers{ ll v,c; }v[N]; struct nod{ ll cnt = 0,sum = 0,l = 0,r = 0; }; nod seg[logN*N]; ll root[N]; ll n,k; ll cnt2 = 1; void upd(ll old,ll cur,ll val,ll l = 1,ll r = maxa){ if(l == r){ seg[cur].sum = seg[old].sum + val; seg[cur].cnt = seg[old].cnt + 1; }else{ ll mij = (l + r)/2; if(val <= mij){ seg[cur].r = seg[old].r; seg[cur].l = cnt2++; upd(seg[old].l,seg[cur].l,val,l,mij); }else{ seg[cur].r = cnt2++; seg[cur].l = seg[old].l; upd(seg[old].r,seg[cur].r,val,mij + 1,r); } seg[cur].cnt = seg[seg[cur].l].cnt + seg[seg[cur].r].cnt; seg[cur].sum = seg[seg[cur].l].sum + seg[seg[cur].r].sum; } } ll get(ll old,ll cur,ll nr = k,ll l = 1,ll r = maxa){ //cout<<old<<' '<<cur<<' '<<nr<<' '<<l<<' '<<r<<'\n'; if(l == r){ //cout<<nr*l<<'\n'; return 1ll*nr*l; }else{ ll mij = (l + r)/2; if(seg[seg[cur].r].cnt - seg[seg[old].r].cnt >= nr){ return get(seg[old].r,seg[cur].r,nr,mij + 1,r); }else{ //cout<<seg[seg[cur].r].sum - seg[seg[old].r].sum<<'\n'; return seg[seg[cur].r].sum - seg[seg[old].r].sum + get(seg[old].l,seg[cur].l,nr - (seg[seg[cur].r].cnt - seg[seg[old].r].cnt),l,mij); } } } ll ans = -inf64; void solve(ll l,ll r,ll l2,ll r2){ if(l > r)return; ///haha shitty greedy wooo yeah ll score = -inf64,id = l2; ll mij = (l + r)/2,i; for(i = l2;i <= min(mij - k + 1,r2);i++){ ll nr = get((i==0?0:root[i-1]),root[mij]); if(nr - v[mij].c + v[i].c > score){ score = nr - v[mij].c + v[i].c; id = i; } } ans = max(ans,score); solve(l,mij - 1,l2,id); solve(mij + 1,r,id,r2); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll i,j; cin>>n>>k; for(i = 0;i < n;i++){ cin>>v[i].v>>v[i].c; v[i].c*=2; } sort(v,v + n,[&](numbers a,numbers b){ return a.c < b.c; }); for(i = 0;i < n;i++){ root[i] = cnt2++; upd(i == 0?0:root[i-1],root[i],v[i].v); } solve(0,n - 1,0,n - 1); cout<<ans; return 0; } /** 5 5 100 1 100 2 100 3 100 4 100 5 **/

Compilation message (stderr)

cake3.cpp: In function 'int main()':
cake3.cpp:72:10: warning: unused variable 'j' [-Wunused-variable]
   72 |     ll i,j;
      |          ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...