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){
		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){}
};
vector<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){}
};
vector<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--;
		g.addEdge(a, b, d);
		es[a].emplace_back(b, d, c);
		es[b].emplace_back(a, d, c);
	}
	g.init(0);
	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 = getLca(u, v);
		ans[i] = g.depco[u] + g.depco[v] - 2*g.depco[c];
		qs[a].emplace_back(x, i, y, 1);
		qs[b].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: In function 'int main()':
a.cc:140:25: error: 'getLca' was not declared in this scope; did you mean 'getwc'?
  140 |                 int c = getLca(u, v);
      |                         ^~~~~~
      |                         getwc
a.cc:142:20: error: 'a' was not declared in this scope
  142 |                 qs[a].emplace_back(x, i, y, 1);
      |                    ^
a.cc:143:20: error: 'b' was not declared in this scope
  143 |                 qs[b].emplace_back(x, i, y, 1);
      |                    ^