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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int L, R, N, M, ans = LLONG_MIN, V[200005], DV[200005], RV[200005], C[200005];
ii T[200005], ft[200005];
vector<int> disc;
priority_queue<int, vector<int>, greater<int> > pq;
inline int ls(int x) { return x & -x; }
void upd(int p, int a, int b) {
for (; p <= disc.size(); p += ls(p))
ft[p].first += a, ft[p].second += b;
}
ii qry(int p) {
ii r = mp(0, 0);
for (; p; p -= ls(p)) {
r.first += ft[p].first;
r.second += ft[p].second;
}
return r;
}
void add(int x) {
upd(DV[x], V[x], 1);
}
void del(int x) {
upd(DV[x], -V[x], -1);
}
int k_largest(int k) {
int curr = 0, cnt = 0;
for (int i = 18; i >= 0; i--)
if (curr + (1 << i) <= disc.size() && cnt + ft[curr + (1 << i)].second < k) {
cnt += ft[curr + (1 << i)].second;
curr += (1 << i);
}
curr++;
assert(curr <= disc.size());
ii tmp = qry(curr);
return tmp.first - (tmp.second - k) * RV[curr];
}
void shift(int l, int r) {
while (l < L) add(--L);
while (r > R) add(++R);
while (l > L) del(L++);
while (r < R) del(R--);
}
void dnc(int l, int r, int x, int y) {
if (l > r) return;
int m = (l + r) >> 1, sum = 0, opt = LLONG_MIN, pos = -1;
for (; !pq.empty(); pq.pop());
for (int i = min(m - M + 1, y); i >= x; i--) {
shift(i, m);
sum = k_largest(M);
if (sum - 2 * (C[m] - C[i]) > opt) {
opt = sum - 2 * (C[m] - C[i]);
pos = i;
}
}
assert(pos != -1);
ans = max(ans, opt);
dnc(l, m - 1, x, pos);
dnc(m + 1, r, pos, y);
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++) {
cin >> T[i].second >> T[i].first;
disc.pb(T[i].second);
}
sort(T + 1, T + 1 + N);
sort(disc.begin(), disc.end());
for (int i = 1; i <= N; i++) {
tie(C[i], V[i]) = T[i];
DV[i] = disc.size() - (lower_bound(disc.begin(), disc.end(), V[i]) - disc.begin() + 1) + 1;
RV[DV[i]] = V[i];
}
L = R = 1;
add(1);
dnc(M, N, 1, N);
cout << ans << '\n';
}
Compilation message (stderr)
cake3.cpp: In function 'void upd(long long int, long long int, long long int)':
cake3.cpp:36:11: 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]
36 | for (; p <= disc.size(); p += ls(p))
| ~~^~~~~~~~~~~~~~
cake3.cpp: In function 'long long int k_largest(long long int)':
cake3.cpp:60:23: 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]
60 | if (curr + (1 << i) <= disc.size() && cnt + ft[curr + (1 << i)].second < k) {
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/10/ext/pb_ds/detail/pat_trie_/pat_trie_.hpp:45,
from /usr/include/c++/10/ext/pb_ds/detail/container_base_dispatch.hpp:90,
from /usr/include/c++/10/ext/pb_ds/assoc_container.hpp:48,
from cake3.cpp:2:
cake3.cpp:65:14: 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]
65 | assert(curr <= disc.size());
| ~~~~~^~~~~~~~~~~~~~
cake3.cpp: At global scope:
cake3.cpp:95:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
95 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |