Language: C++

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;


template<typename T>
struct lca{
	int n, l;
	int root;
	vector< vector<int> > to;
	vector< vector<T> > co;
	vector<int> dep;
	vector<T> depco;
	vector< vector<int> > par;
	
	lca(int n) : n(n), to(n), co(n), dep(n+1), depco(n+1){
		l = 0;
		while((1 << l) < n) l++;
		par.resize(n+1, vector<int>(l, n)); 
	}
	
	void addEdge(int a, int b, T c){
		to[a].push_back(b);
		to[b].push_back(a);
		
		co[a].push_back(c);
		co[b].push_back(c);
	}
	
	void dfs(int now, int from = -1){
		if(from == -1){
			from = n;
			dep[now] = 0;
			depco[now] = 0;
		}
		par[now][0] = from;
		for(ll i=0; i<to[now].size(); i++){
			dep[ to[now][i] ] = dep[now] + 1;
			depco[ to[now][i] ] = dep[now] + co[now][i];
			
			dfs(to[now][i], now); 
		}
		
		return;
	}
	
	void init(int _root = 0){
		root = _root;
		dfs(root);
		
		for(int i=0; i<l-1; i++){
			for(int j=0; j<n; j++){
				par[j][i+1] = par[ par[j][i] ][i];
			}
		}
		return;
	}
	
	int getLca(int a, int b){
		if(dep[a] > dep[b]) swap(a, b);
		int gap = dep[b] - dep[a];
		for(int i=l-1; i>=0; i--){
			int len = 1 << i;
			if(len <= gap){
				b = par[b][i];
				gap -= len;
			}
		}
		if(a == b) return a;
		for(int i=l; i>=0; i--){
			if(par[a][i] != par[b][i]){
				a = par[a][i];
				b = par[b][i];
			}
		}
		
		return par[a][0];
	}
	
	int length(int a, int b){
		int c = getLca(a, b);
		return dep[a] + dep[b] - 2*dep[c];
	}
	int dist(int a, int b){
		int c = getLca(a, b);
		return depco[a] + depco[b] - 2*depco[c];
	}
};


const int MX = 100005;


struct Edge{
	int to, co, col;
	Edge(int to, int co, int col):to(to), co(co), col(col){}
};
Edge es[MX];


vector<ll> ans;
struct Querry{
	int col, qid, y, coeff;
	Querry(int col, int qid, int y, int coeff):col(col), qid(qid), y(y), coeff(coeff){}
};
Querry qs[MX];
int cnt[MX];
ll sum[MX];

void dfs2(int v, int p=-1){
	for(auto q : qs[v]){
		ll x = -sum[q.col];
		x += (ll)q.y * cnt[q.col];
		ans[q.qid] += x*q.coeff;
	}
	for(auto e : es[v]){
		if(e.to == p ) continue;
		cnt[e.col]++; sum[e.col] += e.co;
		dfs2(e.to, v);
		cnt[e.col]--; sum[e.col] -= e.co;
	}
}

int n, q;

int main(){
	cin >> n >> q;
	lca<ll> g(n);
	for(int i=0; i<n-1; i++){
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		a--; b--;
		
		es[a] = Edge(b, d, c);
		es[b] = Edge(a, d, c);
		//es[a].emplace_back(b, d, c);
		//es[b].emplace_back(a, d, c);
		g.addEdge(a, b, d);
	}
	g.init();
	ans = vector<ll>(q);
	for(int i=0; i<q; i++){
		int x, y, u, v;
		cin >> x >> y >> u >> v;
		u--; v--;
		int c = g.getLca(u, v);
		ans[i] = g.depco[u] + g.depco[v] - 2*g.depco[c];
		
		qs[u] = Querry(x, i, y, 1);
		qs[v] = Querry(x, i, y, 1);
		qs[c] = Querry(x, i, y, -2);
		
		//qs[u].emplace_back(x, i, y, 1);
		//qs[v].emplace_back(x, i, y, 1);
		//qs[c].emplace_back(x, i, y, -2);
	}
	dfs2(0);
	for(int i=0; i<q; i++){
		cout << ans[i] << endl;
	}
	
	return 0;
}

