This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
//#include "rail.h"
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=2e5+50;
const int mod=1e9+7;
using namespace std;
int nds,i,n,m,rt[70*nmax],lch[70*nmax],rch[70*nmax];
pair<ll,int>st[70*nmax];
pair<pair<ll,ll>,int>a[nmax];
ll ans=-llinf;
int nlf(pair<ll,int>x)
{
int p=++nds;
lch[p]=rch[p]=0;
st[p]=x;
return p;
}
int npr(int l,int r)
{
int p=++nds;
lch[p]=l,rch[p]=r;
st[p].fr=st[l].fr+st[r].fr;
st[p].sc=st[l].sc+st[r].sc;
return p;
}
int build(int l,int r)
{
if(l==r)return nlf(mkp(0,0));
int mid=(l+r)/2;
return npr(build(l,mid),build(mid+1,r));
}
int upd(int nod,int l,int r,int p,ll v)
{
if(l==r)return nlf(mkp(v,1));
int mid=(l+r)/2;
if(p<=mid)return npr(upd(lch[nod],l,mid,p,v),rch[nod]);
else return npr(lch[nod],upd(rch[nod],mid+1,r,p,v));
}
ll qry(int nod,int old,int k)
{
if(st[nod].sc-st[old].sc==k)return st[nod].fr-st[old].fr;
if(st[lch[nod]].sc-st[lch[old]].sc>=k)return qry(lch[nod],lch[old],k);
else return st[lch[nod]].fr-st[lch[old]].fr+qry(rch[nod],rch[old],k-(st[lch[nod]].sc-st[lch[old]].sc));
}
void rec(int l,int r,int opl,int opr)
{
if(l>r)return;
int mid=(l+r)/2,bst=opl;
ll s,val=-llinf;
for(int i=max(opl,mid+m-1);i<=opr;i++)
{
s=qry(rt[i],rt[mid-1],m)-2LL*(a[i].fr.fr-a[mid].fr.fr);
if(s>val)
{
val=s;
bst=i;
}
}
ans=max(ans,val);
rec(l,mid-1,opl,bst);
rec(mid+1,r,bst,opr);
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>m;
for(i=1;i<=n;i++)cin>>a[i].fr.fr>>a[i].fr.sc;
sort(a+1,a+n+1);
reverse(a+1,a+n+1);
for(i=1;i<=n;i++)
{
a[i].sc=i;
swap(a[i].fr.sc,a[i].fr.fr);
}
sort(a+1,a+n+1);
rt[0]=build(1,n);
for(i=1;i<=n;i++)
{
rt[i]=upd(rt[i-1],1,n,a[i].sc,a[i].fr.sc);
}
rec(1,n-m+1,1,n);
cout<<ans<<endl;
return 0;
}
Compilation message (stderr)
cake3.cpp:2:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("O3")
cake3.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("unroll-loops")
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |