Submission #748147

#TimeUsernameProblemLanguageResultExecution timeMemory
748147snpmrnhlolCake 3 (JOI19_cake3)C++17
0 / 100
2 ms340 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+=val; seg[cur].cnt++; }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); for(i = 0;i < n;i++){ for(j = i + k - 1;j < n;j++){ ans = max(ans,get(i == 0?0:root[i - 1],root[j]) - v[j].c + v[i].c); //cout<<get(i == 0?0:root[i - 1],root[j])<<' '<<i<<' '<<j<<'\n'; } } cout<<ans; return 0; } /** 5 3 2 1 4 2 6 4 8 8 10 16 **/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...