Problem p02373 (C++)

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

using namespace std;

template <typename Monoid> struct DisjointSparseTable {
  using F = function<Monoid(Monoid, Monoid)>;
  const F &f;
  vector<vector<Monoid>> st;

  DisjointSparseTable(const vector<Monoid> &v, const F &f) : f(f) {

    int b = 0;
    while ((1 << b) <= v.size())
      ++b;
    st.resize(b, vector<Monoid>(v.size(), Monoid()));
    for (int i = 0; i < v.size(); i++)
      st[0][i] = v[i];
    for (int i = 1; i < b; i++) {
      int shift = 1 << i;
      for (int j = 0; j < v.size(); j += shift << 1) {
        int t = min(j + shift, (int)v.size());
        st[i][t - 1] = v[t - 1];
        for (int k = t - 2; k >= j; k--)
          st[i][k] = f(v[k], st[i][k + 1]);
        if (v.size() <= t)
          break;
        st[i][t] = v[t];
        int r = min(t + shift, (int)v.size());
        for (int k = t + 1; k < r; k++)
          st[i][k] = f(st[i][k - 1], v[k]);
      }
    }
  }

  Monoid query(int l, int r) {
    if (l == --r)
      return st[0][l];
    int p = 31 - __builtin_clz(l ^ r);
    return f(st[p][l], st[p][r]);
  }
};

using Pi = pair<int, int>;

vector<int> g[100000];
vector<Pi> depth;
int rev[100000];

void dfs(int idx, int dep) {
  rev[idx] = (int)depth.size();
  depth.emplace_back(dep, idx);
  for (auto &to : g[idx]) {
    dfs(to, dep + 1);
    depth.emplace_back(dep, idx);
  }
}

int main() {
  int N, Q;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int k;
    scanf("%d", &k);
    for (int j = 0; j < k; j++) {
      int c;
      scanf("%d", &c);
      g[i].push_back(c);
    }
  }
  dfs(0, -1);
  auto f = [](const Pi &a, const Pi &b) { return min(a, b); };
  DisjointSparseTable<Pi> beet(depth, f);
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    auto p = minmax(rev[a], rev[b]);
    printf("%d\n", beet.query(p.first, p.second).second);
  }
}


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

using namespace std;

template <typename Monoid> struct DisjointSparseTable {
  using F = function<Monoid(Monoid, Monoid)>;
  const F &f;
  vector<vector<Monoid>> st;

  DisjointSparseTable(const vector<Monoid> &v, const F &f) : f(f) {

    int b = 0;
    while ((1 << b) <= v.size())
      ++b;
    st.resize(b, vector<Monoid>(v.size(), Monoid()));
    for (int i = 0; i < v.size(); i++)
      st[0][i] = v[i];
    for (int i = 1; i < b; i++) {
      int shift = 1 << i;
      for (int j = 0; j < v.size(); j += shift << 1) {
        int t = min(j + shift, (int)v.size());
        st[i][t - 1] = v[t - 1];
        for (int k = t - 2; k >= j; k--)
          st[i][k] = f(v[k], st[i][k + 1]);
        if (v.size() <= t)
          break;
        st[i][t] = v[t];
        int r = min(t + shift, (int)v.size());
        for (int k = t + 1; k < r; k++)
          st[i][k] = f(st[i][k - 1], v[k]);
      }
    }
  }

  Monoid query(int l, int r) {
    if (l >= --r)
      return st[0][l];
    int p = 31 - __builtin_clz(l ^ r);
    return f(st[p][l], st[p][r]);
  }
};

using Pi = pair<int, int>;

vector<int> g[100000];
vector<Pi> depth;
int rev[100000];

void dfs(int idx, int dep) {
  rev[idx] = (int)depth.size();
  depth.emplace_back(dep, idx);
  for (auto &to : g[idx]) {
    dfs(to, dep + 1);
    depth.emplace_back(dep, idx);
  }
}

int main() {
  int N, Q;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int k;
    scanf("%d", &k);
    for (int j = 0; j < k; j++) {
      int c;
      scanf("%d", &c);
      g[i].push_back(c);
    }
  }
  dfs(0, -1);
  auto f = [](const Pi &a, const Pi &b) { return min(a, b); };
  DisjointSparseTable<Pi> beet(depth, f);
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    auto p = minmax(rev[a], rev[b]);
    printf("%d\n", beet.query(p.first, p.second).second);
  }
}