이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Copyright © 2024 Youngmin Park. All rights reserved.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#pragma region TEMPLATE
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
const ld PI = acos((ld)-1.0);
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
constexpr bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
constexpr bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // divide a by b rounded down
#ifdef LOCAL
#include "miscellaneous/debug.h"
#else
#define dbg(...) 42
#endif
#pragma endregion TEMPLATE
/**
* Description: Performs range prefix sum queries and point updates. lower_bound returns the first prefix sum >= v
* Source: Own
* Verification: http://www.usaco.org/index.php?page=viewproblem2&cpid=693
* Time Complexity: O(log n) for query and update
* 0-indexing
*/
template <typename T>
struct BIT {
int N; vector<T> bit;
BIT(int n) : N(n), bit(n + 1) {}
void upd(int id, T v) {
for (id++; id <= N; id += id & -id)
bit[id] += v;
}
T query(int id) {
T res = 0;
for (id++; id > 0; id -= id & -id)
res += bit[id];
return res;
}
T query(int l, int r) {
return l > r ? 0 : query(r) - query(l - 1);
}
T lower_bound(T v) {
int id = 0;
T sum = 0;
int lg = 31 - __builtin_clz(N);
for (int j = lg; ~j; j--) {
if (id + (1 << j) <= N && sum + bit[id + (1 << j)] < v) {
id += (1 << j);
sum += bit[id];
}
}
return id;
}
};
void solve() {
int n, m;
cin >> n >> m;
vl cost(n), val(n), ind(n), order;
for (int i = 0; i < n; i++) cin >> val[i] >> cost[i], order.pb(val[i]);
iota(all(ind), 0);
sort(all(ind), [&](int i, int j) { return cost[i] < cost[j]; });
sort(all(order));
order.resize(unique(all(order)) - begin(order));
int k = sz(order);
BIT<ll> bit(k), ex(k);
for (int i = 0; i < n; i++) {
val[i] = lb(all(order), val[i]) - begin(order);
val[i] = k - 1 - val[i];
}
int lef = 0, rig = -1;
auto upd = [&](int l, int r) -> void {
while (rig < r) {
rig++;
int id = val[ind[rig]];
bit.upd(id, 1);
ex.upd(id, 1LL * order[k - 1 - id]);
}
while (lef > l) {
lef--;
int id = val[ind[lef]];
bit.upd(id, 1);
ex.upd(id, 1LL * order[k - 1 - id]);
}
while (rig > r) {
int id = val[ind[rig]];
bit.upd(id, -1);
ex.upd(id, -1LL * order[k - 1 - id]);
rig--;
}
while (lef < l) {
int id = val[ind[lef]];
bit.upd(id, -1);
ex.upd(id, -1LL * order[k - 1 - id]);
lef++;
}
};
auto get_top_m = [&]() -> ll {
int i = bit.lower_bound(m) - 1;
int todo = m - bit.query(i);
return ex.query(i) + 1LL * todo * order[k - 2 - i];
};
ll ans = -LINF;
auto solver = [&](int l, int r, int optl, int optr, auto&& self) -> void {
if (l > r) return;
int mid = (l + r) / 2;
int optm = -1;
ll opt = -LINF;
for (int i = optl; i <= optr; i++) {
if (i < mid + m - 1) continue;
upd(mid, i);
if (ckmax(opt, get_top_m() - 2 * (cost[ind[i]] - cost[ind[mid]]))) {
optm = i;
}
}
ckmax(ans, opt);
if (opt == -LINF) {
self(l, mid - 1, optl, optr, self);
} else {
self(l, mid - 1, optl, optm, self);
self(mid + 1, r, optm, optr, self);
}
};
solver(0, n - 1, 0, n - 1, solver);
cout << ans << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int testcase = 1;
// cin >> testcase;
while (testcase--) {
solve();
}
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * (double)clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp:7: warning: ignoring '#pragma region TEMPLATE' [-Wunknown-pragmas]
7 | #pragma region TEMPLATE
|
cake3.cpp:49: warning: ignoring '#pragma endregion TEMPLATE' [-Wunknown-pragmas]
49 | #pragma endregion TEMPLATE
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |