結果

問題 No.1197 モンスターショー
ユーザー hitonanodehitonanode
提出日時 2020-08-22 17:34:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 12,111 bytes
コンパイル時間 2,792 ms
コンパイル使用メモリ 222,092 KB
実行使用メモリ 36,288 KB
最終ジャッジ日時 2024-10-15 11:22:38
合計ジャッジ時間 8,095 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,496 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
5,248 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template <typename V, typename T> void ndfill(V &x, const T &val) { x = val; }
template <typename V, typename T> void ndfill(vector<V> &vec, const T &val) { for (auto &v : vec) ndfill(v, val); }
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; }
#endif
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl
#else
#define dbg(x)
#endif

struct Empty {
};
using VAL = pair<int, pair<int, lint>>;
using LAZY = Empty;
constexpr LAZY lazy_0 = {};
struct LinkCutTree
{
    struct Node
    {
        Node *l, *r, *p;
        VAL key;
        LAZY lazy;
        bool is_reversed;
        uint32_t sz;
        Node(VAL val) : l(nullptr), r(nullptr), p(nullptr), key(val), lazy(lazy_0), is_reversed(false), sz(1) {}
        bool is_root() const
        {
            return !p || (p->l != this and p->r != this);
        }
        friend ostream &operator<<(ostream &os, const Node &n)
        {
            os << "[";
            if (n.l) os << *(n.l) << ",";
            os << n.key << ",";
            if (n.r) os << *(n.r);
            os << "]";
            return os;
        }
    };
    inline Node *_revise_val(Node *t)
    {
        if (t)
        {
            t->sz = 1;
            t->key.second = make_pair(t->key.first, 0);
            // t->key.second = t->key.first;
            if (t->l)
            {
                t->sz += t->l->sz;
                t->key.second.first += t->l->key.second.first;
                t->key.second.second += t->l->key.second.first + t->l->key.second.second;
                // t->key.second += t->l->key.second;
            }
            if (t->r)
            {
                t->sz += t->r->sz;
                t->key.second.first += t->r->key.second.first;
                t->key.second.second += t->r->key.second.first + t->r->key.second.second;
                // t->key.second += t->r->key.second;
            }
        }
        return t;
    }
    inline void _propagate_lazy(LAZY &a, LAZY b) // 遅延評価伝播
    {
        // a += b;
    }
    inline void _reflect_lazy(Node *a, LAZY b) // 遅延評価反映
    {
        // a->key.first += b;
        // a->key.second += a->sz * b;
        // a->key += b;
    }
    inline void _toggle(Node *t)
    {
        swap(t->l, t->r);
        /* key/lazy modification here */
        t->is_reversed ^= true;
    }

    inline void _resolve_lazy(Node *&t) // 対象の遅延評価を解決
    {
        if (t->is_reversed)
        {
            if (t->l) _toggle(t->l);
            if (t->r) _toggle(t->r);
            t->is_reversed = false;
        }
    }

    void _rot_r(Node *t)
    {
        Node *x = t->p, *y = x->p;
        if ((x->l = t->r)) t->r->p = x;
        t->r = x, x->p = t;
        _revise_val(x), _revise_val(t);
        if ((t->p = y))
        {
            if (y->l == x) y->l = t;
            if (y->r == x) y->r = t;
            _revise_val(y);
        }
    }
    void _rot_l(Node *t)
    {
        Node *x = t->p, *y = x->p;
        if ((x->r = t->l)) t->l->p = x;
        t->l = x, x->p = t;
        _revise_val(x), _revise_val(t);
        if ((t->p = y))
        {
            if (y->l == x) y->l = t;
            if (y->r == x) y->r = t;
            _revise_val(y);
        }
    }

    void _splay(Node *t)
    {
        _resolve_lazy(t);
        while (!t->is_root())
        {
            Node *q = t->p;
            if (q->is_root())
            {
                _resolve_lazy(q), _resolve_lazy(t);
                if (q->l == t) _rot_r(t);
                else _rot_l(t);
            }
            else
            {
                Node *r = q->p;
                _resolve_lazy(r), _resolve_lazy(q), _resolve_lazy(t);
                if (r->l == q)
                {
                    if (q->l == t) _rot_r(q), _rot_r(t);
                    else _rot_l(t), _rot_r(t);
                }
                else
                {
                    if (q->r == t) _rot_l(q), _rot_l(t);
                    else _rot_r(t), _rot_l(t);
                }
            }
        }
    }

    Node *make_node(VAL val) { return new Node(val); }

    Node *expose(Node *t)
    {
        Node *rp = nullptr;
        for (Node *cur = t; cur; cur = cur->p)
        {
            _splay(cur);
            cur->r = rp;
            _revise_val(cur);
            rp = cur;
        }
        _splay(t);
        return rp;
    }
    void link(Node *chi, Node *par)
    {
        expose(chi);
        expose(par);
        chi->p = par;
        par->r = chi;
        _revise_val(par);
    }
    void cut(Node *chi)
    {
        expose(chi);
        Node *par = chi->l;
        chi->l = nullptr;
        _revise_val(chi);
        par->p = nullptr;
    }
    void evert(Node *t)
    {
        expose(t);
        _toggle(t);
        _resolve_lazy(t);
    }
    Node *lca(Node *u, Node *v)
    {
        expose(u);
        return expose(v);
    }
    void add_lazy(Node *t, const LAZY &x)
    {
        expose(t);
        _propagate_lazy(t->lazy, x);
        _resolve_lazy(t);
    }
};

vector<int> depth, depthinv;
vector<int> vs, vsinv, par;
vector<vector<int>> to;
vector<int> sz;

void dfs_lca(int now, int prv, int d)
{
    par[now] = prv;
    depth[now] = d;
    vs[now] = vsinv.size();
    vsinv.emplace_back(now);
    depthinv.emplace_back(d);
    for (auto nxt : to[now]) if (nxt != prv)
    {
        dfs_lca(nxt, now, d + 1);
        sz[now] += sz[nxt];
        vsinv.emplace_back(now);
        depthinv.emplace_back(d);
    }
}
// Static sequence sparse table
// Complexity: O(NlogN) for precalculation, O(1) per query
template <typename T>
struct SparseTable {
    int N, lgN;
    T defaultT;
    std::vector<std::vector<T>> data;
    std::vector<int> lgx_table;
    SparseTable(const std::vector<T> &sequence, T defaultT) : N(sequence.size()), defaultT(defaultT)
    {
        lgx_table.resize(N + 1);
        for (int i = 2; i < N + 1; i++) lgx_table[i] = lgx_table[i >> 1] + 1;
        lgN = lgx_table[N] + 1;
        data.assign(lgN, std::vector<T>(N, defaultT));
        data[0] = sequence;
        for (int d = 1; d < lgN; d++) {
            for (int i = 0; i + (1 << d) <= N; i++) {
                data[d][i] = min(data[d - 1][i], data[d - 1][i + (1 << (d - 1))]);
            }
        }
    }
    T get(int l, int r) { // [l, r), 0-indexed
        assert(l >= 0 and r <= N);
        if (l >= r) swap(l, r);
        int d = lgx_table[r - l];
        return min(data[d][l], data[d][r - (1 << d)]);
    }
};

int main()
{
    int N, K, Q;
    cin >> N >> K >> Q;
    vector<int> C(K);
    cin >> C;
    for (auto &a : C) a--;

    vector<int> cnt(N);
    for (auto a : C) cnt[a]++;

    to.resize(N);
    REP(_, N - 1)
    {
        int a, b;
        cin >> a >> b;
        a--, b--;
        to[a].emplace_back(b);
        to[b].emplace_back(a);
    }

    depth.assign(N, 0);
    depthinv.clear();
    vs.assign(N, -1);
    vsinv.clear();
    par.assign(N, -1);
    sz.assign(N, 1);
    dfs_lca(0, -1, 0);
    dbg(depth);
    dbg(depthinv);
    dbg(vsinv);
    SparseTable<int> rmq(depthinv, 1e9);

    vector<pint> upd_info;

    auto distance = [&](int i, int j) {
        i = vsinv[i];
        j = vsinv[j];
        return depthinv[i] + depthinv[j] - depthinv[rmq.get(i, j)] * 2;
    };

    vector<lint> tot(N), totch(N);
    vector<int> ch(N);

    auto dfs2 = [&](auto &&dfs2, int now, int prv) -> void {
        for (auto nxt : to[now]) if (nxt != prv)
        {
            dfs2(dfs2, nxt, now);
            ch[now] += ch[nxt];
            totch[now] += totch[nxt] + ch[nxt];
        }
    };

    auto dfs3 = [&](auto &&dfs3, int now, int prv) -> void {
        tot[now] = totch[now];
        if (prv != -1)
        {
            tot[now] = tot[prv] - ch[now] + (K - ch[now]);
        }
        for (auto nxt : to[now]) if (nxt != prv)
        {
            dfs3(dfs3, nxt, now);
        }
    };

    auto refresh = [&]() {
        totch.assign(N, 0);
        tot.assign(N, 0);
        ch = cnt;
        dfs2(dfs2, 0, -1);
        dfs3(dfs3, 0, -1);
    };

    refresh();
    dbg(cnt);
    dbg(totch);
    dbg(tot);
    REP(t, Q)
    {
        int q;
        cin >> q;
        if (q == 1)
        {
            int p, d;
            cin >> p >> d;
            p--, d--;

            upd_info.emplace_back(C[p], -1);
            C[p] = d;
            cnt[C[p]]++;
            upd_info.emplace_back(C[p], 1);
        }

        if (upd_info.size() > 500)
        {
            refresh();
        }

        if (q == 2)
        {
            int e;
            cin >> e;
            e--;
            lint ans = tot[e];
            for (auto [i, w] : upd_info)
            {
                ans += w * distance(i, e);
            }
            cout << ans << '\n';
        }
    }
}
0