이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
ID: varunra2
LANG: C++
TASK: cake3
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e12
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
int posl = 0, posr = 0;
int n, m;
VVI vals;
VI dp;
void init() {
vals.resize(n);
dp.assign(n, -INF);
}
struct segtree {
int n;
VI vals;
VI pos;
vector<bool> on;
VII segtree;
void init(VVI cake) {
n = sz(cake);
vals.resize(n);
pos.resize(n);
on.assign(n, false);
for (int i = 0; i < n; i++) {
swap(cake[i][0], cake[i][1]);
}
sort(all(cake), greater<VI>());
for (int i = 0; i < n; i++) {
pos[cake[i][2]] = i;
vals[i] = cake[i][0];
}
segtree.assign(4 * n, MP(0, 0));
}
void merge(PII& a, PII& b, PII& c) { a = MP(b.x + c.x, b.y + c.y); }
void upd(int ind, int cnt, int val, int node, int l, int r) {
if (ind > r or ind < l) return;
if (l == r) {
segtree[node] = MP(val, cnt);
return;
}
int mid = (l + r) / 2;
upd(ind, cnt, val, 2 * node + 1, l, mid);
upd(ind, cnt, val, 2 * node + 2, mid + 1, r);
merge(segtree[node], segtree[2 * node + 1], segtree[2 * node + 2]);
}
int qry(int cnt, int node) {
if (cnt <= 0) return 0;
if (cnt >= segtree[node].y) return segtree[node].x;
return qry(cnt, 2 * node + 1) +
qry(cnt - segtree[2 * node + 1].y, 2 * node + 2);
}
void upd(int ind, int cnt, int val) { upd(ind, cnt, val, 0, 0, n - 1); }
bool turn_on(int ind) {
ind = pos[ind];
if (on[ind]) return false;
on[ind] = true;
upd(ind, 1, vals[ind]);
// debug("on", ind, vals[ind]);
return true;
}
bool turn_off(int ind) {
ind = pos[ind];
if (on[ind] == false) return false;
on[ind] = false;
upd(ind, 0, 0);
// debug("off", ind, vals[ind]);
return true;
}
int qry() {
int val = qry(m - 2, 0);
// debug(posl, posr, val);
return val;
}
} st;
void shiftr(int r) {
while (posr < r) {
st.turn_on(++posr);
}
while (posr > r) {
st.turn_off(posr--);
}
}
void shiftl(int l) {
while (posl < l) {
st.turn_off(posl++);
}
while (posl > l) {
st.turn_on(--posl);
}
}
void activate(int l, int r) {
if (posl > r) {
shiftl(l);
shiftr(r);
} else {
shiftr(r);
shiftl(l);
}
}
void dnc(int l, int r, int optl, int optr) {
if (l > r) return;
int mid = (l + r) / 2;
PII ret = MP(-INF, 1);
if (mid + m - 1 >= n) {
dnc(l, mid - 1, optl, optr);
return;
}
int start = max(mid + m - 1, optl);
// debug(posl, posr, start);
activate(mid + 1, start - 1);
for (int i = start; i <= optr; i++) {
int qr = st.qry();
auto cur = MP(
qr - 2 * (vals[i][0] - vals[mid][0]) + vals[i][1] + vals[mid][1], -i);
// debug(cur, mid, qr, posl, posr);
ret = max(ret, cur);
st.turn_on(i);
posr++;
}
dp[mid] = ret.x;
int opt = -ret.y;
// debug(ret, mid);
dnc(l, mid - 1, optl, opt);
dnc(mid + 1, r, opt, optr);
}
int32_t main() {
// #ifndef ONLINE_JUDGE
// freopen("cake3.in", "r", stdin);
// freopen("cake3.out", "w", stdout);
// #endif
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
init();
for (int i = 0; i < n; i++) {
int v, c;
cin >> v >> c;
vals[i] = {c, v};
}
sort(all(vals));
// debug(vals);
for (int i = 0; i < n; i++) {
vals[i].PB(i);
}
st.init(vals);
st.turn_on(0);
dnc(0, n - 1, 0, n - 1);
// debug(dp);
cout << *max_element(all(dp)) << '\n';
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... |