제출 #312305

#제출 시각아이디문제언어결과실행 시간메모리
312305YJUCake 3 (JOI19_cake3)C++14
24 / 100
4104 ms159584 KiB
#include<bits/stdc++.h> #pragma GCC optimize("unroll-loops,no-stack-protector") using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll,ll> pll; const ll MOD=1e9+7; const ll MOD2=998244353; const ll N=4e5+5; const ld pi=3.14159265359; const ll INF=(1LL<<32); #define SQ(i) ((i)*(i)) #define REP(i,n) for(ll i=0;i<n;i++) #define REP1(i,n) for(ll i=1;i<=n;i++) #define pb push_back #define mp make_pair #define X first #define Y second #define setp setprecision #define lwb lower_bound #define SZ(_a) (ll)_a.size() struct node{ ll l,r,cnt,sum; node *lc,*rc; }*rt=new node{0,INF,0,0,0,0}; ll pl=1,pr=0,n,m,x,y,ans=-INF; vector<pll> v; ll get_cnt(node *nd){ if(!nd)return 0; return nd->cnt; } ll get_sum(node *nd){ if(!nd)return 0; return nd->sum; } void insert(node *nd,ll to,ll d){ if(nd->l==nd->r-1){nd->cnt+=d;nd->sum=nd->l*nd->cnt;return ;} ll mid=(nd->l+nd->r)/2; if(to<mid){ if(!nd->lc)nd->lc=new node{nd->l,mid,0,0,0,0}; insert(nd->lc,to,d); }else{ if(!nd->rc)nd->rc=new node{mid,nd->r,0,0,0,0}; insert(nd->rc,to,d); } nd->cnt=get_cnt(nd->lc)+get_cnt(nd->rc); nd->sum=get_sum(nd->lc)+get_sum(nd->rc); } ll query(node *nd,ll le){ if(nd->l==nd->r-1){return le*nd->l;} ll mid=(nd->l+nd->r)/2; if(le>=get_cnt(nd->rc)){ if(!nd->lc)nd->lc=new node{nd->l,mid,0,0,0,0}; return get_sum(nd->rc)+query(nd->lc,le-get_cnt(nd->rc)); }else{ if(!nd->rc)nd->rc=new node{mid,nd->r,0,0,0,0}; return query(nd->rc,le); } } ll get_ans(ll l,ll r){ ++l;--r; while(pl>l){insert(rt,v[--pl].X,1);} while(pr<r){insert(rt,v[++pr].X,1);} while(pl<l){insert(rt,v[pl++].X,-1);} while(pr>r){insert(rt,v[pr--].X,-1);} if(get_cnt(rt)<m)return -INF; return query(rt,m); } void solve(ll ql,ll qr,ll al,ll ar){ if(ql>qr)return ; ll mid=(ql+qr)/2; ll bans=-INF,bpos=al; for(int i=min(mid-1,ar);i>=al;i--){ long long current = get_ans(i,mid); ll tans=(get_cnt(rt)<m?-INF:current-2*(v[mid].Y-v[i].Y)+v[mid].X+v[i].X); if(tans>bans){ bpos=i,bans=tans; } ans=max(ans,tans); } solve(ql,mid-1,al,bpos); solve(mid+1,qr,bpos,ar); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); cin>>n>>m; m-=2; REP(i,n)cin>>x>>y,v.pb(mp(x,y)); sort(v.begin(),v.end(),[](pll a,pll b){ return (a.Y!=b.Y?a.Y<b.Y:a.X>b.X); }); solve(0,n-1,0,n-1); cout<<ans<<"\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...