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 st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (k+k)
#define sag (k+k+1)
#define orta ((bas+son)/2)
#define coc g[node][i]
#define SURE 3.95
#define mod 1000000007
#define inf 1000000000000000009
#define N 1000005
using namespace std;
typedef long long ll;
typedef pair < ll , ll > ii;
ll n, m, k, ans = -inf, bas = 1, son, amk, a[N], b[N], ne[N], say[4*N], seg[4*N];
ii xx[N];
map < ll , ll > h, x;
map < ll , ll > :: iterator it;
void bitir(){
printf("%lld\n", ans);
exit(0);
}
void up(ll k, ll bas, ll son, ll x, ll y){
if(bas == son){
seg[k] += y*ne[bas];
say[k] += y;
return;
}
if(x <= orta)
up(sol, bas, orta, x, y);
else
up(sag, orta + 1, son, x, y);
seg[k] = seg[sol] + seg[sag];
say[k] = say[sol] + say[sag];
}
ll qu(ll k, ll bas, ll son, ll x){
if(bas == son)
return x*ne[bas];
if(say[sag] >= x)
return qu(sag, orta + 1, son, x);
return seg[sag] + qu(sol, bas, orta, x - say[sag]);
}
void getir(ll bs, ll sn){
while(bas < bs){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
up(1, 1, k, x[a[bas]], -1);
bas++;
}
while(bas > bs){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
bas--;
up(1, 1, k, x[a[bas]], 1);
}
while(son > sn){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
up(1, 1, k, x[a[son]], -1);
son--;
}
while(son < sn){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
son++;
up(1, 1, k, x[a[son]], 1);
}
}
void f(ll bas, ll son, ll optl, ll optr){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
// amk++;if(amk > 50)exit(0);
if(bas > son)return;
ll optm = optr, opt = -inf;
for(ll i = optl; i <= optr; i++){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
if(i - orta + 1 < m)
continue;
getir(orta + 1, i - 1);
ll of = qu(1, 1, k, m - 2);
ll cvp = of + a[orta] + a[i] - 2*(b[i] - b[orta]);
// if(cvp == 152)cout << "geldi amk " << orta << " " << i << " " << of << " = " << cvp << endl;
if(cvp > opt){
opt = cvp;
optm = i;
}
}
// cout << bas << " " << son << " " << orta << " " << optl << " " << optr << " = " << opt << " " << optm << endl;
ans = max(ans, opt);
f(bas, orta - 1, optl, optm);
f(orta + 1, son, optm, optr);
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
scanf("%lld %lld",&n ,&m);
for(ll i = 1; i <= n; i++){
scanf("%lld %lld",&xx[i].nd ,&xx[i].st);
h[xx[i].nd]++;
}
for(it = h.begin(); it != h.end(); it++){
x[it->st] = ++k;
ne[k] = it->st;
// cout << k << " " << it->st << endl;
}
sort(xx + 1, xx + n + 1);
for(ll i = 1; i <= n; i++){
a[i] = xx[i].nd;
b[i] = xx[i].st;
// cout << a[i] << " " << b[i] << endl;;
}
// cout << endl;
// getir(2, 4);
// cout << qu(1, 1, k, 3) << endl;
// cout << qu(1, 1, k, 2) << endl;
// cout << qu(1, 1, k, 1) << endl;
// cout << qu(1, 1, k, 0) << endl;
f(1, n, 1, n);
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'int main()':
cake3.cpp:101:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&n ,&m);
~~~~~^~~~~~~~~~~~~~~~~~~~
cake3.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&xx[i].nd ,&xx[i].st);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |