結果

問題 No.900 aδδitivee
ユーザー tomatoma
提出日時 2019-10-04 21:59:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,710 bytes
コンパイル時間 2,656 ms
コンパイル使用メモリ 194,996 KB
実行使用メモリ 39,680 KB
最終ジャッジ日時 2024-04-14 10:45:21
合計ジャッジ時間 16,954 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(ll (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;

using Graph = vector<vector<ll>>;
using WGraph = vector<vector<pair<ll, ll>>>;

struct HLDecomposition {
    using pii = pair<ll, ll>;
    ll n;
    Graph G;
    vector<ll> vid, inv, par, depth, subsize, head, prev, next, type;

    HLDecomposition(const Graph& G_) :
        n(G_.size()), G(G_),
        vid(n, -1), inv(n), par(n), depth(n), subsize(n, 1),
        head(n), prev(n, -1), next(n, -1), type(n) {}
    void build(vector<ll> roots = { 0 }) {
        ll curtype = 0, pos = 0;
        for (ll root : roots) {
            decide_heavy_edge(root);
            reconstruct(root, curtype++, pos);
        }
    }
    void decide_heavy_edge(ll root) {
        stack<pii> st;
        par[root] = -1, depth[root] = 0;
        st.emplace(root, 0);
        while (!st.empty()) {
            ll now = st.top().first;
            ll& way = st.top().second;
            if (way < G[now].size()) {
                ll child = G[now][way++];
                if (child == par[now])continue;
                par[child] = now;
                depth[child] = depth[now] + 1;
                st.emplace(child, 0);
            }
            else {
                st.pop();
                ll maxsize = 0;
                for (auto child : G[now]) {
                    if (child == par[now])continue;
                    subsize[now] += subsize[child];
                    if (maxsize < subsize[child]) {
                        maxsize = subsize[child];
                        prev[child] = now;
                        next[now] = child;
                    }
                }
            }
        }
    }
    void reconstruct(ll root, ll curtype, ll& pos) {
        stack<ll> st({ root });
        while (!st.empty()) {
            ll start = st.top(); st.pop();
            for (ll v = start; v != -1; v = next[v]) {
                type[v] = curtype;
                vid[v] = pos++;
                inv[vid[v]] = v;
                head[v] = start;
                for (auto child : G[v]) {
                    if (child != par[v] && child != next[v]) {
                        st.push(child);
                    }
                }
            }
        }
    }

    // node query [u, v], f([left, right])
    void foreach_nodes(ll u, ll v, const function<void(ll, ll)>& f) {
        while (true) {
            if (vid[u] > vid[v])swap(u, v);
            f(max(vid[head[v]], vid[u]), vid[v]);
            if (head[u] != head[v])v = par[head[v]];
            else break;
        }
    }

    // edge query[u,v] f([left, right])
    // seg_node[vid[i]] := edge(par[i] -> i)
    void foreach_edges(ll u, ll v, const function<void(ll, ll)>& f) {
        while (true) {
            if (vid[u] > vid[v])swap(u, v);
            if (head[u] != head[v]) {
                f(vid[head[v]], vid[v]);
                v = par[head[v]];
            }
            else {
                if (u != v)f(vid[u] + 1, vid[v]);
                break;
            }
        }
    }
    ll lca(ll u, ll v) {
        while (true) {
            if (vid[u] > vid[v])swap(u, v);
            if (head[u] == head[v])return u;
            v = par[head[v]];
        }
    }
};

template<typename Monoid, typename OperatorMonoid = Monoid>
class LazySegmentTree {
private:
    using F = function<Monoid(Monoid, Monoid)>;
    using G = function<Monoid(Monoid, OperatorMonoid, ll)>;
    using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;

    ll sz; // 対応する配列の幅
    vector<Monoid> data;
    vector<OperatorMonoid> lazy;
    const F f; // 2区間マージ演算(data-data-ボトムアップマージ)
    const G g; // 要素,作用素マージ演算(lazy->data同位置変換時の、(data,lazy,len)の計算)
    const H h; // 作用素マージ演算 (query->lazyトップダウン伝搬時の、(lazy,query_value)の計算)
    const Monoid M1;          // モノイド単位元 (data単位元)
    const OperatorMonoid OM0; // 作用素単位元 (lazy単位元)

    void propagate(ll idx, ll len) {
        // 幅lenのlazy[idx]が存在するとき、値を下に流す
        if (lazy[idx] != OM0) {
            if (idx < sz) {
                lazy[(idx << 1) | 0] = h(lazy[(idx << 1) | 0], lazy[idx]);
                lazy[(idx << 1) | 1] = h(lazy[(idx << 1) | 1], lazy[idx]);
            }
            data[idx] = g(data[idx], lazy[idx], len);
            lazy[idx] = OM0;
        }
    }
    Monoid update_impl(ll a, ll b, const OperatorMonoid& val, ll idx, ll l, ll r) {
        propagate(idx, r - l);
        if (r <= a || b <= l)return data[idx];
        else if (a <= l && r <= b) {
            lazy[idx] = h(lazy[idx], val);
            propagate(idx, r - l);
            return data[idx];
        }
        else return data[idx] = f(
            update_impl(a, b, val, (idx << 1) | 0, l, (l + r) >> 1),
            update_impl(a, b, val, (idx << 1) | 1, (l + r) >> 1, r)
        );
    }
    Monoid query_impl(ll a, ll b, ll idx, ll l, ll r) {
        propagate(idx, r - l);
        if (r <= a || b <= l)return M1;
        else if (a <= l && r <= b)return data[idx];
        else return f(
            query_impl(a, b, (idx << 1) | 0, l, (l + r) >> 1),
            query_impl(a, b, (idx << 1) | 1, (l + r) >> 1, r)
        );
    }

public:
    // init忘れに注意
    LazySegmentTree(ll n, const F f, const G g, const H h,
        const Monoid& M1, const OperatorMonoid OM0)
        :f(f), g(g), h(h), M1(M1), OM0(OM0) {
        sz = 1;
        while (sz < n)sz <<= 1;
        data.assign(2 * sz, M1);
        lazy.assign(2 * sz, OM0);
    }
    void build(const vector<Monoid>& vals) {
        rep(idx, vals.size())data[idx + sz] = vals[idx];
        for (ll idx = sz - 1; idx > 0; idx--) {
            data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
        }
    }
    Monoid update(ll a, ll b, const OperatorMonoid& val) {
        return update_impl(a, b, val, 1, 0, sz);
    }
    Monoid query(ll a, ll b) {
        return query_impl(a, b, 1, 0, sz);
    }
    Monoid operator[](const ll& idx) {
        return query(idx, idx + 1);
    }
};

int main()
{
    ll N;
    cin >> N;

    Graph gh(N);
    map<pair<ll, ll>, ll> weight;
    rep(i, N - 1) {
        ll u, v, w;
        cin >> u >> v >> w;
        gh[u].push_back(v);
        gh[v].push_back(u);
        weight[{u, v}] = weight[{v, u}] = w;
    }

    HLDecomposition hld(gh); hld.build();
    vector<ll> weights(N, 0);
    REP(i, 1, N) {
        auto p = make_pair(i, hld.par[i]);
        if (weight.find(p) != weight.end()) {
            weights[hld.vid[i]] = weight[p];
            continue;
        }
        swap(p.first, p.second);
        if (weight.find(p) != weight.end()) {
            weights[hld.vid[i]] = weight[p];
            continue;
        }
        assert(false);
    }
    auto f = [](ll vl, ll vr) {return vl + vr; };
    auto g = [](ll data, ll lazy, int len) {return data + lazy * len; };
    auto h = [](ll lazy, ll query) {return lazy + query; };
    LazySegmentTree<ll> lst(N, f, g, h, 0, 0);
    lst.build(weights);

    ll Q;
    cin >> Q;
    while (Q--) {
        ll type;
        cin >> type;
        if (type == 1) {
            ll a, x;
            cin >> a >> x;
            ll siz = hld.subsize[a];
            a = hld.vid[a];
            lst.update(a + 1, a + siz + 1, x);
        }
        else {
            ll b, res = 0;
            cin >> b;
            hld.foreach_edges(0, b, [&](ll l, ll r) {
                res += lst.query(l, r + 1);
            });
            cout << res << endl;
        }
    }

    return 0;
}
0