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.5
#define mod 1000000007
#define inf 1000000000000000009
#define N 1000005
using namespace std;
typedef long long ll;
typedef pair < int , int > ii;
int n, m, k, bas = 1, son, bs = 1, sn, amk, a[N], b[N], ne[N], say[4*N];
ll seg[4*N], of, cvp, opt, ans = -inf;
ii xx[N];
map < int , int > h, x;
map < int , int > :: iterator it;
pair < ii , ii > q[N];
void bitir(){
printf("%lld\n", ans);
exit(0);
}
void up(int k, int bas, int son, int x, int y){
if(bas == son){
seg[k] += 1ll*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(int k, int bas, int son, int x){
if(bas == son)
return 1ll*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(int bs, int sn){
while(bas < bs){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
up(1, 1, k, a[bas], -1);
bas++;
}
while(bas > bs){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
bas--;
up(1, 1, k, a[bas], 1);
}
while(son > sn){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
up(1, 1, k, a[son], -1);
son--;
}
while(son < sn){
if(clock()/CLOCKS_PER_SEC > SURE)bitir();
son++;
up(1, 1, k, a[son], 1);
}
}
void f(){
int bas = q[bs].st.st;
int son = q[bs].st.nd;
int optl = q[bs].nd.st;
int optr = q[bs].nd.nd;
bs++;
// cout << bas << " " << son << " " << optl << " " << optr << endl;
// if(clock()/CLOCKS_PER_SEC > SURE)bitir();
// amk++;if(amk > 50)exit(0);
if(bas > son)return;
int optm = optr;
opt = -inf;
for(int i = m + orta - 1; i <= optr; i++){
// if(clock()/CLOCKS_PER_SEC > SURE)bitir();
getir(orta + 1, i - 1);
of = qu(1, 1, k, m - 2);
cvp = of + 1ll*ne[a[orta]] + 1ll*ne[a[i]] - 2ll*(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);
if(bas <= orta - 1)
q[++sn] = mp(mp(bas, orta - 1), mp(optl, optm));
if(orta + 1 <= son)
q[++sn] = mp(mp(orta + 1, son), mp(optm, optr));
// cout << "ekledik " << bs << " " << sn << endl;
// 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("%d %d",&n ,&m);
for(int i = 1; i <= n; i++){
scanf("%d %d",&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(int i = 1; i <= n; i++){
a[i] = x[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;
q[++sn] = mp(mp(1, n), mp(m, n));
while(sn - bs + 1 > 0){
f();
}
// f(1, n, m, n);
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'int main()':
cake3.cpp:113:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n ,&m);
~~~~~^~~~~~~~~~~~~~~~
cake3.cpp:115:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&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... |