Problem p02986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << (i + 1 == v.size() ? "\n" : " ");
  }
  return os;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t) {
    fill_v(e, v);
  }
};
int n, q;
struct edge {
  int to, c, d;
};
vector<vector<edge>> G;
vector<vector<int>> par;
vector<int> dep;
void ddfs(int i, int p) {
  for (auto &e : G[i]) {
    if (e.to == p)
      continue;
    par[e.to][0] = i;
    dep[e.to] = dep[i] + 1;
    ddfs(e.to, i);
  }
}
void build() {
  par[0][0] = -1;
  dep[0] = 0;
  ddfs(0, -1);
  for (int j = 1; j < 20; j++) {
    for (int i = 0; i < n; i++) {
      if (par[i][j - 1] == -1) {
        par[i][j] = -1;
      } else {
        par[i][j] = par[par[i][j - 1]][j - 1];
      }
    }
  }
}
int lca(int u, int v) {
  if (dep[u] > dep[v])
    swap(u, v);
  for (int i = 19; i >= 0; i--) {
    if (((dep[v] - dep[u]) >> i) & 1)
      v = par[v][i];
  }
  if (u == v)
    return u;
  for (int i = 19; i >= 0; i--) {
    if (par[u][i] != par[v][i]) {
      u = par[u][i];
      v = par[v][i];
    }
  }
  return par[u][0];
}
struct st {
  int x, y, t, id;
};
vector<vector<st>> qs;
vector<int> ans, co, sum;
void dfs(int i, int p, int c, int d, int de) {
  for (auto &e : qs[i]) {
    int s = de - sum[e.x] + co[e.x] * e.y;
    if (e.t == 0) {
      ans[e.id] += s;
    } else {
      ans[e.id] -= 2 * s;
    }
  }
  for (auto &e : G[i]) {
    if (e.to == p) {
      continue;
    }
    co[e.c]++;
    sum[e.c] += e.d;
    dfs(e.to, i, e.c, e.d, de + e.d);
  }
  if (p >= 0) {
    co[c]--;
    sum[c] -= d;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> q;
  G.resize(n);
  qs.resize(n);
  par.resize(n);
  dep.resize(n);
  for (int i = 0; i < n; i++) {
    par[i].resize(20);
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    --a;
    --b;
    --c;
    G[a].push_back({b, c, d});
    G[b].push_back({a, c, d});
  }
  build();
  for (int i = 0; i < q; i++) {
    int x, y, u, v;
    cin >> x >> y >> u >> v;
    --x;
    --u;
    --v;
    if (u > 0) {
      qs[u].push_back({x, y, 0, i});
    }
    if (v > 0) {
      qs[v].push_back({x, y, 0, i});
    }
    if (lca(u, v) > 0) {
      qs[lca(u, v)].push_back({x, y, 1, i});
    }
  }
  ans.resize(n);
  co.resize(n);
  sum.resize(n);
  dfs(0, -1, 0, 0, 0);
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << (i + 1 == v.size() ? "\n" : " ");
  }
  return os;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t) {
    fill_v(e, v);
  }
};
int n, q;
struct edge {
  int to, c, d;
};
vector<vector<edge>> G;
vector<vector<int>> par;
vector<int> dep;
void ddfs(int i, int p) {
  for (auto &e : G[i]) {
    if (e.to == p)
      continue;
    par[e.to][0] = i;
    dep[e.to] = dep[i] + 1;
    ddfs(e.to, i);
  }
}
void build() {
  par[0][0] = -1;
  dep[0] = 0;
  ddfs(0, -1);
  for (int j = 1; j < 20; j++) {
    for (int i = 0; i < n; i++) {
      if (par[i][j - 1] == -1) {
        par[i][j] = -1;
      } else {
        par[i][j] = par[par[i][j - 1]][j - 1];
      }
    }
  }
}
int lca(int u, int v) {
  if (dep[u] > dep[v])
    swap(u, v);
  for (int i = 19; i >= 0; i--) {
    if (((dep[v] - dep[u]) >> i) & 1)
      v = par[v][i];
  }
  if (u == v)
    return u;
  for (int i = 19; i >= 0; i--) {
    if (par[u][i] != par[v][i]) {
      u = par[u][i];
      v = par[v][i];
    }
  }
  return par[u][0];
}
struct st {
  int x, y, t, id;
};
vector<vector<st>> qs;
vector<int> ans, co, sum;
void dfs(int i, int p, int c, int d, int de) {
  for (auto &e : qs[i]) {
    int s = de - sum[e.x] + co[e.x] * e.y;
    if (e.t == 0) {
      ans[e.id] += s;
    } else {
      ans[e.id] -= 2 * s;
    }
  }
  for (auto &e : G[i]) {
    if (e.to == p) {
      continue;
    }
    co[e.c]++;
    sum[e.c] += e.d;
    dfs(e.to, i, e.c, e.d, de + e.d);
  }
  if (p >= 0) {
    co[c]--;
    sum[c] -= d;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> q;
  G.resize(n);
  qs.resize(n);
  par.resize(n);
  dep.resize(n);
  for (int i = 0; i < n; i++) {
    par[i].resize(20);
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    --a;
    --b;
    --c;
    G[a].push_back({b, c, d});
    G[b].push_back({a, c, d});
  }
  build();
  for (int i = 0; i < q; i++) {
    int x, y, u, v;
    cin >> x >> y >> u >> v;
    --x;
    --u;
    --v;
    if (u > 0) {
      qs[u].push_back({x, y, 0, i});
    }
    if (v > 0) {
      qs[v].push_back({x, y, 0, i});
    }
    if (lca(u, v) > 0) {
      qs[lca(u, v)].push_back({x, y, 1, i});
    }
  }
  ans.resize(q);
  co.resize(n);
  sum.resize(n);
  dfs(0, -1, 0, 0, 0);
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}