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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,k;
ii arr[200005];
vector<int> uni;
map<int,int> vmap;
struct wavelet{
vector<int> index; ///indexes inside this segment
vector<int> val; ///values inside this segment (sorted)
vector<ll> pref;
vector<int> l_child;
vector<int> r_child;
int _min,_max;
wavelet *l, *r;
wavelet(){}
void init(){
_min=val.front();
_max=val.back();
pref.push_back(0);
rep(x,0,sz(val)) pref.push_back(pref.back()+uni[arr[index[x]].fi]);
if (_min==_max) return; ///dont need to propogate more
l=new wavelet();
r=new wavelet();
int pivot=max(val[val.size()>>1],_min+1);
l_child.push_back(-1);
r_child.push_back(-1);
for (int x=0;x<val.size();x++){
///we handle val and index seperately
if (val[x]<pivot) l->val.push_back(val[x]);
else r->val.push_back(val[x]);
if (arr[index[x]].fi<pivot){ //arr stores values of stuff
l->index.push_back(index[x]);
l_child.push_back(l_child.back()+1);
r_child.push_back(r_child.back());
}
else {
r->index.push_back(index[x]);
l_child.push_back(l_child.back());
r_child.push_back(r_child.back()+1);
}
}
l->init();
r->init();
}
ll query(int i,int j,ll k){
if (_min==_max) return k*uni[_min];
int len=r_child[j+1]-r_child[i];
if (len<=k) return r->pref[r_child[j+1]+1]-r->pref[r_child[i]+1]+l->query(l_child[i]+1,l_child[j+1],k-len);
else return r->query(r_child[i]+1,r_child[j+1],k);
}
}*root=new wavelet();
ll ans=-1e18;
void dp(int l,int r,int optl,int optr){
if (l>r) return;
int m=l+r>>1;
int optm=-1;
ll best=-1e18;
rep(x,optl,optr+1) if (x-m+1>=k){
ll val=2*(arr[m].se-arr[x].se)+uni[arr[m].fi]+uni[arr[x].fi]+root->query(m+1,x-1,k-2);
//cout<<m<<" "<<x<<" "<<val<<endl;
if (val>best){
best=val;
optm=x;
}
}
ans=max(ans,best);
dp(l,m-1,optl,optm),dp(m+1,r,optm,optr);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
rep(x,0,n){
cin>>arr[x].fi>>arr[x].se;
uni.push_back(arr[x].fi);
}
sort(arr,arr+n,[](ii i,ii j){
return i.se<j.se;
});
sort(all(uni));
uni.erase(unique(all(uni)),uni.end());
rep(x,0,sz(uni)){
vmap[uni[x]]=x;
}
rep(x,0,n){
arr[x].fi=vmap[arr[x].fi];
root->index.push_back(x);
root->val.push_back(arr[x].fi);
}
sort(all(root->val));
root->init();
//rep(x,0,n) cout<<uni[arr[x].fi]<<" "; cout<<endl;
dp(0,n-k,0,n-1);
cout<<ans<<endl;
}
Compilation message (stderr)
cake3.cpp: In member function 'void wavelet::init()':
cake3.cpp:62:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int x=0;x<val.size();x++){
~^~~~~~~~~~~
cake3.cpp: In function 'void dp(int, int, int, int)':
cake3.cpp:98:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m=l+r>>1;
~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |