Problem p02986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define mp make_pair
int n, q;
struct edge {
  int v, c, w;
  edge(int vv, int cc, int ww) {
    v = vv;
    c = cc;
    w = ww;
  }
};
vector<edge> G[123456];
int cnt[12345], in[123456], out[123456], tim = 0;
int pa[18][123456];
bool anc(int x, int y) { return in[x] <= in[y] && out[x] >= out[y]; }
void dfs(int cur, int pre) {
  in[cur] = ++tim;
  pa[0][cur] = pre;
  for (edge tmp : G[cur]) {
    int nxt = tmp.v, col = tmp.c, w = tmp.w;
    if (nxt == pre)
      continue;
    dfs(nxt, cur);
  }
  out[cur] = ++tim;
}
void build() {
  for (int i = 1; i < 18; i++)
    for (int j = 1; j <= n; j++)
      pa[i][j] = pa[i - 1][pa[i - 1][j]];
}
int lca(int x, int y) {
  if (anc(x, y))
    return x;
  if (anc(y, x))
    return y;
  for (int i = 17; i >= 0; i--)
    if (!anc(pa[i][x], y))
      x = pa[i][x];
  return pa[0][x];
}
int ans[123456], q_col[123456], q_val[123456], dist[123456];
vector<int> query[123456];
void solve(int cur, int pre, int cur_dis) {
  for (int id : query[cur]) {
    int mul = 1;
    if (id < 0)
      mul = -2, id = -id;
    ans[id] += mul * (cur_dis + cnt[q_col[id]] * q_val[id] - dist[q_col[id]]);
  }

  for (edge tmp : G[cur]) {
    int nxt = tmp.v, col = tmp.c, w = tmp.w;
    if (nxt == pre)
      continue;
    cnt[col]++;
    dist[col] += w;
    solve(nxt, cur, cur_dis + w);
    dist[col] -= w;
    cnt[col]--;
  }
}
int32_t main() {
  ios::sync_with_stdio();
  cin.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int u, v, c, w;
    cin >> u >> v >> c >> w;
    G[u].push_back(edge(v, c, w));
    G[v].push_back(edge(u, c, w));
  }
  dfs(1, 1);
  build();
  for (int i = 1; i <= q; i++) {
    int u, v;
    cin >> q_col[i] >> q_val[i] >> u >> v;
    query[u].push_back(i);
    query[v].push_back(i);
    query[lca(u, v)].push_back(-i);
  }
  solve(1, 1, 0);

  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define mp make_pair
int n, q;
struct edge {
  int v, c, w;
  edge(int vv, int cc, int ww) {
    v = vv;
    c = cc;
    w = ww;
  }
};
vector<edge> G[123456];
int cnt[123456], in[123456], out[123456], tim = 0;
int pa[18][123456];
bool anc(int x, int y) { return in[x] <= in[y] && out[x] >= out[y]; }
void dfs(int cur, int pre) {
  in[cur] = ++tim;
  pa[0][cur] = pre;
  for (edge tmp : G[cur]) {
    int nxt = tmp.v, col = tmp.c, w = tmp.w;
    if (nxt == pre)
      continue;
    dfs(nxt, cur);
  }
  out[cur] = ++tim;
}
void build() {
  for (int i = 1; i < 18; i++)
    for (int j = 1; j <= n; j++)
      pa[i][j] = pa[i - 1][pa[i - 1][j]];
}
int lca(int x, int y) {
  if (anc(x, y))
    return x;
  if (anc(y, x))
    return y;
  for (int i = 17; i >= 0; i--)
    if (!anc(pa[i][x], y))
      x = pa[i][x];
  return pa[0][x];
}
int ans[123456], q_col[123456], q_val[123456], dist[123456];
vector<int> query[123456];
void solve(int cur, int pre, int cur_dis) {
  for (int id : query[cur]) {
    int mul = 1;
    if (id < 0)
      mul = -2, id = -id;
    ans[id] += mul * (cur_dis + cnt[q_col[id]] * q_val[id] - dist[q_col[id]]);
  }

  for (edge tmp : G[cur]) {
    int nxt = tmp.v, col = tmp.c, w = tmp.w;
    if (nxt == pre)
      continue;
    cnt[col]++;
    dist[col] += w;
    solve(nxt, cur, cur_dis + w);
    dist[col] -= w;
    cnt[col]--;
  }
}
int32_t main() {
  ios::sync_with_stdio();
  cin.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int u, v, c, w;
    cin >> u >> v >> c >> w;
    G[u].push_back(edge(v, c, w));
    G[v].push_back(edge(u, c, w));
  }
  dfs(1, 1);
  build();
  for (int i = 1; i <= q; i++) {
    int u, v;
    cin >> q_col[i] >> q_val[i] >> u >> v;
    query[u].push_back(i);
    query[v].push_back(i);
    query[lca(u, v)].push_back(-i);
  }
  solve(1, 1, 0);

  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
}