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;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
pi A[200005];
int dp[200005];
multiset <int> ms, buf;
int L = 1, R = 0, cur = 0, ans = -1e18;
void shift(int l, int r){
while(L < l){
if(buf.find(A[L].se) != buf.end())buf.erase(buf.find(A[L].se));
else ms.erase(ms.find(A[L].se)), cur -= A[L].se;
while(ms.size() < m && !buf.empty())ms.insert(*--buf.end()), cur += *--buf.end(), buf.erase(--buf.end());
L++;
}
while(L > l){
L--;
buf.insert(A[L].se);
while(ms.size() < m && !buf.empty())cur += *--buf.end(), ms.insert(*--buf.end()), buf.erase(--buf.end());
while(!ms.empty() && !buf.empty() && *ms.begin() < *--buf.end()){
int x = *ms.begin(), y = *--buf.end();
ms.erase(ms.begin());
buf.erase(--buf.end());
ms.insert(y); buf.insert(x);
cur += y - x;
}
}
while(R < r){
R++;
buf.insert(A[R].se);
while(ms.size() < m && !buf.empty())cur += *--buf.end(), ms.insert(*--buf.end()), buf.erase(--buf.end());
while(!ms.empty() && !buf.empty() && *ms.begin() < *--buf.end()){
int x = *ms.begin(), y = *--buf.end();
ms.erase(ms.begin());
buf.erase(--buf.end());
ms.insert(y); buf.insert(x);
cur += y - x;
}
}
while(R > r){
if(buf.find(A[R].se) != buf.end())buf.erase(buf.find(A[R].se));
else ms.erase(ms.find(A[R].se)), cur -= A[R].se;
while(ms.size() < m && !buf.empty())cur += *--buf.end(), ms.insert(*--buf.end()), buf.erase(--buf.end());
R--;
}
}
void dnc(int l, int r, int optl, int optr){
if(l > r)return;
int mid = (l + r) >> 1;
int mx = -1e18, pos = optl;
for(int i = optl; i <= min(optr, mid); i++){
shift(i, mid);
if(ms.size() == m && mx < cur - 2 * (A[mid].fi - A[i].fi))mx = cur - 2 * (A[mid].fi - A[i].fi), pos = i;
}
//cout << mid << ' ' << mx << ' ' << pos << '\n';
ans = max(ans, mx);
dnc(l, mid - 1, optl, pos);
dnc(mid + 1, r, pos, optr);
}
void solve(){
cin >> n >> m;
for(int i=1;i<=n;i++)cin >> A[i].se >> A[i].fi;
sort(A+1, A+n+1);
dnc(1, n, 1, n);
cout << ans;
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
cake3.cpp: In function 'void shift(long long int, long long int)':
cake3.cpp:24:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
24 | while(ms.size() < m && !buf.empty())ms.insert(*--buf.end()), cur += *--buf.end(), buf.erase(--buf.end());
| ~~~~~~~~~~^~~
cake3.cpp:30:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
30 | while(ms.size() < m && !buf.empty())cur += *--buf.end(), ms.insert(*--buf.end()), buf.erase(--buf.end());
| ~~~~~~~~~~^~~
cake3.cpp:42:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
42 | while(ms.size() < m && !buf.empty())cur += *--buf.end(), ms.insert(*--buf.end()), buf.erase(--buf.end());
| ~~~~~~~~~~^~~
cake3.cpp:54:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
54 | while(ms.size() < m && !buf.empty())cur += *--buf.end(), ms.insert(*--buf.end()), buf.erase(--buf.end());
| ~~~~~~~~~~^~~
cake3.cpp: In function 'void dnc(long long int, long long int, long long int, long long int)':
cake3.cpp:65:16: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
65 | if(ms.size() == m && mx < cur - 2 * (A[mid].fi - A[i].fi))mx = cur - 2 * (A[mid].fi - A[i].fi), pos = i;
| ~~~~~~~~~~^~~~
cake3.cpp: At global scope:
cake3.cpp:81:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
81 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |