이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifdef local
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : "\033[0m)\n")));
}
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
#define sort_uni(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;
using ll = int64_t;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1e18;
const int maxn = 500025, inf = 1e9;
struct Event {
int l, r, fl, fr;
};
vector<int> u;
struct kthMaxsum {
int cnt[maxn];
ll sum[maxn];
int sz;
void insert(int x) {
++sz;
int d = u[x];
for (++x; x < maxn; x += x & -x)
cnt[x] += 1, sum[x] += d;
}
void erase(int x) {
--sz;
int d = u[x];
for (++x; x < maxn; x += x & -x)
cnt[x] -= 1, sum[x] -= d;
}
ll query(int k) {
if (sz < k) return -INF;
int p = 0;
for (int s = 1 << 20; s; s >>= 1) {
if (p + s < maxn && cnt[p + s] < k) {
k -= cnt[p += s];
}
}
ll res = 1LL * u[p] * k;
for (; p > 0; p -= p & -p)
res += sum[p];
return res;
}
} ds;
pair<int,int> v[maxn];
int n, k;
int id[maxn];
void update(int l, int r) { // [l, r]
static int curL = 1, curR = 0;
while (curR < r) ds.insert(id[++curR]);
while (curL > l) ds.insert(id[--curL]);
while (curR > r) ds.erase(id[curR--]);
while (curL < l) ds.erase(id[curL++]);
}
ll res = -INF;
void solve(int l, int r, int fl, int fr) {
if (l == r) return;
int m = (l+r)/2;
ll ans = -INF;
int pos = -1;
for (int i = fl; i < fr && i < m; i++) {
update(i+1, m-1);
ll cur = ds.query(k) + v[i].second + v[m].second - abs(v[i].first - v[m].first) * 2;
if (ans < cur) {
ans = cur;
pos = i;
}
}
res = max(res, ans);
solve(l, m, fl, pos+1);
solve(m+1, r, pos, fr);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
/*
int seed = time(0);
debug(seed);
srand(seed);
for (int i = 0; i < n; i++) v[i] = {rand() % 10, rand() % 10};
*/
for (int i = 0; i < n; i++) cin >> v[i].second >> v[i].first;
sort(v, v+n);
k -= 2;
/*
ll ans = -INF;
for (int i = 0; i < n; i++) {
priority_queue<int, vector<int>, greater<int>> pq;
ll sum = 0;
for (int j = i+1; j < n; j++) {
if (pq.size() == k)
ans = max(ans, sum + v[i].second + v[j].second - (v[j].first - v[i].first) * 2);
sum += v[j].second, pq.push(v[j].second);
if (pq.size() > k) sum -= pq.top(), pq.pop();
}
}
debug(ans);
*/
for (int i = 0; i < n; i++) u.push_back(v[i].second);
sort_uni(u);
reverse(all(u));
for (int i = 0; i < n; i++) id[i] = lower_bound(all(u), v[i].second, greater<>()) - u.begin();
solve(k+1, n, 0, n);
cout << res << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |