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;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#define int long long
typedef long long ll;
#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];
multiset <int> ms, buf;
int L = 1, R = 0, x, y;
ll cur = 0, ans = -1e18;
inline void shift(const int l, const 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()){
x = *--buf.end();
ms.insert(x), cur += x, 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()){
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()){
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()){
x = *--buf.end();
ms.insert(x), cur += x, buf.erase(--buf.end());
}
R--;
}
}
void dnc(int l, int r, int optl, int optr){
if(l > r)return;
int mid = (l + r) >> 1;
ll mx = -1e18; int pos = optl;
for(int i = optl; i <= min(optr, mid); i++){
shift(i, mid);
if(ms.size() < m)break;
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(int, int)':
cake3.cpp:29:19: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
29 | while(ms.size() < m && !buf.empty()){
| ~~~~~~~~~~^~~
cake3.cpp:38:19: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
38 | while(ms.size() < m && !buf.empty())cur += *--buf.end(), ms.insert(*--buf.end()), buf.erase(--buf.end());
| ~~~~~~~~~~^~~
cake3.cpp:50:19: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
50 | while(ms.size() < m && !buf.empty())cur += *--buf.end(), ms.insert(*--buf.end()), buf.erase(--buf.end());
| ~~~~~~~~~~^~~
cake3.cpp:62:19: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
62 | while(ms.size() < m && !buf.empty()){
| ~~~~~~~~~~^~~
cake3.cpp: In function 'void dnc(int, int, int, int)':
cake3.cpp:76:16: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
76 | if(ms.size() < m)break;
| ~~~~~~~~~~^~~
cake3.cpp:77:16: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
77 | 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:93:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
93 | 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... |