题解:P12459 [JOI2025 预选赛 R2] 亲密的厨师
首先将题意转化一步。设
将所有厨师按
那么我们让厨师
将所有
时空复杂度都是一个
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <set>
using namespace std;
using Pii = pair<int, int>;
const int kN = 4e5 + 5;
const int V = 1e9;
const Pii Empty = {-1, -1};
int n, m, q, rd;
int a[kN], b[kN];
int eu[kN], ev[kN];
void Renumber () {
static int p[kN], to[kN];
iota(p + 1, p + n + 1, 1);
sort(p + 1, p + n + 1, [&](int i, int j) -> bool {
return a[i] < a[j];
});
for (int i = 1; i <= n; ++i) to[p[i]] = i;
static int na[kN], nb[kN];
for (int i = 1; i <= n; ++i) na[to[i]] = a[i], nb[to[i]] = b[i];
for (int i = 1; i <= n; ++i) a[i] = na[i], b[i] = nb[i];
for (int i = 1; i <= m; ++i) {
eu[i] = to[eu[i]], ev[i] = to[ev[i]];
}
}
const int kT = kN * 80;
struct Seg {
int tot;
int lc[kT], rc[kT], cnt[kT];
void Insert (int &k, int t, int x, int L = 1, int R = V) {
k = ++tot;
cnt[k] = cnt[t] + 1;
if (L == R) return;
int mid = (L + R) >> 1;
if (x <= mid) rc[k] = rc[t], Insert(lc[k], lc[t], x, L, mid);
else lc[k] = lc[t], Insert(rc[k], rc[t], x, mid + 1, R);
}
Pii Subtree_end (int k, int L = 1, int R = V) {
if (L == R) return {L, cnt[k]};
int mid = (L + R) >> 1;
if (rc[k] && cnt[rc[k]]) return Subtree_end(rc[k], mid + 1, R);
else return Subtree_end(lc[k], L, mid);
}
Pii Find_pre (int k, int x, int L = 1, int R = V) {
if (!k || L == R) return Empty;
int mid = (L + R) >> 1;
if (x <= mid) return Find_pre(lc[k], x, L, mid);
Pii ret = Find_pre(rc[k], x, mid + 1, R);
if (ret != Empty) return ret;
if (lc[k] && cnt[lc[k]]) return Subtree_end(lc[k], L, mid);
return Empty;
}
} seg;
int rt[kN];
struct Node {
int i, a, b;
Pii p;
Node () {}
Node (int i, int a, int b, Pii p) : i(i), a(a), b(b), p(p) {}
int Val () const {
return a + max(b, p.first);
}
bool operator< (const Node &n) const {
return Val() < n.Val();
}
};
int main () {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i];
for (int i = 1; i <= m; ++i) cin >> eu[i] >> ev[i];
Renumber();
// for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
// cout << '\n';
// for (int i = 1; i <= n; ++i) cout << b[i] << ' ';
// cout << '\n';
// for (int i = 1; i <= m; ++i) cout << "Edge " << eu[i] << ' ' << ev[i] << '\n';
vector<int> qry;
for (int i = 0, x; i < q; ++i) {
cin >> x;
qry.push_back(x);
rd = max(rd, x);
}
vector<int> banned;
for (int i = 1; i <= m; ++i) {
int u = eu[i], v = ev[i];
banned.push_back(max(a[u], a[v]) + max(b[u], b[v]));
}
rd += banned.size();
for (int i = 1; i <= n; ++i) seg.Insert(rt[i], rt[i - 1], b[i]);
priority_queue<Node> q;
for (int i = 1; i <= n; ++i) {
Pii p = seg.Subtree_end(rt[i - 1]);
if (p == Empty) continue;
q.push(Node(i, a[i], b[i], p));
}
multiset<int, greater<int>> st;
int cur = rd;
while (true) {
Node node = q.top();
q.pop();
int ned = min(cur, node.p.second);
for (int i = 1; i <= ned; ++i) st.insert(node.Val());
cur -= ned;
if (!cur) break;
Pii p = seg.Find_pre(rt[node.i - 1], node.p.first);
// cout << node.i << ' ' << node.a << ' ' << node.b << ' ' << node.p.first << ' ' << node.p.second << ' ' << node.Val() << p.first << ' ' << p.second << '\n';
if (p == Empty) continue;
node.p = p;
q.push(node);
}
// for (auto i : st) cout << i << ' ';
// cout << '\n';
for (auto i : banned) {
auto it = st.find(i);
if (it != st.end()) st.erase(it);
}
static int ans[kN];
rd -= banned.size();
for (int i = 1; i <= rd; ++i) {
ans[i] = *st.begin();
st.erase(st.begin());
}
for (auto i : qry) cout << ans[i] << '\n';
return 0;
}