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>
#define ll long long
#define f first
#define s second
#define pb push_back
#define N 200005
using namespace std;
pair<ll,ll>a[N];
long long co,cur,z,kk,sum1,sum2,of;
int t,raod,pp,mid,ind;
ll n,m,ans=-9999999999999999;
bool lst[3 * N + 60000];
vector<long long>v[3*N + 60000];
vector<int>b[3*N + 60000],g;
void build(int x){
if((int)v[x].size() <= 1){
lst[x] = 1;
return;
}
g.clear();
for(int i=1; i<v[x].size(); i++)
g.pb(v[x][i]);
sort(g.begin() , g.end());
if(g[0] == g[g.size() - 1]){
lst[x] = 1;
return;
}
of=0;
mid = g[((int)g.size() - 1) / 2];
for(int i=((int)g.size() - 1) / 2; i>=0; i--){
if(g[i] == mid)of++;
}
raod = 0,pp = 0;
v[2*x].pb(0);b[2*x].pb(0);
v[2*x+1].pb(0);b[2*x+1].pb(0);
kk = v[x][1] , sum1 = 0 , sum2 = 0;
for(int i=1; i<v[x].size(); i++){
t = v[x][i];
if(t == mid)of--;
if(t != kk)pp = 1;
if(t <= mid && !(t == mid && of < 0)){
v[2 * x].pb(t);
sum1 += t;
raod++;
}
else{
v[2 * x + 1].pb(t);
sum2 += t;
}
b[x].pb(raod);
v[x][i] = sum2;
}
if(!pp){lst[x] = 1;return;}
build(2 * x);
build(2 * x + 1);
}
void count(int x,int L,int R,int rao){
if(lst[x] == 1){
if((int)v[x].size() <= 1)return;
co += min((ll)rao , (ll)((int)v[x].size() - 1)) * v[x][1];
return;
}
if(R - L + 1 - b[x][R] + b[x][L - 1] >= rao)
count(2 * x + 1 , L - b[x][L - 1] , R - b[x][R] , rao);
if(R - L + 1 - b[x][R] + b[x][L - 1] < rao){
co += v[x][R] - v[x][L - 1];
count(2 * x, b[x][L - 1] + 1 , b[x][R] , rao - (R - L + 1 - b[x][R] + b[x][L - 1]));
}
}
void buildwavelet(){
v[1].pb(0);
b[1].pb(0);
for(int i=1; i<=n; i++)
v[1].pb(a[i].s);
build(1);
}
ll get(int x,int y,int k){
co = 0;
count(1 , x , y , k);
return co;
}
void solve(ll l,ll r,ll x,ll y){
l = max(l , x + m - 1);
if(l > r)return;
ll k = (l + r) / 2;
ll mx = -9999999999999999 , ind = y;
ll cur = 0;
for(int i=min(y , k - m + 1); i>=x; i--){
cur = get(i + 1 , k - 1 , m - 2) + a[i].s + a[k].s - 2 * (a[k].f - a[i].f);
if(cur > mx)
mx = cur,
ind = i;
}
if(mx == -9999999999999999)return;
ans = max(ans , mx);
if(l == r)return;
solve(l , k , x , ind);
solve(k + 1 , r , ind , y);
}
int main(){
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i=1; i<=n; i++){
cin >> a[i].s >> a[i].f;
}
sort(a + 1 , a + n + 1);
buildwavelet();
solve(1 , n , 1 , n);
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'void build(int)':
cake3.cpp:21:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1; i<v[x].size(); i++)
~^~~~~~~~~~~~
cake3.cpp:37:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1; i<v[x].size(); 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... |