Compiler output:
a.cc:98:11: error: no matching function for call to 'Edge::Edge()'
   98 | Edge es[MX];
      |           ^
a.cc:96:9: note: candidate: 'Edge::Edge(int, int, int)'
   96 |         Edge(int to, int co, int col):to(to), co(co), col(col){}
      |         ^~~~
a.cc:96:9: note:   candidate expects 3 arguments, 0 provided
a.cc:94:8: note: candidate: 'constexpr Edge::Edge(const Edge&)'
   94 | struct Edge{
      |        ^~~~
a.cc:94:8: note:   candidate expects 1 argument, 0 provided
a.cc:94:8: note: candidate: 'constexpr Edge::Edge(Edge&&)'
a.cc:94:8: note:   candidate expects 1 argument, 0 provided
a.cc:106:13: error: no matching function for call to 'Querry::Querry()'
  106 | Querry qs[MX];
      |             ^
a.cc:104:9: note: candidate: 'Querry::Querry(int, int, int, int)'
  104 |         Querry(int col, int qid, int y, int coeff):col(col), qid(qid), y(y), coeff(coeff){}
      |         ^~~~~~
a.cc:104:9: note:   candidate expects 4 arguments, 0 provided
a.cc:102:8: note: candidate: 'constexpr Querry::Querry(const Querry&)'
  102 | struct Querry{
      |        ^~~~~~
a.cc:102:8: note:   candidate expects 1 argument, 0 provided
a.cc:102:8: note: candidate: 'constexpr Querry::Querry(Querry&&)'
a.cc:102:8: note:   candidate expects 1 argument, 0 provided
a.cc: In function 'void dfs2(int, int)':
a.cc:111:26: error: 'begin' was not declared in this scope
  111 |         for(auto q : qs[v]){
      |                          ^
a.cc:111:26: note: suggested alternatives:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166,
                 from a.cc:1:
/usr/include/c++/14/valarray:1238:5: note:   'std::begin'
 1238 |     begin(const valarray<_Tp>& __va) noexcept
      |     ^~~~~
In file included from /usr/include/c++/14/filesystem:53,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:200:
/usr/include/c++/14/bits/fs_dir.h:608:3: note:   'std::filesystem::__cxx11::begin'
  608 |   begin(recursive_directory_iterator __iter) noexcept
      |   ^~~~~
a.cc:111:26: error: 'end' was not declared in this scope
  111 |         for(auto q : qs[v]){
      |                          ^
a.cc:111:26: note: suggested alternatives:
/usr/include/c++/14/valarray:1265:5: note:   'std::end'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/bits/fs_dir.h:613:3: note:   'std::filesystem::__cxx11::end'
  613 |   end(recursive_directory_iterator) noexcept
      |   ^~~
a.cc:116:26: error: 'begin' was not declared in this scope
  116 |         for(auto e : es[v]){
      |                          ^
a.cc:116:26: note: suggested alternatives:
/usr/include/c++/14/valarray:1238:5: note:   'std::begin'
 1238 |     begin(const valarray<_Tp>& __va) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/fs_dir.h:608:3: note:   'std::filesystem::__cxx11::begin'
  608 |   begin(recursive_directory_iterator __iter) noexcept
      |   ^~~~~
a.cc:116:26: error: 'end' was not declared in this scope
  116 |         for(auto e : es[v]){
      |                          ^
a.cc:116:26: note: suggested alternatives:
/usr/include/c++/14/valarray:1265:5: note:   'std::end'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/bits/fs_dir.h:613:3: note:   'std::filesystem::__cxx11::end'
  613 |   end(recursive_directory_iterator) noexcept
      |   ^~~