Problem p03634 (C++)

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

#define ORZ_lqs2015
#define ORZ_EVERYBODY

#define fi first
#define se second
typedef long long ll;
const size_t MAX_N = (size_t)1e5 + 5;

vector<pair<int, ll>> g[MAX_N];
ll dis[MAX_N];
int lca[20][MAX_N], n, k, dep[MAX_N];

void dfs(int v, int p, int depth, ll d) {
  dep[v] = depth;
  lca[0][v] = p;
  dis[v] = d;
  for (auto x : g[v])
    if (x.fi != p)
      dfs(x.fi, v, depth + 1, d + x.se);
}

int up(int x, int height) {
  for (int i = 20; i >= 0; i--)
    if (height >> i & 1)
      x = lca[i][x];
  return x;
}

int getLca(int x, int y) {
  if (dep[x] < dep[y])
    swap(x, y);
  x = up(x, dep[x] - dep[y]);
  if (x == y)
    return x;
  int l = 1, r = 20, mid, res = -1;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (up(x, mid) == up(y, mid)) {
      r = mid - 1;
      res = mid;
    } else {
      l = mid + 1;
    }
  }
  return up(x, res);
}

ll getDis(int x, int y) {
  assert(dep[x] >= dep[y]);
  return dis[x] - dis[y];
}

int main() {
  scanf("%d", &n);
  for (int i = 1, a, b, c; i < n; i++) {
    scanf("%d%d%d", &a, &b, &c);
    g[a].emplace_back(b, (ll)c);
    g[b].emplace_back(a, (ll)c);
  }
  dfs(1, 0, 0, 0LL);
  for (int i = 1; i < 20; i++)
    for (int j = 1; j <= n; j++)
      lca[i][j] = lca[i - 1][lca[i - 1][j]];
  int q;
  scanf("%d%d", &q, &k);
  unordered_map<int, int> l;
  l.reserve(512);
  l.max_load_factor(0.25f);
  ios::sync_with_stdio(false);
  for (int i = 0, x, y; i < q; i++) {
    scanf("%d%d", &x, &y);
    if (!l.count(x))
      l[x] = getLca(x, k);
    if (!l.count(y))
      l[y] = getLca(y, k);
    cout << getDis(x, l[x]) + getDis(k, l[x]) + getDis(k, l[y]) +
                getDis(y, l[y])
         << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ORZ_lqs2015
#define ORZ_EVERYBODY

#define fi first
#define se second
typedef long long ll;
const size_t MAX_N = (size_t)1e5 + 5;

vector<pair<int, ll>> g[MAX_N];
ll dis[MAX_N];
int lca[20][MAX_N], n, k, dep[MAX_N];

void dfs(int v, int p, int depth, ll d) {
  dep[v] = depth;
  lca[0][v] = p;
  dis[v] = d;
  for (auto x : g[v])
    if (x.fi != p)
      dfs(x.fi, v, depth + 1, d + x.se);
}

int up(int x, int height) {
  for (int i = 20; i >= 0; i--)
    if (height >> i & 1)
      x = lca[i][x];
  return x;
}

int getLca(int x, int y) {
  if (dep[x] < dep[y])
    swap(x, y);
  x = up(x, dep[x] - dep[y]);
  if (x == y)
    return x;
  int l = 1, r = n, mid, res = -1;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (up(x, mid) == up(y, mid)) {
      r = mid - 1;
      res = mid;
    } else {
      l = mid + 1;
    }
  }
  return up(x, res);
}

ll getDis(int x, int y) {
  assert(dep[x] >= dep[y]);
  return dis[x] - dis[y];
}

int main() {
  scanf("%d", &n);
  for (int i = 1, a, b, c; i < n; i++) {
    scanf("%d%d%d", &a, &b, &c);
    g[a].emplace_back(b, (ll)c);
    g[b].emplace_back(a, (ll)c);
  }
  dfs(1, 0, 0, 0LL);
  for (int i = 1; i < 20; i++)
    for (int j = 1; j <= n; j++)
      lca[i][j] = lca[i - 1][lca[i - 1][j]];
  int q;
  scanf("%d%d", &q, &k);
  unordered_map<int, int> l;
  l.reserve(512);
  l.max_load_factor(0.25f);
  ios::sync_with_stdio(false);
  for (int i = 0, x, y; i < q; i++) {
    scanf("%d%d", &x, &y);
    if (!l.count(x))
      l[x] = getLca(x, k);
    if (!l.count(y))
      l[y] = getLca(y, k);
    cout << getDis(x, l[x]) + getDis(k, l[x]) + getDis(k, l[y]) +
                getDis(y, l[y])
         << '\n';
  }
}