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>
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);
}
int n,m;
vector<pair<int,int>> v;
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();
}
};
vector<seg*> tr;
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);
}
}
int ans1=-INF;
void dc(int l,int r,int a,int b){
if(l>=r){
return;
}
pair<int,int> p={-INF,-1};
int m1=(l+r)>>1;
for(int i=a;i<=min(b,m1-1);i++){
p=max(p,{ask(tr[i],tr[m1],m)-(v[m1].x-v[i+1].x)*2,i});
//cout<<m1<<' '<<i<<endl;
}
ans1=max(ans1,p.first);
dc(l,m1,a,p.second);
dc(m1+1,r,p.second,b);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
v.resize(n+1);
for(int i=1;i<=n;i++){
cin>>v[i].y>>v[i].x;
}
sort(v.begin(),v.end());
tr.resize(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;
dc(m,n+1,0,n);
cout<<ans1<<endl;
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'int32_t main()':
cake3.cpp:108:9: warning: unused variable 'idx' [-Wunused-variable]
108 | int idx=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... |