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;
struct ab {
int a, b;
};
struct event {
int type, val, t;
};
int m;
const int MAX_N = 2e5;
const long long INF = 1e18;
ab v[MAX_N + 1];
int timee = 0;
long long sumA;
vector<event> events;
multiset<int> s;
void rollback( int t ) {
while ( !events.empty() && events.back().t > t ) {
int x = events.back().val;
if (events.back().type == 1 ) {
s.erase( s.find( x ) );
sumA -= x;
} else {
s.insert( x );
sumA += x;
}
events.pop_back();
}
}
long long solve( int ll, int lr, int rl, int rr ) {
if ( ll > lr || rl > rr )
return -INF;
int l = (ll + lr) / 2;
long long answer = -INF;
int bestR = rr;
int t = timee;
for ( int i = l; i <= lr && i < rl; i++ ) {
sumA += v[i].a;
events.push_back( { 1, v[i].a, ++timee, } );
s.insert( v[i].a );
if ( s.size() > m ) {
sumA -= *s.begin();
events.push_back( { -1, *s.begin(), ++timee, } );
s.erase( s.begin() );
}
}
for ( int r = max( l, rl ); r <= rr; r++ ) {
s.insert( v[r].a );
sumA += v[r].a;
events.push_back( { 1, v[r].a, ++timee, } );
if ( s.size() > m ) {
sumA -= *s.begin();
events.push_back( { -1, *s.begin(), ++timee, } );
s.erase( s.begin() );
}
if ( s.size() >= m ) {
if ( sumA - 2 * (v[r].b - v[l].b) > answer ) {
answer = sumA - 2 * (v[r].b - v[l].b);
bestR = r;
}
}
}
if ( ll <= l - 1 ) {
rollback( t );
for ( int i = min( rl - 1, lr ); i >= l; i-- ) {
s.insert( v[i].a );
sumA += v[i].a;
events.push_back( { 1, v[i].a, ++timee, } );
if ( s.size() > m ) {
sumA -= *s.begin();
events.push_back( { -1, *s.begin(), ++timee, } );
s.erase( s.begin() );
}
}
answer = max( answer, solve( ll, l - 1, rl, bestR ) );
}
if ( l + 1 <= lr ) {
rollback( t );
for ( int i = max( rl, lr + 1 ); i < bestR; i++ ) {
s.insert( v[i].a );
sumA += v[i].a;
events.push_back( { 1, v[i].a, ++timee, } );
if ( s.size() > m ) {
sumA -= *s.begin();
events.push_back( { -1, *s.begin(), ++timee, } );
s.erase( s.begin() );
}
}
answer = max( answer, solve( l + 1, lr, bestR, rr ) );
}
rollback( t );
return answer;
}
int main() {
int n;
cin.tie( NULL );
ios_base::sync_with_stdio( false );
cin >> n >> m;
for ( int i = 1; i <= n; i++ )
cin >> v[i].a >> v[i].b;
sort( v + 1, v + 1 + n, []( ab x, ab y ) {
return x.b < y.b;
} );
cout << solve( 1, n, 1, n );
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'long long int solve(int, int, int, int)':
cake3.cpp:51:23: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | if ( s.size() > m ) {
| ~~~~~~~~~^~~
cake3.cpp:62:23: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
62 | if ( s.size() > m ) {
| ~~~~~~~~~^~~
cake3.cpp:68:23: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
68 | if ( s.size() >= m ) {
| ~~~~~~~~~^~~~
cake3.cpp:83:27: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
83 | if ( s.size() > m ) {
| ~~~~~~~~~^~~
cake3.cpp:99:27: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
99 | if ( s.size() > m ) {
| ~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |