Language: C++

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

const int MAXN = 1e5;
const int MAXC = 1e4;
struct edge{int from,to,color,length;};
vector<vector<edge>> to(MAXN);
vector<int> G[MAXN];
struct LCA{
    static const int BITLEN_MAX = 30;
    vector<int> parent[BITLEN_MAX];
    vector<int> depth;
    int bitlen;

    void initialize(int N, const vector<int> edges[]){
        int root = 0;
        bitlen = 1;
        while((1<<bitlen) < N) bitlen += 1;
        for(int i=0; i<bitlen; i++) parent[i].resize(N);
        depth.resize(N, -1);

        dfs(root, -1, 0, edges);
        for(int k=0; k<bitlen-1; k++){
            for(int v=0; v<N; v++){
                if(depth[v] == -1) continue;
                if(parent[k][v] < 0){
                    parent[k+1][v] = -1;
                }else{
                    parent[k+1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }

    void dfs(int v, int p, int d, const vector<int> edges[]){
        parent[0][v] = p;
        depth[v] = d;
        for(auto u : edges[v]){
            if(u != p) dfs(u, v, d+1, edges);
        }
    }

    int calc_lca(int u, int v){
        if(depth[u] > depth[v]) swap(u, v);
        for(int k=0; k<bitlen; k++){
            if( ((depth[v]-depth[u]) >> k) & 1 ) v = parent[k][v];
        }
        if(u == v) return u;
        for(int k=bitlen-1; k>=0; k--){
            if(parent[k][u] != parent[k][v]){
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }

    int calc_dist(int u, int v){
        int l = calc_lca(u, v);
        return depth[u] + depth[v] - depth[l]*2;
    }
};

vector<int> dist[MAXN]; // 頂点iへの距離
vector<int> needcol[MAXN]; // 頂点iに必要な色
vector<int> colcnt[MAXC]; // 現在通った辺のうち色iの総和
vector<int> colsum[MAXC]; // 現在通った辺のうち色iの総和
map<int,int> cnt[MAXN], sum[MAXN];
void dfs2(int v, int p){
	
	// 現在の総和を（必要がある場合だけ）memo
	for(auto col : needcol[v]){
		cnt[v][col] = colcnt[col];
		sum[v][col] = colsum[col];
	}
	
	for(auto e : to[v]){
		if(e.to == p) continue;
		dist[e.to] = dist[v] + e.length;
		
		// 進んだ分足す
		colcnt[e.color]++;
		colsum[e.color] += e.length;
		
		dfs2(e.to,v);
		
		// 戻るので引く
		colcnt[e.color]--;
		colsum[e.color] -= e.length;
	}
	return;
}

int main(){
	
	int n,q;
	cin >> n >> q;
	
	rep(i,n-1){
		int a,b,c,d;
		cin >> a >> b >> c >> d;
		--a; --b; --c;
		to[a].push_back(edge{a,b,c,d});
		to[b].push_back(edge{b,a,c,d});
		G[a].push_back(b);
		G[b].push_back(a);
	}
	
	LCA lca;
	lca.initialize(n, G);
	
	int X[100000], Y[100000], U[100000], V[100000], L[100000];
	rep(i,q){
        cin >> X[i] >> Y[i] >> U[i] >> V[i];
        X[i]--; U[i]--; V[i]--;
		L[i] = lca.calc_lca(U[i], V[i]);
        needcol[U[i]].push_back(X[i]);
        needcol[V[i]].push_back(X[i]);
        needcol[L[i]].push_back(X[i]);
	}

	dfs2(0,-1);
	
	
	rep(i,q){
		int x = X[i], y = Y[i], u = U[i], v = V[i], lcauv = L[i];
		
		int nowd = dist[u] + dist[v] - 2*dist[lcauv];
		int nowsum = sum[u][x] + sum[v][x] - 2*sum[lcauv][x];
		int nowcnt = cnt[u][x] + cnt[v][x] - 2*cnt[lcauv][x];
		cout << nowd - nowsum + nowcnt*y << endl;
	}
	return 0;
}

Compiler output:
a.cc: In function 'void dfs2(int, int)':
a.cc:75:41: error: cannot convert 'std::vector<int>' to 'std::map<int, int>::mapped_type' {aka 'int'} in assignment
   75 |                 cnt[v][col] = colcnt[col];
      |                               ~~~~~~~~~~^
      |                                         |
      |                                         std::vector<int>
a.cc:76:41: error: cannot convert 'std::vector<int>' to 'std::map<int, int>::mapped_type' {aka 'int'} in assignment
   76 |                 sum[v][col] = colsum[col];
      |                               ~~~~~~~~~~^
      |                                         |
      |                                         std::vector<int>
a.cc:81:38: error: no match for 'operator+' (operand types are 'std::vector<int>' and 'int')
   81 |                 dist[e.to] = dist[v] + e.length;
      |                              ~~~~~~~ ^ ~~~~~~~~
      |                                    |     |
      |                                    |     int
      |                                    std::vector<int>
In file included from /usr/include/c++/14/bits/stl_algobase.h:67,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator.h:627:5: note: candidate: 'template<class _Iterator> constexpr std::reverse_iterator<_Iterator> std::operator+(typename reverse_iterator<_Iterator>::difference_type, const reverse_iterator<_Iterator>&)'
  627 |     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:627:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   mismatched types 'const std::reverse_iterator<_Iterator>' and 'int'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note: candidate: 'template<class _Iterator> constexpr std::move_iterator<_IteratorL> std::operator+(typename move_iterator<_IteratorL>::difference_type, const move_iterator<_IteratorL>&)'
 1798 |     operator+(typename move_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   mismatched types 'const std::move_iterator<_IteratorL>' and 'int'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
In file included from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/basic_string.h:3598:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3598 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3598:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   'std::vector<int>' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/basic_string.h:3616:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3616 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3616:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   mismatched types 'const _CharT*' and 'std::vector<int>'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/basic_string.h:3635:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3635 |     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3635:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'int'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/basic_string.h:3652:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)'
 3652 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3652:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   'std::vector<int>' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/basic_string.h:3670:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'
 3670 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3670:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   'std::vector<int>' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/basic_string.h:3682:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3682 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3682:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   'std::vector<int>' is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/basic_string.h:3689:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3689 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3689:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   'std::vector<int>' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/basic_string.h:3696:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3696 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3696:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   'std::vector<int>' is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/basic_string.h:3719:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3719 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3719:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   mismatched types 'const _CharT*' and 'std::vector<int>'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/basic_string.h:3726:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3726 |     operator+(_CharT __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3726:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'int'
   81 |                 dist[e.to] = dist[v] + e.length;
      |                                          ^~~~~~
/usr/include/c++/14/bits/basic_string.h:3733:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const _CharT*)'
 3733 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3733:5: note:   template argument deduction/substitution failed:
a.cc:81:42: note:   'std::vector<int>' is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _All