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 ll long long
#define pii pair<ll,int>
#define f first
#define s second
#define endl "\n"
const int N = 2e5 + 5, mod = 1e9 + 7; //!
int t, n, val[N], a[N],m, cur = 0;
pair<int,int> p[N];
ll ans;
vector<int> rem;
multiset<int> s;
ll sum = 0;
void add(int id, int t) {// cout << val[id] << " " << t << endl;
id = val[id];
if(t == 1) {
s.insert(id); sum += id;
if(s.size() > m) {
int x = *s.begin();
rem.push_back(x);
sum -= x;
s.erase(s.find(x));
}
return;
}
if(!s.count(id)) rem.pop_back();
else {
s.erase(s.find(id)); sum -= id;
if(rem.size()) {
int x = rem.back();
sum += x;
s.insert(x);
rem.pop_back();
}
}
}
void solve(int l,int r,int ql, int qr) {
if(r < l || ql > qr) return;
int mid = (l + r) >> 1;
pii opt; opt = {-2e17, ql};
for(int i = mid; i >= max(l, qr + 1); i--) add(a[i], 1);
if(mid < max(l, qr + 1))add(a[mid], 1);
for(int i = min(qr, mid - 1); i >= ql; i--) {
add(a[i], 1);
if(mid - i + 1 >= m)
opt = max(opt, make_pair(sum - 2 * (p[mid].f - p[i].f), i));
}
ans = max(ans, opt.f);
for(int i = ql; i <= min(qr, mid - 1); i++) add(a[i], -1);
if(mid < max(l, qr + 1))add(a[mid], -1);
for(int i = max(l, qr + 1); i <= mid; i++) add(a[i], -1);
if(l == r) return;
for(int i = opt.s + 1; i <= min(qr, l - 1); i++) add(a[i], 1);
solve(l, mid - 1, ql, opt.s);
for(int i = min(qr, l - 1); i >= opt.s + 1; i--) add(a[i], -1);
for(int i = max(l, qr + 1); i <= mid; i++) add(a[i], 1);
solve(mid + 1,r, opt.s, qr);
for(int i = mid; i >= max(l, qr + 1); i--) add(a[i], -1);
}
main() {
ios_base::sync_with_stdio(false),
cin.tie(0),cout.tie(0);
cin >> n >> m;
ans = -1e18;
vector<pair<int,int> > x;
for(int i = 1; i <= n; i++) {
cin >> p[i].s >> p[i].f;
}
sort(p + 1, p + n + 1);
for(int i = 1; i <= n; i++) {
x.push_back({p[i].s, i});
}
sort(x.begin(), x.end());
for(int i = 0; i < x.size(); i++) {
if(!i || x[i].f != x[i - 1].f) cur++;
val[cur] = x[i].f;
a[x[i].s] = cur;
}
solve(1, n, 1, n);
cout << ans;
}
Compilation message (stderr)
cake3.cpp: In function 'void add(int, int)':
cake3.cpp:21:15: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
21 | if(s.size() > m) {
| ^
cake3.cpp: At global scope:
cake3.cpp:69:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
69 | main() {
| ^~~~
cake3.cpp: In function 'int main()':
cake3.cpp:83:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i = 0; i < x.size(); i++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |