Problem p02986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define int long long
// #define double long double
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x)                                                              \
  for (auto &youso_ : (x))                                                     \
  cin >> youso_
#define bitcnt(x) __builtin_popcount(x)
#define lbit(x) __builtin_ffsll(x)
#define rbit(x) __builtin_clzll(x)
#define SZ(x) ((long long)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
#define cinfast() cin.tie(0), ios::sync_with_stdio(false)
#define PERM(c)                                                                \
  sort(All(c));                                                                \
  for (bool cp = true; cp; cp = next_permutation(All(c)))
#define MKORDER(n)                                                             \
  vector<int> od(n);                                                           \
  iota(All(od), 0LL);

template <typename T = long long> inline T IN() {
  T x;
  cin >> x;
  return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
  cin >> head;
  CIN(move(tail)...);
}
#define CCIN(...)                                                              \
  char __VA_ARGS__;                                                            \
  CIN(__VA_ARGS__)
#define DCIN(...)                                                              \
  double __VA_ARGS__;                                                          \
  CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
  long long __VA_ARGS__;                                                       \
  CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
  string __VA_ARGS__;                                                          \
  CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v)                                                              \
  {                                                                            \
    FOREACH(x, v) { cout << x << " "; }                                        \
    cout << "\n";                                                              \
  }
