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 int ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=2e5+5;
const ld pi=3.14159265359;
const long long 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;
long long sum;
node *lc,*rc;
}*RT,*rt[N];
ll pl=1,pr=0,n,m,x,y;
long long ans=-INF,val[N];
vector<pll> v;
vector<ll> lis;
map<ll,ll> rev;
ll getcnt(node *nd){
if(!nd)return 0;
return nd->cnt;
}
long long getsum(node *nd){
if(!nd)return 0;
return nd->sum;
}
node *insert(node *nd,ll to){
if(nd->l==nd->r-1){return new node{nd->l,nd->r,nd->cnt+1,val[nd->l]*(nd->cnt+1),0,0};}
ll mid=(nd->l+nd->r)/2;
if(to<mid){
if(!nd->lc)nd->lc=new node{nd->l,mid,0,0,0,0};
node *tmp=insert(nd->lc,to);
return new node{nd->l,nd->r,getcnt(nd->rc)+getcnt(tmp),getsum(nd->rc)+getsum(tmp),tmp,nd->rc};
}else{
if(!nd->rc)nd->rc=new node{mid,nd->r,0,0,0,0};
node *tmp=insert(nd->rc,to);
return new node{nd->l,nd->r,getcnt(nd->lc)+getcnt(tmp),getsum(nd->lc)+getsum(tmp),nd->lc,tmp};
}
}
long long query(node *nda,node *ndb,ll le){
if(nda->l==nda->r-1){return le*val[nda->l];}
ll mid=(nda->l+nda->r)/2;
if(le>=getcnt(nda->rc)-(ndb?getcnt(ndb->rc):0)){
if(!nda->lc)nda->lc=new node{nda->l,mid,0,0,0,0};
return getsum(nda->rc)-(ndb?getsum(ndb->rc):0)+query(nda->lc,(ndb?ndb->lc:0),le-(getcnt(nda->rc)-(ndb?getcnt(ndb->rc):0)));
}else{
if(!nda->rc)nda->rc=new node{mid,nda->r,0,0,0,0};
return query(nda->rc,(ndb?ndb->rc:0),le);
}
}
long long getans(ll ql,ll qr){
--qr;//(ql,qr]
if(getcnt(rt[qr])-getcnt(rt[ql])<m)return -INF;
return query(rt[qr],rt[ql],m)-2*(v[qr+1].Y-v[ql].Y)+v[qr+1].X+v[ql].X;
}
void solve(ll ql,ll qr,ll al,ll ar){
if(ql>qr)return ;
ll mid=(ql+qr)/2;
long long bans=-INF,bpos=al;
for(int i=min(mid-1,ar);i>=al;i--){
long long tans = getans(i,mid);
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)),lis.pb(x);
sort(v.begin(),v.end(),[](pll a,pll b){
return (a.Y!=b.Y?a.Y<b.Y:a.X>b.X);
});
sort(lis.begin(),lis.end());
lis.resize(unique(lis.begin(),lis.end())-lis.begin());
REP(i,SZ(lis)){
val[i]=lis[i];rev[lis[i]]=i;
}
RT=new node{0,SZ(lis),0,0,0,0};
REP(i,n){
rt[i]=(i==0?insert(RT,rev[v[i].X]):insert(rt[i-1],rev[v[i].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... |