Submission #710025

#TimeUsernameProblemLanguageResultExecution timeMemory
710025Darren0724Cake 3 (JOI19_cake3)C++17
0 / 100
1 ms724 KiB
#include<bits/stdc++.h> using namespace std; #define all(x) x.begin(),x.end() #define int long long #define x first #define y second const int INF=1e18; const int N=1000000005; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); int get(int a,int b){ return uniform_int_distribution<int>(a,b)(rnd); } struct seg{ int l,m,r; seg *lc,*rc; int cnt=0,total=0; seg(int l1,int r1){ l=l1,r=r1; m=(l+r)>>1; lc=rc=NULL; } void pull(){ cnt=0; total=0; if(lc){ cnt+=lc->cnt; total+=lc->total; } if(rc){ cnt+=rc->cnt; total+=rc->total; } } void add(int p){ if(r-l==1){ cnt++; total+=p; return; } m=(l+r)>>1; if(p<m){ lc=(lc?new seg(*lc):new seg(l,m)); lc->add(p); } else{ rc=(rc?new seg(*rc):new seg(m,r)); rc->add(p); } pull(); } }; int ask(seg* a,seg* b,int need){ if(a->r-a->l==1){ return (need>b->cnt-a->cnt?-INF:need*a->l); } if(!a->lc){ a->lc=new seg(a->l,a->m); } if(!a->rc){ a->rc=new seg(a->m,a->r); } if(!b->lc){ b->lc=new seg(b->l,b->m); } if(!b->rc){ b->rc=new seg(b->m,b->r); } if(b->rc->cnt-a->rc->cnt>=need){ return ask(a->rc,b->rc,need); } else{ return b->rc->total-a->rc->total+ask(a->lc,b->lc,need-b->rc->cnt+a->rc->cnt); } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n,m; cin>>n>>m; vector<pair<int,int>> v(n+1); for(int i=1;i<=n;i++){ cin>>v[i].y>>v[i].x; } sort(v.begin(),v.end()); int ans1=-INF; vector<seg*> tr(n+1); tr[0]=new seg(0,N); for(int i=1;i<=n;i++){ tr[i]=new seg(*tr[i-1]); tr[i]->add(v[i].y); } int idx=0; for(int i=1;i<=n;i++){ while(1){ int tmp=ask(tr[idx],tr[i],m)-(v[i].x-v[idx+1].x)*2; int tmp1=ask(tr[idx+1],tr[i],m)-(v[i].x-v[idx+2].x)*2; if(tmp1>tmp){ idx++; } else{ break; } } int ans=ask(tr[idx],tr[i],m)-(v[i].x-v[idx+1].x)*2; ans1=max(ans1,ans); } cout<<ans1<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...