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;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;
const int MXN = 200005, MXC = 1000000005, LG = 40, INF = 3.9e18;
int n, m;
pii p[MXN], ans[MXN];
namespace PSMG {
#define mid ((l + r) >> 1)
#define ls(x) nd[x].L
#define rs(x) nd[x].R
struct NODE {
int val, cnt, L, R;
NODE() {}
} nd[MXN * LG];
int nc, rt[MXN];
int new_node(int ref) {
nd[nc] = nd[ref];
return nc++;
}
void pull(int id) {
nd[id].val = nd[ls(id)].val + nd[rs(id)].val;
nd[id].cnt = nd[ls(id)].cnt + nd[rs(id)].cnt;
}
int modify(int pre, int l, int r, int p) {
int id = new_node(pre);
if (l + 1 == r) {
nd[id].val += p;
nd[id].cnt++;
return id;
}
if (p < mid) ls(id) = modify(ls(id), l, mid, p);
else rs(id) = modify(rs(id), mid, r, p);
pull(id);
return id;
}
void BUILD() {
nc = 2;
rt[0] = 1;
FOR(i, 0, n) {
rt[i + 1] = modify(rt[i], 0, MXC, p[i].fs);
}
}
int query(int lid, int rid, int l, int r, int bnd) {
if (l + 1 == r) return l * bnd;
int rval = nd[rs(rid)].val - nd[rs(lid)].val;
int rcnt = nd[rs(rid)].cnt - nd[rs(lid)].cnt;
if (rcnt == bnd) return rval;
if (rcnt > bnd) return query(rs(lid), rs(rid), mid, r, bnd);
return rval + query(ls(lid), ls(rid), l, mid, bnd - rcnt);
}
int QUERY(int l, int r, int bnd) {
return query(rt[l], rt[r], 0, MXC, bnd);
}
#undef mid
#undef ls
#undef rs
}
int calc(int l, int r) {
if (r - l < m) return -INF;
int y = PSMG::QUERY(l + 1, r - 1, m - 2);
int x = p[l].fs + p[l].sc * 2 + p[r - 1].fs - p[r - 1].sc * 2 + y;
return x;
}
void SOLVE(int l, int r, int kl, int kr) {
if (l == r) return;
int mid = (l + r) >> 1;
ans[mid] = mp(-INF, -1LL);
FOR(i, kl, kr + 1) ans[mid] = max(ans[mid], mp(calc(i, mid), i));
int K = ans[mid].sc;
SOLVE(l, mid, kl, K);
SOLVE(mid + 1, r, K, kr);
}
void miku() {
cin >> n >> m;
FOR(i, 0, n) cin >> p[i].fs >> p[i].sc;
sort(p, p + n, [](pii a, pii b) -> bool {
return a.sc < b.sc;
});
PSMG::BUILD();
SOLVE(m, n + 1, 0, n - m);
int ANS = -INF;
// FOR(i, m, n + 1) cout << ans[i].fs << ' ' << ans[i].sc << '\n';
FOR(i, m, n + 1) ANS = max(ANS, ans[i].fs);
cout << ANS << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |