Problem p02986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
const int nax = 1e5 + 5;
pair<int, int> arr[nax];
vector<vector<pair<int, pair<int, ll>>>> v(nax);
int vis[nax] = {0};
int level[nax];
int parent[nax];
int dp[nax][20];
struct s {
  int a, b, c, d;
};
vector<vector
> pre(nax);
int ans[nax] = {0};
int find(int x, int d) {
  for (int i = 19; i >= 0; i--) {
    if (d < (1 << i))
      continue;
    x = dp[x][i];
    d -= (1 << i);
  }
  return x;
}
void dfs(int ci, int l) {
  vis[ci] = 1;
  level[ci] = l;
  if (l == 0)
    parent[ci] = ci;
  for (auto x : v[ci]) {
    if (vis[x.first] == 1)
      continue;
    parent[x.first] = ci;
    dfs(x.first, l + 1);
  }
}
int lca(int a, int b) {
  int p = a;
  int q = b;
  if (level[a] > level[b]) {
    p = b;
    q = a;
  }
  q = find(q, abs(level[a] - level[b]));
  if (p == q)
    return p;
  for (int i = 19; i >= 0; i--) {
    if (dp[p][i] == dp[q][i])
      continue;
    p = dp[p][i];
    q = dp[q][i];
  }
  return dp[p][0];
}
void dfs1(int ci, int sum, int col, int val) {
  vis[ci] = 1;
  for (auto x : pre[ci]) {
    if (x.b == 0)
      ans[x.a] += sum - arr[x.c].second + arr[x.c].first * x.d;
    else
      ans[x.a] -= 2 * (sum - arr[x.c].second + arr[x.c].first * x.d);
  }
  for (auto x : v[ci]) {
    if (vis[x.first] == 1)
      continue;
    arr[x.second.first].first++;
    arr[x.second.first].second += x.second.second;
    dfs1(x.first, sum + x.second.second, x.second.first, x.second.second);
  }
  if (ci != 0) {
    arr[col].first--;
    arr[col].second -= val;
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  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--;
    c--;
    v[a].push_back({b, {c, d}});
    v[b].push_back({a, {c, d}});
    arr[i] = {0, 0};
  }
  dfs(0, 0);
  for (int i = 0; i < n; i++) {
    vis[i] = 0;
    dp[i][0] = parent[i];
  }
  for (int j = 1; j < 20; j++) {
    for (int i = 0; i < n; i++)
      dp[i][j] = dp[dp[i][j - 1]][j - 1];
  }
  vector
 query;
  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    c--;
    d--;
    pre[c].push_back({i, 0, a, b});
    pre[d].push_back({i, 0, a, b});
    pre[lca(c, d)].push_back({i, 1, a, b});
  }
  dfs1(0, 0, -1, -1);
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
const int nax = 1e5 + 5;
pair<int, int> arr[nax];
vector<vector<pair<int, pair<int, ll>>>> v(nax);
int vis[nax] = {0};
int level[nax];
int parent[nax];
int dp[nax][20];
struct s {
  int a, b, c, d;
};
vector<vector
> pre(nax);
int ans[nax] = {0};
int find(int x, int d) {
  for (int i = 19; i >= 0; i--) {
    if (d < (1 << i))
      continue;
    x = dp[x][i];
    d -= (1 << i);
  }
  return x;
}
void dfs(int ci, int l) {
  vis[ci] = 1;
  level[ci] = l;
  if (l == 0)
    parent[ci] = ci;
  for (auto x : v[ci]) {
    if (vis[x.first] == 1)
      continue;
    parent[x.first] = ci;
    dfs(x.first, l + 1);
  }
}
int lca(int a, int b) {
  int p = a;
  int q = b;
  if (level[a] > level[b]) {
    p = b;
    q = a;
  }
  q = find(q, abs(level[a] - level[b]));
  if (p == q)
    return p;
  for (int i = 19; i >= 0; i--) {
    if (dp[p][i] == dp[q][i])
      continue;
    p = dp[p][i];
    q = dp[q][i];
  }
  return dp[p][0];
}
void dfs1(int ci, int sum, int col, int val) {
  vis[ci] = 1;
  for (auto x : pre[ci]) {
    if (x.b == 0)
      ans[x.a] += sum - arr[x.c].second + arr[x.c].first * x.d;
    else
      ans[x.a] -= 2 * (sum - arr[x.c].second + arr[x.c].first * x.d);
  }
  for (auto x : v[ci]) {
    if (vis[x.first] == 1)
      continue;
    arr[x.second.first].first++;
    arr[x.second.first].second += x.second.second;
    dfs1(x.first, sum + x.second.second, x.second.first, x.second.second);
  }
  if (ci != 0) {
    arr[col].first--;
    arr[col].second -= val;
  }
}
int main() {
  /*
        #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
        #endif
    */
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  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--;
    c--;
    v[a].push_back({b, {c, d}});
    v[b].push_back({a, {c, d}});
    arr[i] = {0, 0};
  }
  dfs(0, 0);
  for (int i = 0; i < n; i++) {
    vis[i] = 0;
    dp[i][0] = parent[i];
  }
  for (int j = 1; j < 20; j++) {
    for (int i = 0; i < n; i++)
      dp[i][j] = dp[dp[i][j - 1]][j - 1];
  }
  vector
 query;
  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    c--;
    d--;
    pre[c].push_back({i, 0, a, b});
    pre[d].push_back({i, 0, a, b});
    pre[lca(c, d)].push_back({i, 1, a, b});
  }
  dfs1(0, 0, -1, -1);
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\n";
}