template <typename T = string> inline void eputs(T s) {
  cout << s << "\n";
  exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;

typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18;
const double PI = atan(1.0) * 4.0;
const ll dw[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dh[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238

ll N, M;
struct edge {
  ll to, col, len;
};
vector<vector<edge>> g;
VL d;

// doubling build:n query:logn
struct LCA {
  int n, h;
  vector<vector<int>> G, par;
  vector<int> dep;
  LCA() {}
  LCA(int n) : n(n), G(n), dep(n) {
    h = 1;
    while ((1 << h) <= n)
      h++;
    par.assign(h, vector<int>(n, -1));
  }

  void add_edge(int u, int v) {
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  void dfs(int v, int p, int d) {
    par[0][v] = p;
    dep[v] = d;
    for (int u : G[v])
      if (u != p)
        dfs(u, v, d + 1);
  }

  void build(int r = 0) {
    dfs(r, -1, 0);
    for (int k = 0; k + 1 < h; k++)
      for (int v = 0; v < n; v++)
        if (~par[k][v])
          par[k + 1][v] = par[k][par[k][v]];
  }

  int lca(int u, int v) {
    if (dep[u] > dep[v])
      swap(u, v);
    REP(k, h) {
      if (((dep[v] - dep[u]) >> k) & 1)
        v = par[k][v];
    }
    if (u == v)
      return u;

    for (int k = h - 1; k >= 0; k--)
      if (par[k][u] != par[k][v])
        u = par[k][u], v = par[k][v];

    return par[0][u];
  }

  int distance(int u, int v) { return dep[u] + dep[v] - dep[lca(u, v)] * 2; }
};

ll ans[100100], clen[100100], ccnt[100100], x[100100], y[100100], u[100100],
    v[100100];
using tp = tuple<ll, ll, ll, ll>;
vector<vector<tp>> qs;
vector<map<ll, ll>> dist;

void dfs(ll now, ll par, ll d) {
  FOREACH(dd, dist[now]) {
    ll q = dd.fi;
    dd.se = d - clen[x[q]] + ccnt[x[q]] * y[q];
  }
  FOREACH(e, g[now]) {
    ll to = e.to, col = e.col, len = e.len;
    if (to == par)
      continue;
    clen[col] += len;
    ccnt[col]++;
    dfs(to, now, d + len);
    clen[col] -= len;
    ccnt[col]--;
  }
}

signed main() {
  cinfast();
  LCIN(N, Q);
  LCA lca(N);
  g.resize(N);
  REP(i, N - 1) {
    LCIN(a, b, c, d);
    a--, b--;
    g[a].push_back({b, c, d});
    g[b].push_back({a, c, d});
    lca.add_edge(a, b);
  }
  lca.build();
  qs.resize(N);
  dist.resize(N);
  VL com(N);
  REP(i, Q) {
    cin >> x[i] >> y[i] >> u[i] >> v[i];
    u[i]--, v[i]--;
    dist[u[i]][i] = 0;
    dist[v[i]][i] = 0;
    com[i] = lca.lca(u[i], v[i]);
    dist[com[i]][i] = 0;
  }
  dfs(0, -1, 0);
  REP(i, Q) {
    cout << dist[u[i]][i] + dist[v[i]][i] - 2 * dist[com[i]][i] << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int long long
// #define double long double
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x)                                                              \
  for (auto &youso_ : (x))                                                     \
  cin >> youso_
#define bitcnt(x) __builtin_popcount(x)
#define lbit(x) __builtin_ffsll(x)
#define rbit(x) __builtin_clzll(x)
#define SZ(x) ((long long)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
#define cinfast() cin.tie(0), ios::sync_with_stdio(false)
#define PERM(c)                                                                \
  sort(All(c));                                                                \
  for (bool cp = true; cp; cp = next_permutation(All(c)))
#define MKORDER(n)                                                             \
  vector<int> od(n);                                                           \
  iota(All(od), 0LL);

template <typename T = long long> inline T IN() {
  T x;
  cin >> x;
  return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
  cin >> head;
  CIN(move(tail)...);
}
#define CCIN(...)                                                              \
  char __VA_ARGS__;                                                            \
  CIN(__VA_ARGS__)
#define DCIN(...)                                                              \
  double __VA_ARGS__;                                                          \
  CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
  long long __VA_ARGS__;                                                       \
  CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
  string __VA_ARGS__;                                                          \
  CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v)                                                              \
  {                                                                            \
    FOREACH(x, v) { cout << x << " "; }                                        \
    cout << "\n";                                                              \
  }
template <typename T = string> inline void eputs(T s) {
  cout << s << "\n";
  exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;

typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18;
const double PI = atan(1.0) * 4.0;
const ll dw[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dh[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238

ll N, M;
struct edge {
  ll to, col, len;
};
vector<vector<edge>> g;
VL d;

// doubling build:n query:logn
struct LCA {
  int n, h;
  vector<vector<int>> G, par;
  vector<int> dep;
  LCA() {}
  LCA(int n) : n(n), G(n), dep(n) {
    h = 1;
    while ((1 << h) <= n)
      h++;
    par.assign(h, vector<int>(n, -1));
  }

  void add_edge(int u, int v) {
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  void dfs(int v, int p, int d) {
    par[0][v] = p;
    dep[v] = d;
    for (int u : G[v])
      if (u != p)
        dfs(u, v, d + 1);
  }

  void build(int r = 0) {
    dfs(r, -1, 0);
    for (int k = 0; k + 1 < h; k++)
      for (int v = 0; v < n; v++)
        if (~par[k][v])
          par[k + 1][v] = par[k][par[k][v]];
  }

  int lca(int u, int v) {
    if (dep[u] > dep[v])
      swap(u, v);
    REP(k, h) {
      if (((dep[v] - dep[u]) >> k) & 1)
        v = par[k][v];
    }
    if (u == v)
      return u;

    for (int k = h - 1; k >= 0; k--)
      if (par[k][u] != par[k][v])
        u = par[k][u], v = par[k][v];

    return par[0][u];
  }

  int distance(int u, int v) { return dep[u] + dep[v] - dep[lca(u, v)] * 2; }
};

ll ans[100100], clen[100100], ccnt[100100], x[100100], y[100100], u[100100],
    v[100100];
using tp = tuple<ll, ll, ll, ll>;
vector<vector<tp>> qs;
vector<map<ll, ll>> dist;

void dfs(ll now, ll par, ll d) {
  FOREACH(dd, dist[now]) {
    ll q = dd.fi;
    dd.se = d - clen[x[q]] + ccnt[x[q]] * y[q];
  }
  FOREACH(e, g[now]) {
    ll to = e.to, col = e.col, len = e.len;
    if (to == par)
      continue;
    clen[col] += len;
    ccnt[col]++;
    dfs(to, now, d + len);
    clen[col] -= len;
    ccnt[col]--;
  }
}

signed main() {
  cinfast();
  LCIN(N, Q);
  LCA lca(N);
  g.resize(N);
  REP(i, N - 1) {
    LCIN(a, b, c, d);
    a--, b--;
    g[a].push_back({b, c, d});
    g[b].push_back({a, c, d});
    lca.add_edge(a, b);
  }
  lca.build();
  qs.resize(N);
  dist.resize(N);
  VL com(Q);
  REP(i, Q) {
    cin >> x[i] >> y[i] >> u[i] >> v[i];
    u[i]--, v[i]--;
    dist[u[i]][i] = 0;
    dist[v[i]][i] = 0;
    com[i] = lca.lca(u[i], v[i]);
    dist[com[i]][i] = 0;
  }
  dfs(0, -1, 0);
  REP(i, Q) {
    cout << dist[u[i]][i] + dist[v[i]][i] - 2 * dist[com[i]][i] << "\n";
  }
}