ABC170E 题解

对于每个幼儿园维护婴儿 rating 的 multiset,再维护这些幼儿园最大 rating 的 multiset(设其为 \(M\))。

修改时:

  • 如果要修改的婴儿 rating 为原来幼儿园中的最大值,就更新 \(M\)
  • 如果要修改的婴儿 rating 为新的幼儿园中的最大值,也要更新 \(M\)

注意判定 multiset 是否为空。

#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
using i64 = long long;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, q;
    cin >> n >> q;

    vector<int> rating(n), in(n);
    map<int, multiset<int>> children;
    multiset<int> mx;

    for (int i = 0; i < n; ++i) {
        cin >> rating[i] >> in[i];
        children[in[i]].emplace(rating[i]);
    }

    for (const auto& [idx, rate] : children)
        mx.emplace(*rate.rbegin());

    while (q-- > 0) {
        int idx, to;
        cin >> idx >> to;

        int from = in[--idx], rate = rating[idx];
        bool is_max = false;

        if (rate == *children[from].rbegin() && children[from].count(rate) == 1) {
            mx.erase(mx.find(rate));
            is_max = true;
        }

        children[from].erase(children[from].find(rate));

        if (is_max && !children[from].empty())
            mx.emplace(*children[from].rbegin());

        int curr_max = -1;

        if (!children[to].empty())
            curr_max = *children[to].rbegin();

        children[to].emplace(rate);

        if (rate == *children[to].rbegin() && children[to].count(rate) == 1) {
            mx.emplace(rate);

            if (children[to].size() > 1)
                mx.erase(mx.find(curr_max));
        }

        in[idx] = to;
        cout << *mx.begin() << '\n';
    }

    return 0;
}
posted @ 2025-08-02 16:11  David9006  阅读(6)  评论(0)    收藏  举报