Language: C++

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
// #define int ll
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()

template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type
fill_v(T &t, const V &v) { t=v; }
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); }

template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a) {
    out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
    out<<'[';
    for(const T &i: a) out<<i<<',';
    out<<']';
    return out;
}
template<class T>
ostream &operator <<(ostream& out, const set<T>& a) {
    out<<'{';
    for(const T &i: a) out<<i<<',';
    out<<'}';
    return out;
}
template<class T, class S>
ostream &operator <<(ostream& out, const map<T,S>& a) {
    out<<'{';
    for(auto &i: a) out<<i<<',';
    out<<'}';
    return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;

struct HLDecomposition {
    int n, pos;
    vector<vector<ll> g;
    vector<ll> vid,   // HL分解後のグラフでのid
        head,  // 頂点が属するheavy-pathのheadのid
        sub,   // 部分木のサイズ
        hvy,   // heavy-path上での次の頂点のid
        par,   // 親のid
        depth, // 深さ
        inv,   // HL分解前のグラフのid（添え字が分解後のid）
        type,  // 森をHL分解するときの属する木の番号
        ps,    // 行きがけ順
        pt;    // 帰りがけ順

    HLDecomposition(){}
    HLDecomposition(int sz):
        n(sz), pos(0), g(n),
        vid(n,-1), head(n), sub(n,1), hvy(n,-1),
        par(n), depth(n), inv(n), type(n), ps(n), pt(n) {}

    void add_edge(int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u);
    }
    void build(vector<ll> rs=vector<ll>(1,0)) {
        int c=0;
        for(int r: rs) {
            dfs1(r);
            dfs2(r, c++);
        }
    }

    // 根rtからdfsして部分木の大きさ、heavy-edgeの判定などをする
    void dfs1(int rt) {
        stack<PII> st;
        par[rt] = -1;
        depth[rt] = 0;
        st.emplace(rt, 0);
        while(st.size()) {
            int v = st.top().first;
            int &i = st.top().second;
            if(i < (int)g[v].size()) {
                int u = g[v][i++];
                if(u == par[v]) continue;
                par[u] = v;
                depth[u] = depth[v]+1;
                st.emplace(u, 0);
            } else {
                st.pop();
                for(int &u: g[v]){
                    if(u == par[v]) swap(u, g[v].back());
                    if(u == par[v]) continue;
                    sub[v] += sub[u];
                    if(sub[u]>sub[g[v].front()]) swap(u, g[v].front());
                }
            }
        }
    }
    // 根r、c番目の木についてchainについての情報をまとめる
    void dfs2(int r, int c) {
        using T = tuple<int, int, int>;
        stack<T> st;
        st.emplace(r,r,0);
        while(!st.empty()) {
            int v,h;
            tie(v,h,ignore)=st.top();
            int &i=get<2>(st.top());
            if(!i) {
                type[v]=c;
                ps[v]=vid[v]=pos++;
                inv[vid[v]]=v;
                head[v]=h;
                hvy[v]=(g[v].empty()?-1:g[v][0]);
                if(hvy[v]==par[v]) hvy[v]=-1;
            }
            if(i<(int)g[v].size()) {
                int u=g[v][i++];
                if(u==par[v]) continue;
                st.emplace(u,(hvy[v]==u?h:u),0);
            } else {
                st.pop();
                pt[v]=pos;
            }
        }
    }
    // 頂点に対する処理 [u,v] 開区間なので注意!!!
    void for_each(int u, int v, const function<void(int,int)>& f) {
        while(1){
            if(vid[u]>vid[v]) swap(u,v);
            // [max(vid[head[v]],vid[u]), vid[v]] の区間についての操作を行う
            f(max(vid[head[v]], vid[u]), vid[v]);
            if(head[u]!=head[v]) v = par[head[v]];
            else break;
        }
    }
    // 辺に対する処理 [u,v] 開区間なので注意!!!
    void for_each_edge(int u, int v, const function<void(int,int)>& f) {
        while(1) {
            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;
            }
        }
    }
    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 depth[u] + depth[v] - 2*depth[lca(u,v)];
    }
};

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n, q;
    cin >> n >> q;
    vector<vector<ll>> edges(n-1);
    vecotr<ll> a(n-1), b(n-1), c(n-1), d(n-1);
    HLDecomposition hld(n);
    REP(i, n-1) {
        cin >> a[i] >> b[i] >> c[i] >> d[i];
        a[i]--, b[i]--;
        hld.add_edge(a[i], b[i]);
        edges[c[i]].push_back(i);
    }
    vector<ll> x(q), y(q), u(q), v(q);
    vector<vector<ll>> query(n);
    REP(i, q) {
        cin >> x[i] >> y[i] >> u[i] >> v[i];
        x[i]--, u[i]--, v[i]--;
        query[x[i]].push_back(i);
    }

    vector<ll> ans(q);
    REP(i, n) {
        for(auto e: edges[i]) {
            auto f = [&](int a, int b) {
                cnt.update(a, b, 1);
                dist.update(a, b, 0);
            };
            hld.for_each_edge(a[e], b[e], f);
        }

        for(auto j: query) {
            auto f = [&](int a, int b) {
                ans[j] += dist.query(a, b+1) + cnt.query(a, b+1) * y[j];
            };
            hld.for_each_edge(a[e], b[e], f);
        }

        for(auto e: edges[i]) {
            auto f = [&](int a, int b) {
                cnt.update(a, b, 0);
                dist.update(a, b, d[e]);
            };
            hld.for_each_edge(a[e], b[e], f);
        }
    }

    for(auto i: ans) cout << i << endl;

    return 0;
}


