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;
using ll = long long;
using t4=tuple<int,int,int,int>;
const int N=2e5+5;
const int K=1<<19;
const ll INF=LLONG_MAX/2;
int n,m;
ll v[N],c[N],pos[N];
ll ans=-INF;
pair<int,int> a[N];
vector<pair<int,int>> vec;
vector<t4> q;
struct Node{
ll sum;
int freq;
Node():sum(0),freq(0){}
Node(ll v):sum(v),freq(1){}
Node(ll v,int f):sum(v),freq(f){}
friend Node operator+(const Node &lhs,const Node &rhs){
return Node(lhs.sum+rhs.sum,lhs.freq+rhs.freq);
}
};
struct SegTree{
Node t[K];
void build(int l,int r,int i){
t[i]=Node();
if(l==r)return;
int m=(l+r)/2;
build(l,m,i*2);
build(m+1,r,i*2+1);
}
void build(){
build(1,n,1);
}
void update(int l,int r,int i,int x,const Node &v){
if(x<l||r<x)return;
if(l==r)return void(t[i]=v);
int m=(l+r)/2;
update(l,m,i*2,x,v);
update(m+1,r,i*2+1,x,v);
t[i]=t[i*2]+t[i*2+1];
}
void update(int x,const Node &v){
update(1,n,1,x,v);
}
ll query(int l,int r,int i,int k){
if(l==r)return t[i].sum;
int m=(l+r)/2;
if(t[i*2+1].freq<k)return query(l,m,i*2,k-t[i*2+1].freq)+t[i*2+1].sum;
return query(m+1,r,i*2+1,k);
}
ll query(int k){
return query(1,n,1,k);
}
}s;
int st=1,ed=0;
void work(int l,int r){
while(ed<r)s.update(pos[++ed],Node(v[ed]));
while(st>l)s.update(pos[--st],Node(v[st]));
while(ed>r)s.update(pos[ed--],Node());
while(st<l)s.update(pos[st++],Node());
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for(int i=1;i<=n;i++)cin >> a[i].second >> a[i].first;
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
tie(c[i],v[i])=a[i];
vec.emplace_back(v[i],i);
}
sort(vec.begin(),vec.end());
for(int i=0;i<n;i++)pos[vec[i].second]=i+1;
q.emplace_back(1,n-m+1,1,n);
while(!q.empty()){
vector<t4> nq;
for(auto [l,r,optl,optr]:q){
if(l>r)continue;
int mid=(l+r)/2;
ll res=-INF;
int opt=-1;
for(int i=max(mid+m-1,optl),cnt=0;i<=optr;i++){
work(mid,i);
ll tmp=s.query(m)-2*c[i];
if(tmp>res)res=tmp,opt=i;
}
res+=2*c[mid];
ans=max(ans,res);
nq.emplace_back(l,mid-1,optl,opt);
nq.emplace_back(mid+1,r,opt,optr);
}
swap(q,nq);
}
cout << ans;
}
Compilation message (stderr)
cake3.cpp: In function 'void work(int, int)':
cake3.cpp:67:29: warning: operation on 'ed' may be undefined [-Wsequence-point]
67 | while(ed<r)s.update(pos[++ed],Node(v[ed]));
| ^~~~
cake3.cpp:68:29: warning: operation on 'st' may be undefined [-Wsequence-point]
68 | while(st>l)s.update(pos[--st],Node(v[st]));
| ^~~~
cake3.cpp: In function 'int main()':
cake3.cpp:92:41: warning: unused variable 'cnt' [-Wunused-variable]
92 | for(int i=max(mid+m-1,optl),cnt=0;i<=optr;i++){
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |