[P3273 / SCOI2011] 棘手的操作 题解
看一眼标签和题解,啊,全是可删堆
我们考虑用启发式合并和 (multi)set,先用一个并查集维护连通性,接着用一个 set 记录每个块的最大值,用 multiset 记录全局最大值,再用
设
U
将
我们假设
具体来说,假设
cin >> x >> y;
x = getf(x);
y = getf(y);
if (x == y) continue;
if (s[x].size() < s[y].size()) swap(x, y);
if (!s[x].empty()&&gm.find((*s[x].begin()).first + v[x])!=gm.end()) gm.erase(gm.find((*s[x].begin()).first + v[x]));
if (!s[y].empty()&&gm.find((*s[y].begin()).first + v[y])!=gm.end()) gm.erase(gm.find((*s[y].begin()).first + v[y]));
for (auto it : s[y]) {
a[it.second] = it.first + v[y] - v[x];
s[x].insert({a[it.second], it.second});
}
s[y].clear();
f[x] += f[y];
f[y] = x;
if (!s[x].empty()) gm.insert((*s[x].begin()).first + v[x]);
A1
我们先把 set 和 multiset 里的值删除,然后改变
cin >> x >> y;
int rt = getf(x);
if (!s[rt].empty()) gm.erase((*s[rt].begin()).first + v[rt]);
s[rt].erase({a[x], x});
a[x] += y;
s[rt].insert({a[x], x});
if (!s[rt].empty()) gm.insert((*s[rt].begin()).first + v[rt]);
A2
我们只需将这个连通块的最大元素在 multiset 里改过来,在修改
int rt = getf(x);
if (!s[rt].empty()) gm.erase((*s[rt].begin()).first + v[rt]);
v[rt] += y;
if (!s[rt].empty()) gm.insert((*s[rt].begin()).first + v[rt]);
A3
只需修改整体标记。
cin>>y;
u+=y;
F1
由于真实的
cin >> x;
cout << a[x] + v[getf(x)] + u << "\n";
F2
我们寻找根为
cin >> x;
int rt = getf(x);
if (s[rt].empty()) {
cout << v[rt] + u << "\n";
} else {
cout << (*s[rt].begin()).first + v[rt] + u << "\n";
}
F3
如果 multiset 为空,输出
if (gm.empty()) {
cout << u << "\n";
} else {
cout << (*gm.begin()) + u << "\n";
}
简单吧(写了 2h)。