結果

問題 No.386 貪欲な領主
ユーザー minatominato
提出日時 2020-04-09 08:30:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 5,395 bytes
コンパイル時間 2,560 ms
コンパイル使用メモリ 187,692 KB
実行使用メモリ 18,208 KB
最終ジャッジ日時 2023-09-27 12:22:45
合計ジャッジ時間 4,951 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 110 ms
18,052 KB
testcase_05 AC 129 ms
13,572 KB
testcase_06 AC 127 ms
13,264 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 17 ms
4,384 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 4 ms
4,384 KB
testcase_14 AC 129 ms
13,292 KB
testcase_15 AC 94 ms
18,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define ln '\n'
constexpr long long MOD = 1000000007LL;
//constexpr long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull; 
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true;} return false; }
template<class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true;} return false; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


struct HeavyLightDecomposition {
    vector<vector<int>> G;
    vector<int> vid, head, sub, par, dep, inv, type;

    HeavyLightDecomposition(int N) :
        G(N),vid(N,-1),head(N),sub(N,1),
        par(N,-1),dep(N,0),inv(N),type(N){}

    void add_edge(int u,int v) {
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    void build(vector<int> rs={0}) {
        int c=0,pos=0;
        for(int r:rs) {
            dfs_sz(r);
            head[r]=r;
            dfs_hld(r,c++,pos);
        }
    }

    void dfs_sz(int v) {
        for(int &u:G[v]) if(u==par[v]) swap(u,G[v].back());
        if(~par[v]) G[v].pop_back();
        for(int &u:G[v]) {
            par[u]=v;
            dep[u]=dep[v]+1;
            dfs_sz(u);
            sub[v]+=sub[u];
            if(sub[u]>sub[G[v][0]]) swap(u,G[v][0]);
        }
    }

    void dfs_hld(int v,int c,int &pos) {
        vid[v]=pos++;
        inv[vid[v]]=v;
        type[v]=c;
        for(int u:G[v]){
            if(u==par[v]) continue;
            head[u]=(u==G[v][0]?head[v]:u);
            dfs_hld(u,c,pos);
        }
    }

    int lca(int u,int v){
        while(1){
            if(vid[u]>vid[v]) swap(u,v);
            if(head[u]==head[v]) return u;
            v=par[head[v]];
        }
    }

    int distance(int u,int v){
        return dep[u]+dep[v]-2*dep[lca(u,v)];
    }

    // for_each(vertex)
    template<typename F>
    void for_each(int u, int v, const F& f) {
        while(1){
            if(vid[u]>vid[v]) swap(u,v);
            f(max(vid[head[v]],vid[u]),vid[v]+1);
            if(head[u]!=head[v]) v=par[head[v]];
            else break;
        }
    }

    template<typename T,typename Q,typename F>
    T for_each(int u,int v,T ti,const Q &q,const F &f){
        T l=ti,r=ti;
        while(1){
            if(vid[u]>vid[v]){
                swap(u,v);
                swap(l,r);
            }
            l=f(l,q(max(vid[head[v]],vid[u]),vid[v]+1));
            if(head[u]!=head[v]) v=par[head[v]];
            else break;
        }
        return f(l,r);
    }

    // for_each(edge)
    // [l, r) <- attention!!
    template<typename F>
    void for_each_edge(int u, int v,const F& f) {
        while(1){
            if(vid[u]>vid[v]) swap(u,v);
            if(head[u]!=head[v]){
                f(vid[head[v]],vid[v]+1);
                v=par[head[v]];
            }else{
                if(u!=v) f(vid[u]+1,vid[v]+1);
                break;
            }
        }
    }

    int search(int v, int d) {
        if (dep[v] < d || d < 0) return -1;
        while (1) {
            if (dep[v] - dep[head[v]] + 1 <= d) {
                d -= dep[v] - dep[head[v]] + 1;
                v = par[head[v]];
            } else {
                return inv[vid[v]-d];
            }
        }
    }
};

template<typename T, typename Monoid>
struct SegmentTree {
    T unity;
    Monoid merge;
    int N;
    vector<T> node;

    SegmentTree(Monoid f, T id) : merge(f), unity(id) {}

    void init(int sz) {
        N = 1;
        while (N < sz) N *= 2;
        node.assign(2*N,unity);
    }

    void set(int k, const T &val) {node[k+N] = val;}

    void build() {
        for (int i = N-1; i > 0; --i) {
            node[i] = merge(node[i<<1|0], node[i<<1|1]);
        }
    }

    // (0-indexed)
    void update(int x, T val) {
        x += N;
        node[x] = val;
        while (x > 1) {
            x >>= 1;
            node[x] = merge(node[x<<1|0], node[x<<1|1]);
        }
    }

    // [l, r) (0-indexed)
    T get(int l, int r) {
        if (l >= r) return unity;
        T resl = unity, resr = unity;
        l += N; r += N;
        while (l < r) {
            if (l & 1) resl = merge(resl, node[l++]);
            l >>= 1;
            if (r & 1) resr = merge(node[--r], resr);
            r >>= 1;
        }
        return merge(resl, resr);
    }

    T operator[](int x) {return node[x+N];}
};

/*
example
auto f=[](int a,int b){return max(a,b);};
const int id = -1e9;
SegmentTree<int, decltype(f)> seg(f,id);
*/

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N; cin >> N;
    HeavyLightDecomposition HLD(N);
    rep(i,N-1) {
        int u,v; cin >> u >> v;
        HLD.add_edge(u,v);
    }
    HLD.build();

    auto f=[](ll a, ll b) {return a+b;};
    const ll id = 0;
    SegmentTree<ll, decltype(f)> seg(f,id);
    seg.init(N);
    rep(i,N) {
        int c; cin >> c;
        seg.set(HLD.vid[i],c);
    }
    seg.build();

    int M; cin >> M;
    ll ans = 0;
    rep(i,M) {
        int a,b,c; cin >> a >> b >> c;
        auto g=[&] (int u, int v) {ans+=seg.get(u,v)*c;};
        HLD.for_each<decltype(g)>(a,b,g);
    }

    cout << ans << ln;
}
0