Compiler output:
a.cc:60:23: error: template argument 1 is invalid
   60 |     vector<vector<ll> g;
      |                       ^
a.cc:60:23: error: template argument 2 is invalid
a.cc: In constructor 'HLDecomposition::HLDecomposition(int)':
a.cc:74:24: error: class 'HLDecomposition' does not have any field named 'g'
   74 |         n(sz), pos(0), g(n),
      |                        ^
a.cc: In member function 'void HLDecomposition::add_edge(int, int)':
a.cc:79:9: error: 'g' was not declared in this scope
   79 |         g[u].push_back(v);
      |         ^
a.cc: In member function 'void HLDecomposition::dfs1(int)':
a.cc:98:31: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'long long int'
   98 |             int &i = st.top().second;
      |                      ~~~~~~~~~^~~~~~
a.cc:99:25: error: 'g' was not declared in this scope
   99 |             if(i < (int)g[v].size()) {
      |                         ^
a.cc: In member function 'void HLDecomposition::dfs2(int, int)':
a.cc:130:25: error: 'g' was not declared in this scope
  130 |                 hvy[v]=(g[v].empty()?-1:g[v][0]);
      |                         ^
a.cc:133:23: error: 'g' was not declared in this scope
  133 |             if(i<(int)g[v].size()) {
      |                       ^
a.cc: In function 'int main()':
a.cc:186:5: error: 'vecotr' was not declared in this scope
  186 |     vecotr<ll> a(n-1), b(n-1), c(n-1), d(n-1);
      |     ^~~~~~
a.cc:186:14: error: expected primary-expression before '>' token
  186 |     vecotr<ll> a(n-1), b(n-1), c(n-1), d(n-1);
      |              ^
a.cc:186:16: error: 'a' was not declared in this scope
  186 |     vecotr<ll> a(n-1), b(n-1), c(n-1), d(n-1);
      |                ^
a.cc:186:24: error: 'b' was not declared in this scope
  186 |     vecotr<ll> a(n-1), b(n-1), c(n-1), d(n-1);
      |                        ^
a.cc:186:32: error: 'c' was not declared in this scope
  186 |     vecotr<ll> a(n-1), b(n-1), c(n-1), d(n-1);
      |                                ^
a.cc:186:40: error: 'd' was not declared in this scope
  186 |     vecotr<ll> a(n-1), b(n-1), c(n-1), d(n-1);
      |                                        ^
a.cc: In lambda function:
a.cc:206:17: error: 'cnt' was not declared in this scope; did you mean 'int'?
  206 |                 cnt.update(a, b, 1);
      |                 ^~~
      |                 int
a.cc:207:17: error: 'dist' was not declared in this scope
  207 |                 dist.update(a, b, 0);
      |                 ^~~~
a.cc: In lambda function:
a.cc:214:20: error: no match for 'operator[]' (operand types are 'std::vector<long long int>' and 'std::vector<long long int>')
  214 |                 ans[j] += dist.query(a, b+1) + cnt.query(a, b+1) * y[j];
      |                    ^
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1128:7: note: candidate: 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = long long int; _Alloc = std::allocator<long long int>; reference = long long int&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   no known conversion for argument 1 from 'std::vector<long long int>' to 'std::vector<long long int>::size_type' {aka 'long unsigned int'}
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1147:7: note: candidate: 'std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](size_type) const [with _Tp = long long int; _Alloc = std::allocator<long long int>; const_reference = const long long int&; size_type = long unsigned int]'
 1147 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1147:28: note:   no known conversion for argument 1 from 'std::vector<long long int>' to 'std::vector<long long int>::size_type' {aka 'long unsigned int'}
 1147 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc:214:27: error: 'dist' was not declared in this scope
  214 |                 ans[j] += dist.query(a, b+1) + cnt.query(a, b+1) * y[j];
      |                           ^~~~
a.cc:214:48: error: 'cnt' was not declared in this scope; did you mean 'int'?
  214 |                 ans[j] += dist.query(a, b+1) + cnt.query(a, b+1) * y[j];
      |                                                ^~~
      |                                                int
a.cc:214:69: error: no match for 'operator[]' (operand types are 'std::vector<long long int>' and 'std::vector<long long int>')
  214 |                 ans[j] += dist.query(a, b+1) + cnt.query(a, b+1) * y[j];
      |                                                                     ^
/usr/include/c++/14/bits/stl_vector.h:1128:7: note: candidate: 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = long long int; _Alloc = std::allocator<long long int>; reference = long long int&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   no known conversion for argument 1 from 'std::vector<long long int>' to 'std::vector<long long int>::size_type' {aka 'long unsigned int'}
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1147:7: note: candidate: 'std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](size_type) const [with _Tp = long long int; _Alloc = std::allocator<long long int>; const_reference = const long long int&; size_type = long unsigned int]'
 1147 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1147:28: note:   no known conversion for argument 1 from 'std::vector<long long int>' to 'std::vector<long long int>::size_type' {aka 'long unsigned int'}
 1147 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc: In function 'int main()':
a.cc:216:33: error: 'e' was not declared in this scope
  216 |             hld.for_each_edge(a[e], b[e], f);
      |                                 ^
a.cc: In lambda function:
a.cc:221:17: error: 'cnt' was not declared in this scope; did you mean 'int'?
  221 |                 cnt.update(a, b, 0);
      |                 ^~~
      |                 int
a.cc:222:17: error: 'dist' was not declared in this scope
  222 |                 dist.update(a, b, d[e]);
      |                 ^~~~