Language: C++

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
const int maxn = 100010;

struct edge {
	int u,c,l;
};

int n,q;
int colour[maxn],depth[maxn],dist[maxn];
vector <pi> colours[maxn];
vector <int> pp[maxn];
vector <edge> adjlist[maxn];
int parent[maxn][18];

int weight[maxn],heavy[maxn],head[maxn],pos[maxn];
int edgew[maxn];

int dfs(int x, int p) {
	parent[x][0] = p;
	int max_child = -1;
	int size = 1;
	
	for (auto cur: adjlist[x]) {
		if (cur.u == p) continue;
		depth[cur.u] = depth[x] + 1;
		weight[cur.u] = cur.l;
		dist[cur.u] = dist[x] + cur.l;
		colour[cur.u] = cur.c;
		
		int child_size = dfs(cur.u,x);
		size += child_size;
		if (child_size > max_child) {
			max_child = child_size;
			heavy[x] = cur.u;
		}
	}
	return size;
}

int co = 0;
void decompose(int x, int h) {
	head[x] = h; pos[x] = co++;
	//cout << x << " " << h << " " << colour[x] << " " << pos[x] << "\n";
	if (x != 1) {
		edgew[pos[x]] = weight[x];
		colours[colour[x]].push_back(pi(pos[x],0));
	}
	
	if (heavy[x] != -1) decompose(heavy[x],h);
	for (auto cur: adjlist[x]) {
		if (cur.u == parent[x][0] or cur.u == heavy[x]) continue;
		decompose(cur.u,cur.u);
	}
}

int query(int x, int y, int c,int d) {
	//cout << "NQ: " << x << " " << y << " " << c << " " << d << "\n";
	if (colours[c].size() == 0) return 0;
	int l = upper_bound(all(colours[c]),pi(y,LLONG_MAX/2)) - colours[c].begin();
	int f = lower_bound(all(colours[c]),pi(x,-1)) - colours[c].begin();
	int og = 0;
	if (l != 0) og = colours[c][l-1].s;
	if (f != 0) og -= colours[c][f-1].s;
	//cout << "Q: " << l << " " << f << " " << og << "\n";
	return (l - f) * d - og;
}

int realq(int a, int b, int c, int d) {
	int res = 0;
	while (head[a] != head[b]) {
		if (depth[head[a]] > depth[head[b]]) swap(a,b);
		//cout << a << " " << b << " " << res << "\n";
		res += query(pos[head[b]],pos[b],c,d);
		b = parent[head[b]][0];
	}
	if (depth[a] > depth[b]) swap(a,b);
	if (a == b) return res;
	res += query(pos[a] + 1, pos[b],c,d);
	//cout << a << " " << b << " " << res << "\n";
	return res;
}

int findp(int x, int k) {
	for (int i = 17; i >= 0; i--) {
		if (k >= (1 << i)) {
			if (x == -1) return -1;
			x = parent[x][i];
			k -= (1 << i);
		}
	}
	return x;
}

int lca(int x, int y) {
	if (depth[x] < depth[y]) swap(x,y); 
	int diff_depth = depth[x] - depth[y];
	x = findp(x,diff_depth);
	
	if (x == y) return x;
	for (int k=17;k >=0; k--) { 
		if (parent[x][k] != parent[y][k]) {
			x = parent[x][k];
			y = parent[y][k];
		}
	}
	return parent[x][0];
	
}


int findd(int a, int b) {
	//cout << dist[a] << " " << dist[b] << " " << lca(a,b) << " " << dist[lca(a,b)] << "\n";
	return dist[a] + dist[b] - 2*dist[lca(a,b)];
}

int32_t main() {
	FAST
	cin >> n >> q;
	for (int i =0;i<n-1;i++) {
		int a,b,c,d; cin >> a >> b >> c >> d;
		edge bx = {b,c,d};
		edge ax = {a,c,d};
		adjlist[a].push_back(bx);
		adjlist[b].push_back(ax);
	}
	memset(parent,-1,sizeof parent);
	memset(heavy,-1,sizeof heavy);
	//cout << "HI\n";
	dfs(1,-1);
	//cout << "HELLO\n";
	for (int k=1;k<18;k++) {
		for (int i=1;i<=n;i++) {
			if (parent[i][k-1] != -1)
				parent[i][k] = parent[parent[i][k-1]][k-1];
			else parent[i][k] = -1;
		}
	}
	
	decompose(1,1);
	//cout << "HI\n";
	for (int i =1;i<=N;i++) {
		if (colours[i].size() == 0) continue;
		sort(colours[i].begin(),colours[i].end());
		int csum = 0;
		for (int j=0;j<colours[i].size();j++) {
			csum += edgew[colours[i][j].f];
			colours[i][j].s = csum;
		}
	}
	
	
	//for (int i =1;i<=n;i++) cout << i << " " << pos[i] << "\n";
	//cout << "\n";
	//for (auto i: col) {
		//cout << i << " " << colours[i].size() << "\n";
		//for (auto j : colours[i]) {
			//cout << "col: " <<  i << " " << j << "\n";
		//}
	//}
	//cout << upper_bound(all(colours[1]),2) - colours[1].begin() << "\n";
		
	for (int i =0;i<q;i++) {
		int a,b,c,d; cin >> c >> d >> a >> b;
		//cout << findd(a,b) << "\n";
		cout << findd(a,b) + realq(a,b,c,d) << "\n";
	}
	
}




Compiler output:
a.cc: In function 'int32_t main()':
a.cc:148:26: error: 'N' was not declared in this scope
  148 |         for (int i =1;i<=N;i++) {
      |                          ^