Problem p02986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define IO_OP                                                                  \
  std::ios::sync_with_stdio(0);                                                \
  std::cin.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;

const int INF = 1e9 + 7, N = 1e5 + 8, M = 300;

vector<pair<int, pi>> G[N];
int d[N], p[20][N], pc[N], pd[N], n, q, dis[N];
unordered_map<int, int> s[N], ct[N];
bool yes[N];

void dfs(int u = 1, int pa = 1, int dp = 0, int c = -1, int pdp = 0) {
  d[u] = dp;
  p[0][u] = pa;
  pc[u] = c;
  pd[u] = pdp;
  dis[u] = dis[pa] + pdp;
  for (auto &e : G[u])
    if (e.F != pa)
      dfs(e.F, u, dp + 1, e.S.F, e.S.S);
  if (dp % M == 0) {
    int t = u;
    while (p[0][t] != t && !yes[t]) {
      s[pc[t]][u] += pd[t];
      ct[pc[t]][u]++;
      t = p[0][t];
    }
    if (yes[t])
      for (int i = 1; i <= n - 1; i++)
        s[i][u] += s[i][t], ct[i][u] += ct[i][t];
    yes[u] = true;
  }
}

int lca(int x, int y) {
  if (d[x] < d[y])
    swap(x, y);
  for (int i = 19; i >= 0; --i)
    if (d[p[i][x]] >= d[y])
      x = p[i][x];
  if (x == y)
    return x;
  for (int i = 19; i >= 0; --i) {
    if (p[i][x] != p[i][y]) {
      x = p[i][x];
      y = p[i][y];
    }
  }
  return p[0][x];
}

pi qry(int u, int x) {
  pi ret = {0, 0};
  while (p[0][u] != u && !yes[u]) {
    ret.F += (pc[u] == x) * pd[u];
    ret.S += (pc[u] == x);
    u = p[0][u];
  }
  if (yes[u])
    ret.F += s[x][u], ret.S += ct[x][u];
  return ret;
}

int qry(int u, int x, int y) {
  int ret = dis[u];
  pi pp = qry(u, x);
  ret -= pp.F;
  ret += pp.S * y;
  return ret;
}

int main() {
  IO_OP;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    G[a].PB({b, {c, d}});
    G[b].PB({a, {c, d}});
  }
  dfs();
  for (int i = 1; i < 20; ++i) {
    for (int j = 1; j <= n; ++j)
      p[i][j] = p[i - 1][p[i - 1][j]];
  }
  while (q--) {
    int x, y, u, v;
    cin >> x >> y >> u >> v;
    cout << qry(u, x, y) + qry(v, x, y) - 2 * qry(lca(u, v), x, y) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define IO_OP                                                                  \
  std::ios::sync_with_stdio(0);                                                \
  std::cin.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;

const int INF = 1e9 + 7, N = 1e5 + 8, M = 400;

vector<pair<int, pi>> G[N];
int d[N], p[20][N], pc[N], pd[N], n, q, dis[N];
unordered_map<int, int> s[N], ct[N];
bool yes[N];

void dfs(int u = 1, int pa = 1, int dp = 0, int c = -1, int pdp = 0) {
  d[u] = dp;
  p[0][u] = pa;
  pc[u] = c;
  pd[u] = pdp;
  dis[u] = dis[pa] + pdp;
  for (auto &e : G[u])
    if (e.F != pa)
      dfs(e.F, u, dp + 1, e.S.F, e.S.S);
  if (dp % M == 0) {
    int t = u;
    while (p[0][t] != t && !yes[t]) {
      s[pc[t]][u] += pd[t];
      ct[pc[t]][u]++;
      t = p[0][t];
    }
    if (yes[t])
      for (int i = 1; i <= n - 1; i++)
        s[i][u] += s[i][t], ct[i][u] += ct[i][t];
    yes[u] = true;
  }
}

int lca(int x, int y) {
  if (d[x] < d[y])
    swap(x, y);
  for (int i = 19; i >= 0; --i)
    if (d[p[i][x]] >= d[y])
      x = p[i][x];
  if (x == y)
    return x;
  for (int i = 19; i >= 0; --i) {
    if (p[i][x] != p[i][y]) {
      x = p[i][x];
      y = p[i][y];
    }
  }
  return p[0][x];
}

pi qry(int u, int x) {
  pi ret = {0, 0};
  while (p[0][u] != u && !yes[u]) {
    ret.F += (pc[u] == x) * pd[u];
    ret.S += (pc[u] == x);
    u = p[0][u];
  }
  if (yes[u])
    ret.F += s[x][u], ret.S += ct[x][u];
  return ret;
}

int qry(int u, int x, int y) {
  int ret = dis[u];
  pi pp = qry(u, x);
  ret -= pp.F;
  ret += pp.S * y;
  return ret;
}

int main() {
  IO_OP;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    G[a].PB({b, {c, d}});
    G[b].PB({a, {c, d}});
  }
  dfs();
  for (int i = 1; i < 20; ++i) {
    for (int j = 1; j <= n; ++j)
      p[i][j] = p[i - 1][p[i - 1][j]];
  }
  while (q--) {
    int x, y, u, v;
    cin >> x >> y >> u >> v;
    cout << qry(u, x, y) + qry(v, x, y) - 2 * qry(lca(u, v), x, y) << endl;
  }
}