This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 11.07.2022 14:10:03 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define div ___div
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define FULL(i) (MASK(i) - 1)
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class T> print_op(vector <T>) { out << '{'; for (size_t i = 0; i + 1 < u.size(); ++i) out << u[i] << ", "; if (!u.empty()) out << u.back(); return out << '}'; }
template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.fi << ", " << u.se << ')'; }
template <class InIter, class OutIter> void compress(InIter first, InIter last, OutIter result) { vector <__typeof(*first)> v(first, last); sort(v.begin(), v.end()); unique(v.begin(), v.end()); while (first != last) { *result = lower_bound(v.begin(), v.end(), *first) - v.begin() + 1; ++first; ++result; } }
template <class A, class B> inline bool minimize(A &a, B b) { A eps = 1e-9; if (a > b + eps) { a = b; return true; } return false; }
template <class A, class B> inline bool maximize(A &a, B b) { A eps = 1e-9; if (a + eps < b) { a = b; return true; } return false; }
const double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0);
const int dir[] = {1, 0, -1, 0, 1, 1, -1, -1, 1};
const int MAX = 2e5 + 5;
const long long oo = 1e18;
int N, M, B[MAX];
pair <int, int> A[MAX];
long long res = -oo;
struct Node {
long long sum;
int cnt;
Node *left, *right;
Node(): sum(0), cnt(0), left(nullptr), right(nullptr) {}
Node(Node *l, Node *r) {
sum = l->sum + r->sum;
cnt = l->cnt + r->cnt;
left = l;
right = r;
}
} *versions[MAX];
Node* build(int l, int r) {
if (l == r) return new Node();
int m = l + r >> 1;
return new Node(build(l, m), build(m + 1, r));
}
Node* upgrade(Node *pre, int l, int r, int p) {
if (l == r) {
Node *tmp = new Node();
tmp->sum = pre->sum + B[p];
tmp->cnt = pre->cnt + 1;
return tmp;
}
int m = l + r >> 1;
if (p <= m) return new Node(upgrade(pre->left, l, m, p), pre->right);
return new Node(pre->left, upgrade(pre->right, m + 1, r, p));
}
long long get(Node *left, Node *right, int l, int r, int cnt) {
if (l == r) {
int c = right->cnt - left->cnt;
if (c < cnt) return -oo;
return 1LL * cnt * B[l];
}
int m = l + r >> 1;
int c = right->right->cnt - left->right->cnt;
if (c >= cnt) return get(left->right, right->right, m + 1, r, cnt);
return right->right->sum - left->right->sum + get(left->left, right->left, l, m, cnt - c);
}
void solve(int l, int r, int from, int to) {
if (l > r) return;
int mid = l + r >> 1;
long long best = -oo, pos = -1;
for (int i = max(mid, from); i <= to; ++i)
if (maximize(best, get(versions[mid - 1], versions[i], 1, N, M) - 2 * (A[i].fi - A[mid].fi))) pos = i;
assert(pos != -1);
maximize(res, best);
solve(l, mid - 1, from, pos);
solve(mid + 1, r, pos, to);
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("cake3");
cin >> N >> M;
for (int i = 1; i <= N; ++i) cin >> A[i].se >> A[i].fi;
sort(A + 1, A + 1 + N);
for (int i = 1; i <= N; ++i) B[i] = A[i].se;
sort(B + 1, B + 1 + N);
versions[0] = build(1, N);
for (int i = 1; i <= N; ++i) {
int p = lower_bound(B + 1, B + 1 + N, A[i].se) - B;
versions[i] = upgrade(versions[i - 1], 1, N, p);
}
solve(1, N - M + 1, 1, N);
cout << res << '\n';
cerr << "Time elapsed: " << TIME << " s.\n";
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'Node* build(int, int)':
cake3.cpp:56:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int m = l + r >> 1;
| ~~^~~
cake3.cpp: In function 'Node* upgrade(Node*, int, int, int)':
cake3.cpp:67:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | int m = l + r >> 1;
| ~~^~~
cake3.cpp: In function 'long long int get(Node*, Node*, int, int, int)':
cake3.cpp:78:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
78 | int m = l + r >> 1;
| ~~^~~
cake3.cpp: In function 'void solve(int, int, int, int)':
cake3.cpp:86:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
86 | int mid = l + r >> 1;
| ~~^~~
cake3.cpp: In function 'int main()':
cake3.cpp:22:69: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cake3.cpp:98:2: note: in expansion of macro 'file'
98 | file("cake3");
| ^~~~
cake3.cpp:22:103: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
cake3.cpp:98:2: note: in expansion of macro 'file'
98 | file("cake3");
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |