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 gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pdd pair<long double, long double>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pmax pair<ll, pll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
const int mxN=200002;
const int mxM=300000;
const int mxQ=3000010;
const ll MOD=998244353;
const ll INF=1e18;
int dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
int ddx[8]={1, 1, 0, -1, -1, -1, 0, 1}, ddy[8]={0, -1, -1, -1, 0, 1, 1, 1};
int N, M;
pll A[mxN];
queue <pair<pll, pll>> que[20];
set <pll> hi, lo;
ll sum;
ll ans=-INF;
void spush(ll a, ll b)
{
if(hi.size()<M)
{
sum+=a;
hi.insert(pll(a, b));
}
else if(a>hi.begin()->fi)
{
sum-=hi.begin()->fi;
lo.insert(*hi.begin());
hi.erase(hi.begin());
hi.insert(pll(a, b));
sum+=a;
}
else lo.insert(pll(a, b));
}
void spop(ll a, ll b)
{
auto it=lo.find(pll(a, b));
if(it!=lo.end()) lo.erase(it);
else
{
sum-=a;
hi.erase(hi.find(pll(a, b)));
if(lo.size())
{
hi.insert(*lo.rbegin());
sum+=lo.rbegin()->fi;
it=lo.end(); it--;
lo.erase(it);
}
}
}
int main()
{
gibon
cin >> N >> M;
for(int i=1;i<=N;i++) cin >> A[i].fi >> A[i].se;
sort(A+1, A+N+1, [](pll a, pll b){return a.se<b.se;});
que[0].emplace(pll(M, N), pll(1, N));
for(int i=0;i<20;i++)
{
hi.clear();
lo.clear();
sum=0;
int s=1, e=0;
while(que[i].size())
{
int l=que[i].front().fi.fi, r=que[i].front().fi.se, mid=(l+r)/2;
pll rng=que[i].front().se;
que[i].pop();
while(e<mid)
{
e++;
spush(A[e].fi, e);
}
while(s<rng.fi)
{
spop(A[s].fi, s);
s++;
}
pll ret=pll(-INF, 0);
for(int j=rng.fi;j<=min((ll)mid, rng.se);j++)
{
if(j!=s)
{
spop(A[s].fi, s);
s++;
}
if(hi.size()==M) ret=max(ret, pll(sum-2*(A[mid].se-A[j].se), j));
}
ans=max(ans, ret.fi);
if(l!=mid) que[i+1].emplace(pll(l, mid-1), pll(rng.fi, ret.se));
if(mid!=r) que[i+1].emplace(pll(mid+1, r), pll(ret.se, rng.se));
}
}
cout << ans;
}
Compilation message (stderr)
cake3.cpp: In function 'void spush(ll, ll)':
cake3.cpp:29:17: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
29 | if(hi.size()<M)
| ~~~~~~~~~^~
cake3.cpp: In function 'int main()':
cake3.cpp:97:29: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
97 | if(hi.size()==M) ret=max(ret, pll(sum-2*(A[mid].se-A[j].se), j));
| ~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |