Language: C++

#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
#define lowbit(i) (i & -i)
using namespace std;
const int maxn = 100001;
#define int long long
typedef long long ll;
void operator += (pair < int, int > &a, pair < int, int > b){
	a.first += b.first;
	a.second += b.second;
}
void operator += (pair < int, int > &a, pair < int, int > &b){
	a.first += b.first;
	a.second += b.second;
}
int n, g[maxn], id[maxn], depth[maxn], top[maxn], up[maxn], sz[maxn];
struct info{
	int to = 0, w = 0, c = 0;
}p[maxn];
vector < info > edge[maxn];
int LCA(int a, int b){
	while(g[a] != g[b]){
		if(depth[top[g[a]]] > depth[top[g[b]]])a = up[top[g[a]]];
		else b = up[top[g[b]]];
	}
	return depth[a] < depth[b] ? a : b;
}
struct BIT{
	ll oval[maxn] = {};
	unordered_map < int, pair < ll, int > > val[maxn];
	void add(int i, int v, int c = 0){
		for(;i <= n;i += lowbit(i))
			if(c)val[i][c].first += v, ++val[i][c].second;
			else oval[i] += v;
	}
	ll query(int i){
	    ll res = 0;
		for(;i;i ^= lowbit(i))res += oval[i];
		return res;
	}
	pair < ll, int > query(int i, int c){
		pair < ll, int > res = {0, 0};
		for(;i;i ^= lowbit(i)){
			auto it = val[i].find(c);
			if(it != val[i].end())
				res += it->second;
		}
		return res;
	}
	pair < ll, int > query(int l, int r, int c){
		if(l > r)swap(l, r);
		if(!c)return {query(r) - query(l - 1), 0};
		pair < ll, int > a = query(r, c), b = query(l - 1, c);
		a.first -= b.first;
		a.second -= b.second;
		return a;
	}
}fwt;
void dfs(int now, int last = 0){
	static int d = 0;
	depth[now] = ++d;
	sz[now] = 1;
	p[now].to = last;
	if(last){
		for(int i = 0;i < edge[now].size();++i)
			if(edge[now][i].to == last)swap(edge[now][i], edge[now].back());
		edge[now].pop_back();
	}
	for(int i = 0;i < edge[now].size();++i){
		p[edge[now][i].to] = edge[now][i];
		dfs(edge[now][i].to, now);
		sz[now] += sz[edge[now][i].to];
		if(sz[edge[now][i].to] > sz[edge[now][0].to])swap(edge[now][i], edge[now][0]);
	}
	--d;
}
void dfs2(int now){
	static int x = 0;
	static int gr = 0;
	id[now] = ++x;
	if(x != 1){
		fwt.add(x, p[now].w);
		fwt.add(x, p[now].w, p[now].c);
	}
	g[now] = gr;
	if(!top[gr])top[gr] = now, up[gr] = p[now].to;
	if(!edge[now].empty()){
		dfs2(edge[now][0].to);
		for(int i = 1;i < edge[now].size();++i)
			++gr, dfs2(edge[now][i].to);
	}
}
ll get_weight(int i){
	ll res = 0;
	for(;i;i = up[g[i]]){
		res += fwt.query(id[i], id[top[g[i]]], 0).first;
	}
	return res;
}
pair < ll, int > get_color(int i, int c){
	pair < ll, int > res = {0, 0};
	for(;i;i = up[g[i]])
		res += fwt.query(id[i], id[top[g[i]]], c);
	return res;
}
signed main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	int q;
	cin >> n >> q;
	for(int a, b, c, w, i = 1;i < n;++i){
		cin >> a >> b >> c >> w;
		edge[a].push_back({b, w, c});
		edge[b].push_back({a, w, c});
	}
	dfs(1);
	dfs2(1);
	pair < ll, int > a, b, c;
	for(int x, y, u, v, lca, res;q--;){
		cin >> x >> y >> u >> v;
		lca = LCA(u, v);
		a = get_color(u, x), b = get_color(v, x), c = get_color(lca, x);
		cout << get_weight(u) + get_weight(v) - (get_weight(lca) << 1) - a.first + b.first - (c.first << 1) + (a.second + b.second - (c.second << 1)) * (ll)y << '\n';
		// res = get_weight(u) + get_weight(v) - (get_weight(lca) << 1);
		// res -= a.first + b.first - (c.first << 1);
		// res += (a.second + b.second - (c.second << 1)) * y;
		// cout << res << '\n';
	}

}


Compiler output:
a.cc: In member function 'std::pair<long long int, long long int> BIT::query(long long int, long long int)':
a.cc:48:37: error: ambiguous overload for 'operator+=' (operand types are 'std::pair<long long int, long long int>' and 'std::pair<long long int, long long int>')
   48 |                                 res += it->second;
      |                                 ~~~~^~~~~~~~~~~~~
a.cc:10:6: note: candidate: 'void operator+=(std::pair<long long int, long long int>&, std::pair<long long int, long long int>)'
   10 | void operator += (pair < int, int > &a, pair < int, int > b){
      |      ^~~~~~~~
a.cc:14:6: note: candidate: 'void operator+=(std::pair<long long int, long long int>&, std::pair<long long int, long long int>&)'
   14 | void operator += (pair < int, int > &a, pair < int, int > &b){
      |      ^~~~~~~~