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 f first
#define s second
#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7; // !
int t, val[N],m;
pii a[N];
int ans = -1e18;
vector<int> x;
vector<int> c[N], sum[N];
void build(int u, int l, int r, vector<int> v) {
if(l == r) return;
int mid = (l + r) / 2;
vector<int> vl, vr;
c[u].resize((int)v.size());
sum[u].resize((int)v.size());
for(int i = 0; i < v.size(); i++) {
if(i) c[u][i] = c[u][i - 1], sum[u][i] = sum[u][i - 1];
if(v[i] <= mid) vl.push_back(v[i]), ++c[u][i], sum[u][i] += val[v[i]];
else vr.push_back(v[i]);
}
build(2 * u, l, mid, vl);
build(2 * u + 1, mid + 1, r, vr);
}
int get(int u, int l, int r, int L, int R, int k) {
if(l == r) return k * val[l];
int mid = (l + r) / 2;
if(c[u][R] - (L > 0) * c[u][L - 1] >= k) return get(2 * u, l, mid, (L > 0 ? c[u][L - 1] : 0), c[u][R] - 1, k);
return sum[u][R] - (L > 0) * sum[u][L - 1] + get(2 * u + 1, mid + 1, r, (L > 0 ? L - 1 - c[u][L - 1] + 1 : 0), R - c[u][R], k - (c[u][R] - (L > 0) * c[u][L - 1]));
}
void solve(int l, int r, int optl, int optr) {
int mid = (l + r) / 2, cur = -1e18, opt = -1;
for(int i = optl; i <= min(mid - m + 1, optr); i++) {
int V = get(1, 1, (int)x.size(), i - 1, mid - 1, m) - 2 * (a[mid].f - a[i].f);
if(V > cur) cur = V, opt = i;
}
ans = max(ans, cur);
if(l == r) return;
if(l < mid) solve(l, mid - 1, optl, opt);
if(mid < r) solve(mid + 1, r, opt, optr);
}
main(){
int n;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> a[i].s >> a[i].f;
x.push_back(a[i].s);
}
sort(a + 1, a + n + 1);
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
for(int i = 0; i < x.size(); i++) val[i + 1] = x[i];
vector<int> v;
for(int i = 1; i <= n; i++) {
a[i].s = lower_bound(x.begin(), x.end(), a[i].s) - x.begin() + 1;
v.push_back(a[i].s);
}
build(1, 1, (int)x.size(), v);
solve(m, n, 1, n);
cout << ans;
}
Compilation message (stderr)
cake3.cpp: In function 'void build(long long int, long long int, long long int, std::vector<long long int>)':
cake3.cpp:19:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
cake3.cpp: At global scope:
cake3.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
44 | main(){
| ^~~~
cake3.cpp: In function 'int main()':
cake3.cpp:55:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i = 0; i < x.size(); i++) val[i + 1] = x[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... |