Language: C++

struct node {
    int to;
    int col;
    int dis;
};

struct query {
    int id;
    int col;
    int dis;
    int coef;
};

const int MAX_N = 100000;
const int MAX_LOGN = 20;
const int MAX_Q = 100000;
const int MAX_C = 100000 - 1;

vector<vector<node>> G(MAX_N);
vector<vector<query>> qs(MAX_N);

vector<vector<int>> par(MAX_N, vector<int>(MAX_LOGN, -1));
vector<int> dep(MAX_N, 0);
vector<int> dis(MAX_N, 0);
vector<int> cnt(MAX_C, 0);
vector<int> sum(MAX_C, 0);

vector<int> res(MAX_Q, 0);

void init(int u, int p = -1, int d = 0) {
    if (p >= 0) {
        par[u][0] = p;
        dep[u] = dep[p] + 1;
        dis[u] = dis[p] + d;
    }
    for (auto g : G[u]) {
        int v = g.to;
        if (v != p) {
            init(v, u, g.dis);
        }
    }
}
void parent(int n) {
    for (int i = 0; i < MAX_LOGN; i++) {
        for (int j = 0; i < n; i++) {
            if (par[j][i] != -1) {
                par[j][i + 1] = par[par[j][i]][i];
            }
        }
    }
}
int lca(int u, int v) {
    if (dep[u] > dep[v]) {
        swap(u, v);
    }
    for (int i = MAX_LOGN - 1; i >= 0; i--) {
        if (((dep[v] - dep[u]) >> i) & 1) {
            v = par[v][i];
        }
    }
    if (u == v) {
        return u;
    }
    for (int i = MAX_LOGN - 1; i >= 0; i--) {
        if (par[u][i] != par[v][i]) {
            u = par[u][i];
            v = par[v][i];
        }
    }
    return par[u][0];
}
void dfs(int u, int p = -1) {
    for (auto q : qs[u]) {
        int diff = q.dis * cnt[q.col] - sum[q.col];
        res[q.id] += diff * q.coef;
    }
    for (auto g : G[u]) {
        int v = g.to;
        if (v == p) {
            continue;
        }
        cnt[g.col]++;
        sum[g.col] += g.dis;
        dfs(v, u);
        cnt[g.col]--;
        sum[g.col] -= g.dis;
    }
}

int main() {
    int N, Q;
    cin >> N >> Q;
    for (int i = 0; i < N - 1; i++) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        a--, b--;
        G[a].push_back({b, c, d});
        G[b].push_back({a, c, d});
    }
    init(0);
    parent(N);
    for (int i = 0; i < Q; i++) {
        int x, y, u, v;
        cin >> x >> y >> u >> v;
        u--, v--;
        int w = lca(u, v);
        qs[u].push_back({i, x, y, 1});
        qs[v].push_back({i, x, y, 1});
        qs[w].push_back({i, x, y, -2});
        res[i] = dis[u] + dis[v] - 2 * dis[w];
    }
    dfs(0);
    for (int i = 0; i < Q; i++) {
        cout << res[i] << endl;
    }
    return 0;

Compiler output:
a.cc:19:1: error: 'vector' does not name a type
   19 | vector<vector<node>> G(MAX_N);
      | ^~~~~~
a.cc:20:1: error: 'vector' does not name a type
   20 | vector<vector<query>> qs(MAX_N);
      | ^~~~~~
a.cc:22:1: error: 'vector' does not name a type
   22 | vector<vector<int>> par(MAX_N, vector<int>(MAX_LOGN, -1));
      | ^~~~~~
a.cc:23:1: error: 'vector' does not name a type
   23 | vector<int> dep(MAX_N, 0);
      | ^~~~~~
a.cc:24:1: error: 'vector' does not name a type
   24 | vector<int> dis(MAX_N, 0);
      | ^~~~~~
a.cc:25:1: error: 'vector' does not name a type
   25 | vector<int> cnt(MAX_C, 0);
      | ^~~~~~
a.cc:26:1: error: 'vector' does not name a type
   26 | vector<int> sum(MAX_C, 0);
      | ^~~~~~
a.cc:28:1: error: 'vector' does not name a type
   28 | vector<int> res(MAX_Q, 0);
      | ^~~~~~
a.cc: In function 'void init(int, int, int)':
a.cc:32:9: error: 'par' was not declared in this scope
   32 |         par[u][0] = p;
      |         ^~~
a.cc:33:9: error: 'dep' was not declared in this scope
   33 |         dep[u] = dep[p] + 1;
      |         ^~~
a.cc:34:9: error: 'dis' was not declared in this scope
   34 |         dis[u] = dis[p] + d;
      |         ^~~
a.cc:36:19: error: 'G' was not declared in this scope
   36 |     for (auto g : G[u]) {
      |                   ^
a.cc: In function 'void parent(int)':
a.cc:46:17: error: 'par' was not declared in this scope
   46 |             if (par[j][i] != -1) {
      |                 ^~~
a.cc: In function 'int lca(int, int)':
a.cc:53:9: error: 'dep' was not declared in this scope
   53 |     if (dep[u] > dep[v]) {
      |         ^~~
a.cc:54:9: error: 'swap' was not declared in this scope
   54 |         swap(u, v);
      |         ^~~~
a.cc:57:15: error: 'dep' was not declared in this scope
   57 |         if (((dep[v] - dep[u]) >> i) & 1) {
      |               ^~~
a.cc:58:17: error: 'par' was not declared in this scope
   58 |             v = par[v][i];
      |                 ^~~
a.cc:65:13: error: 'par' was not declared in this scope
   65 |         if (par[u][i] != par[v][i]) {
      |             ^~~
a.cc:70:12: error: 'par' was not declared in this scope
   70 |     return par[u][0];
      |            ^~~
a.cc: In function 'void dfs(int, int)':
a.cc:73:19: error: 'qs' was not declared in this scope; did you mean 'q'?
   73 |     for (auto q : qs[u]) {
      |                   ^~
      |                   q
a.cc:74:28: error: 'cnt' was not declared in this scope; did you mean 'int'?
   74 |         int diff = q.dis * cnt[q.col] - sum[q.col];
      |                            ^~~
      |                            int
a.cc:74:41: error: 'sum' was not declared in this scope
   74 |         int diff = q.dis * cnt[q.col] - sum[q.col];
      |                                         ^~~
a.cc:75:9: error: 'res' was not declared in this scope
   75 |         res[q.id] += diff * q.coef;
      |         ^~~
a.cc:77:19: error: 'G' was not declared in this scope
   77 |     for (auto g : G[u]) {
      |                   ^
a.cc:82:9: error: 'cnt' was not declared in this scope; did you mean 'int'?
   82 |         cnt[g.col]++;
      |         ^~~
      |         int
a.cc:83:9: error: 'sum' was not declared in this scope
   83 |         sum[g.col] += g.dis;
      |         ^~~
a.cc: In function 'int main()':
a.cc:92:5: error: 'cin' was not declared in this scope
   92 |     cin >> N >> Q;
      |     ^~~
a.cc:97:9: error: 'G' was not declared in this scope
   97 |         G[a].push_back({b, c, d});
      |         ^
a.cc:107:9: error: 'qs' was not declared in this scope
  107 |         qs[u].push_back({i, x, y, 1});
      |         ^~
a.cc:110:9: error: 'res' was not declared in this scope
  110 |         res[i] = dis[u] + dis[v] - 2 * dis[w];
      |         ^~~
a.cc:110:18: error: 'dis' was not declared in this scope; did you mean 'dfs'?
  110 |         res[i] = dis[u] + dis[v] - 2 * dis[w];
      |                  ^~~
      |                  dfs
a.cc:114:9: error: 'cout' was not declared in this scope
  114 |         cout << res[i] << endl;
      |         ^~~~
a.cc:114:17: error: 'res' was not declared in this scope
  114 |         cout << res[i] << endl;
      |                 ^~~
a.cc:114:27: error: 'endl' was not declared in this scope
  114 |         cout << res[i] << endl;
      |                           ^~~~
a.cc:116:14: error: expected '}' at end of input
  116 |     return 0;
      |              ^
a.cc:90:12: note: to match this '{'
   90 | int main() {
      |            ^