Problem p02986 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)

using namespace std;
using ll = long long;

struct HLD {
  const vector<vector<int>> &g;
  vector<int> size, label, parent, head;
  int k;

  HLD(const vector<vector<int>> &g_)
      : g(g_), size(g_.size()), label(g_.size()), parent(g_.size()),
        head(g_.size()) {
    dfs(0, -1);
    dfs2(0, -1, 0);
  }

  void dfs(int u, int p) {
    size[u] = 1;
    for (int v : g[u])
      if (v != p) {
        dfs(v, u);
        size[u] += size[v];
      }
  }

  void dfs2(int u, int p, int h) {
    label[u] = k++;
    head[u] = h;
    parent[u] = p;
    for (int v : g[u])
      if (v != p && size[v] * 2 > size[u])
        dfs2(v, u, h);
    for (int v : g[u])
      if (v != p && size[v] * 2 <= size[u])
        dfs2(v, u, v);
  }

  template <class F> void each_edge(int u, int v, F f) {
    for (;;) {
      if (label[u] > label[v])
        swap(u, v);
      if (head[u] == head[v]) {
        if (label[u] + 1 <= label[v]) {
          f(label[u] + 1, label[v]);
        }
        return;
      }
      f(label[head[v]], label[v]);
      v = parent[head[v]];
    }
  }
};

struct RMQ {
  int N;
  vector<int> dat;

  RMQ(int n) : N(n), dat(n * 2, 0) {}

  void update(int k, int v) {
    k += N;
    dat[k] = v;
    while (k > 1) {
      k >>= 1;
      dat[k] = dat[k * 2] + dat[k * 2 + 1];
    }
  }

  int query(int l, int r) {
    l += N;
    r += N;
    int res = 0;
    while (l <= r) {
      if ((l & 1) == 1)
        res += dat[l++];
      if ((r & 1) == 0)
        res += dat[r--];
      l >>= 1;
      r >>= 1;
    }
    return res;
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<vector<int>> g(n);
  struct edge {
    int v, w, c;
  };
  vector<vector<edge>> g2(n);
  rep(i, n - 1) {
    int u, v, c, w;
    cin >> u >> v >> c >> w;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
    g2[u].push_back((edge){v, w, c});
    g2[v].push_back((edge){u, w, c});
  }
  HLD hld(g);
  vector<int> ws(n);
  vector<int> cs(n);
  map<int, vector<int>> mp2;
  auto dfs = [&](auto dfs, int u, int p) -> void {
    for (edge e : g2[u])
      if (e.v != p) {
        ws[e.v] = e.w;
        cs[e.v] = e.c;
        dfs(dfs, e.v, u);
      }
  };
  dfs(dfs, 0, -1);
  for (int i = 1; i < n; i++) {
    mp2[cs[i]].push_back(i);
  }
  RMQ rmq(n);
  for (int i = 1; i < n; i++) {
    rmq.update(hld.label[i], ws[i]);
  }
  vector<ll> ans(q);
  vector<int> xs(q), ys(q), us(q), vs(q);
  rep(i, q) {
    cin >> xs[i] >> ys[i] >> us[i] >> vs[i];
    us[i]--;
    vs[i]--;
    hld.each_edge(us[i], vs[i],
                  [&](int l, int r) { ans[i] += rmq.query(l, r); });
  }
  map<int, vector<int>> mp;
  rep(i, q) { mp[xs[i]].push_back(i); }
  RMQ rmq2(n);
  RMQ rmq3(n);
  for (auto kv : mp) {
    for (int i : mp2[kv.first]) {
      rmq2.update(hld.label[i], ws[i]);
      rmq3.update(hld.label[i], 1);
    }
    for (int i : kv.second) {
      hld.each_edge(us[i], vs[i], [&](int l, int r) {
        ans[i] -= rmq2.query(l, r);
        ans[i] += rmq3.query(l, r) * ys[i];
      });
    }
    for (int i : mp2[kv.first]) {
      rmq2.update(hld.label[i], 0);
      rmq3.update(hld.label[i], 0);
    }
  }
  rep(i, q) { cout << ans[i] << '\n'; }
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)

using namespace std;
using ll = long long;

struct HLD {
  const vector<vector<int>> &g;
  vector<int> size, label, parent, head;
  int k;

  HLD(const vector<vector<int>> &g_)
      : g(g_), size(g_.size()), label(g_.size()), parent(g_.size()),
        head(g_.size()) {
    k = 0;
    dfs(0, -1);
    dfs2(0, -1, 0);
  }

  void dfs(int u, int p) {
    size[u] = 1;
    for (int v : g[u])
      if (v != p) {
        dfs(v, u);
        size[u] += size[v];
      }
  }

  void dfs2(int u, int p, int h) {
    label[u] = k++;
    head[u] = h;
    parent[u] = p;
    for (int v : g[u])
      if (v != p && size[v] * 2 > size[u])
        dfs2(v, u, h);
    for (int v : g[u])
      if (v != p && size[v] * 2 <= size[u])
        dfs2(v, u, v);
  }

  template <class F> void each_edge(int u, int v, F f) {
    for (;;) {
      if (label[u] > label[v])
        swap(u, v);
      if (head[u] == head[v]) {
        if (label[u] + 1 <= label[v]) {
          f(label[u] + 1, label[v]);
        }
        return;
      }
      f(label[head[v]], label[v]);
      v = parent[head[v]];
    }
  }
};

struct RMQ {
  int N;
  vector<int> dat;

  RMQ(int n) : N(n), dat(n * 2, 0) {}

  void update(int k, int v) {
    k += N;
    dat[k] = v;
    while (k > 1) {
      k >>= 1;
      dat[k] = dat[k * 2] + dat[k * 2 + 1];
    }
  }

  int query(int l, int r) {
    l += N;
    r += N;
    int res = 0;
    while (l <= r) {
      if ((l & 1) == 1)
        res += dat[l++];
      if ((r & 1) == 0)
        res += dat[r--];
      l >>= 1;
      r >>= 1;
    }
    return res;
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<vector<int>> g(n);
  struct edge {
    int v, w, c;
  };
  vector<vector<edge>> g2(n);
  rep(i, n - 1) {
    int u, v, c, w;
    cin >> u >> v >> c >> w;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
    g2[u].push_back((edge){v, w, c});
    g2[v].push_back((edge){u, w, c});
  }
  HLD hld(g);
  vector<int> ws(n);
  vector<int> cs(n);
  map<int, vector<int>> mp2;
  auto dfs = [&](auto dfs, int u, int p) -> void {
    for (edge e : g2[u])
      if (e.v != p) {
        ws[e.v] = e.w;
        cs[e.v] = e.c;
        dfs(dfs, e.v, u);
      }
  };
  dfs(dfs, 0, -1);
  for (int i = 1; i < n; i++) {
    mp2[cs[i]].push_back(i);
  }
  RMQ rmq(n);
  for (int i = 1; i < n; i++) {
    rmq.update(hld.label[i], ws[i]);
  }
  vector<ll> ans(q);
  vector<int> xs(q), ys(q), us(q), vs(q);
  rep(i, q) {
    cin >> xs[i] >> ys[i] >> us[i] >> vs[i];
    us[i]--;
    vs[i]--;
    hld.each_edge(us[i], vs[i],
                  [&](int l, int r) { ans[i] += rmq.query(l, r); });
  }
  map<int, vector<int>> mp;
  rep(i, q) { mp[xs[i]].push_back(i); }
  RMQ rmq2(n);
  RMQ rmq3(n);
  for (auto kv : mp) {
    for (int i : mp2[kv.first]) {
      rmq2.update(hld.label[i], ws[i]);
      rmq3.update(hld.label[i], 1);
    }
    for (int i : kv.second) {
      hld.each_edge(us[i], vs[i], [&](int l, int r) {
        ans[i] -= rmq2.query(l, r);
        ans[i] += rmq3.query(l, r) * ys[i];
      });
    }
    for (int i : mp2[kv.first]) {
      rmq2.update(hld.label[i], 0);
      rmq3.update(hld.label[i], 0);
    }
  }
  rep(i, q) { cout << ans[i] << '\n'; }
}