#include "nile.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pl pair<ll, ll>
#define pll pair<ll, pl>
#define ppl pair<pl, pl>
vector<ppl> segTree(4e5); // {{F'L', F'L}, {FL', FL}}
ppl merge(ppl a, ppl b) {
if (a.first.first == 0) return b;
if (b.first.first == 0) return a;
ppl res = {{0, 0}, {0, 0}};
res.first.first = min(min(a.first.second + b.second.first, a.first.first + b.first.first - 2), LLONG_MAX / 4);
res.first.second = min(min(a.first.second + b.second.second, a.first.first + b.first.second - 2), LLONG_MAX / 4);
res.second.first = min(min(a.second.second + b.second.first, a.second.first + b.first.first - 2), LLONG_MAX / 4);
res.second.second = min(min(a.second.second + b.second.second, a.second.first + b.first.second - 2), LLONG_MAX / 4);
return res;
}
void construct(int cur, int left, int right) {
if (left == right) {
segTree[cur] = {{LLONG_MAX / 4, 2}, {2, LLONG_MAX / 4}};
return;
}
int mid = (left + right) / 2;
construct(cur * 2, left, mid);
construct(cur * 2 + 1, mid + 1, right);
segTree[cur] = merge(segTree[cur * 2], segTree[cur * 2 + 1]);
}
ppl query(int cur, int left, int right, int qLeft, int qRight) {
if (qRight < left || qLeft > right) return {{0, 0}, {0, 0}};
if (qLeft <= left && qRight >= right) return segTree[cur];
int mid = (left + right) / 2;
return merge(query(cur * 2, left, mid, qLeft, qRight), query(cur * 2 + 1, mid + 1, right, qLeft, qRight));
}
std::vector<long long> calculate_costs(std::vector<int> W, std::vector<int> A, std::vector<int> B, std::vector<int> E) {
int Q = E.size();
std::vector<long long> R(Q, 0);
int n = W.size();
vector<ll> weights;
for (auto &i : W) weights.push_back(i);
sort(weights.begin(), weights.end());
// vector<pair<ll, pair<ll, ll>>> items(n);
// for (int i = 0; i < n; ++i) items[i] = {W[i], {A[i], B[i]}};
// sort(items.begin(), items.end());
// vector<ll> benefit(n);
// for (int i = 0; i < n; ++i) benefit[i] = items[i].second.first - items[i].second.second;
map<int, int> sections;
sections[0] = n - 1;
vector<pl> edges;
for (int i = 0; i < n - 1; ++i) edges.push_back({weights[i + 1] - weights[i], (ll)(i)});
sort(edges.begin(), edges.end());
vector<pl> queries(Q);
for (int i = 0; i < Q; ++i) queries[i] = {E[i], i};
sort(queries.rbegin(), queries.rend());
construct(1, 1, n);
auto preQ = query(1, 1, n, 1, n);
ll curRes = min(min(preQ.first.first, preQ.first.second), min(preQ.second.first, preQ.second.second));
for (auto &[d, idx] : queries) {
while (!edges.empty()) {
auto [len, pos] = edges.back();
if (len <= d) break;
edges.pop_back();
auto section = prev(sections.upper_bound(pos));
int left = section->first, right = section->second;
sections.erase(section);
sections[left] = pos;
sections[pos + 1] = right;
auto curQ = query(1, 1, n, left + 1, right + 1);
curRes -= min(min(curQ.first.first, curQ.first.second), min(curQ.second.first, curQ.second.second));
curQ = query(1, 1, n, left + 1, pos + 1);
curRes += min(min(curQ.first.first, curQ.first.second), min(curQ.second.first, curQ.second.second));
curQ = query(1, 1, n, pos + 2, right + 1);
curRes += min(min(curQ.first.first, curQ.first.second), min(curQ.second.first, curQ.second.second));
}
R[idx] = curRes;
}
return R;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |