This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long lll;
typedef int 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 ll K=350;
const ld pi=3.14159265359;
const lll INF=(1LL<<60);
#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()
ll pl=1,pr=0,n,m,x,y;
lll ans=-INF,current=0;
multiset<ll> msa,msb;
vector<pll> v;
void add(ll id){
msb.insert(v[id].X);
while(SZ(msa)<m&&SZ(msb)){
current+=*prev(msb.end());
msa.insert(*prev(msb.end()));
msb.erase(prev(msb.end()));
}
while(SZ(msa)&&SZ(msb)&&*msa.begin()<*prev(msb.end())){
ll tmpa=*msa.begin(),tmpb=*prev(msb.end());
msa.erase(msa.begin());msb.erase(prev(msb.end()));
current=current-tmpa+tmpb;
msa.insert(tmpb);msb.insert(tmpa);
}
}
void rem(ll id){
if(msa.find(v[id].X)!=msa.end())current-=v[id].X,msa.erase(msa.find(v[id].X));
else msb.erase(msb.find(v[id].X));
while(SZ(msa)<m&&SZ(msb)){
current+=*prev(msb.end());
msa.insert(*prev(msb.end()));
msb.erase(prev(msb.end()));
}
while(SZ(msa)&&SZ(msb)&&*msa.begin()<*prev(msb.end())){
ll tmpa=*msa.begin(),tmpb=*prev(msb.end());
msa.erase(msa.begin());msb.erase(prev(msb.end()));
current=current-tmpa+tmpb;
msa.insert(tmpb);msb.insert(tmpa);
}
}
void get_ans(ll l,ll r){
++l;--r;
while(pl>l){add(--pl);}
while(pr<r){add(++pr);}
while(pl<l){rem(pl++);}
while(pr>r){rem(pr--);}
}
void solve(ll ql,ll qr,ll al,ll ar){
if(ql>qr)return ;
ll mid=(ql+qr)/2;
msa.clear();msb.clear();current=0;
pl=mid;pr=mid-1;
lll bans=-INF,bpos=al;
for(int i=min(mid-1,ar);i>=al;i--){
get_ans(i,mid);
lll tans=(SZ(msa)<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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |