Problem p02373 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define INF 200000000

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;

vector<int> G[100000];
vector<int> et, depth, id;

struct ST {
  vector<pii> data;
  int n;
  int segn;

  ST(int v, int k) : n(v) {
    for (segn = 1; segn < (n * 2 - 1); segn *= 2)
      ;
    data.assign(2 * segn, make_pair(INF, -1));
    for (int i = 0; i < k; i++) {
      data[segn + i] = make_pair(depth[i], et[i]);
    }
    for (int i = segn - 1; i >= 1; --i) {
      data[i] = min(data[i * 2], data[i * 2 + 1]);
    }
  }

  pii find(int s, int t, int l, int r) {
    if (r == -1) {
      r = segn;
    }
    if (t <= s || r == 0) {
      return make_pair(INF, -1);
    }
    if (t - s == r) {
      return data[l];
    }

    return min(find(s, min(t, r / 2), l * 2, r / 2),
               find(max(0, s - (r / 2)), t - (r / 2), l * 2 + 1, r / 2));
  }
};

void ET(int n, int p, int &eti, int d) {
  id[n] = eti;
  et[eti] = n;
  depth[eti] = d;
  eti++;

  for (auto &e : G[n]) {
    if (e != p) {
      ET(e, n, eti, d + 1);
      et[eti] = n;
      depth[eti] = d;
      eti++;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;

    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      G[i].emplace_back(c);
    }
  }

  et = vector<int>(n * 2 - 1);
  depth = vector<int>(n * 2 - 1);
  id = vector<int>(n);

  int eti = 0;
  ET(0, -1, eti, 0);

  ST st(n, eti);

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int u, v;
    cin >> u >> v;

    printf("%d\\n",
           st.find(min(id[u], id[v]), max(id[u], id[v]) + 1, 1, -1).second);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))

#define INF 200000000

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;

vector<int> G[100000];
vector<int> et, depth, id;

struct ST {
  vector<pii> data;
  int n;
  int segn;

  ST(int v, int k) : n(v) {
    for (segn = 1; segn < (n * 2 - 1); segn *= 2)
      ;
    data.assign(2 * segn, make_pair(INF, -1));
    for (int i = 0; i < k; i++) {
      data[segn + i] = make_pair(depth[i], et[i]);
    }
    for (int i = segn - 1; i >= 1; --i) {
      data[i] = min(data[i * 2], data[i * 2 + 1]);
    }
  }

  pii find(int s, int t, int l, int r) {
    if (r == -1) {
      r = segn;
    }
    if (t <= s || r == 0) {
      return make_pair(INF, -1);
    }
    if (t - s == r) {
      return data[l];
    }

    return min(find(s, min(t, r / 2), l * 2, r / 2),
               find(max(0, s - (r / 2)), t - (r / 2), l * 2 + 1, r / 2));
  }
};

void ET(int n, int p, int &eti, int d) {
  id[n] = eti;
  et[eti] = n;
  depth[eti] = d;
  eti++;

  for (auto &e : G[n]) {
    if (e != p) {
      ET(e, n, eti, d + 1);
      et[eti] = n;
      depth[eti] = d;
      eti++;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;

    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      G[i].emplace_back(c);
    }
  }

  et = vector<int>(n * 2 - 1);
  depth = vector<int>(n * 2 - 1);
  id = vector<int>(n);

  int eti = 0;
  ET(0, -1, eti, 0);

  ST st(n, eti);

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int u, v;
    cin >> u >> v;

    printf("%d\\n",
           st.find(min(id[u], id[v]), max(id[u], id[v]) + 1, 1, -1).second);
  }

  return 0;
}