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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
int h, w, n, m, k, s, t, q, ans, sum, last, cnt;
struct Edge {
  int to, cost;
};
vector<int> vec[100010];
string str;
bool f;
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Seg {
  int n;
  vector<int> node, num;
  Seg(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    node.resize(n * 2 - 1);
    num.resize(n * 2 - 1);
    rep(i, n) node[n + i - 1] = std::numeric_limits<int>::max();
    Mfor(i, n - 2) node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void update(int i, int x, int u) {
    i = i + n - 1;
    node[i] = x;
    num[i] = u;
    while (i > 0) {
      i = (i - 1) / 2;
      LL mn = i * 2 + 1;
      if (node[i * 2 + 1] > node[i * 2 + 2])
        mn = i * 2 + 2;
      node[i] = node[mn];
      num[i] = num[mn];
    }
  }
  Pll find(int i, int a, int b, int l, int r) {
    if (b < l || r < a)
      return Pll(INF, INF);
    if (l <= a && b <= r)
      return Pll(node[i], num[i]);
    Pll v1 = find(i * 2 + 1, a, (a + b) / 2, l, r),
        v2 = find(i * 2 + 2, (a + b + 1) / 2, b, l, r);
    return (v1.fir < v2.fir ? v1 : v2);
  }
};
struct vertex {
  int l, r, deep;
  bool f = 0;
};
vertex v[100010];
Seg seg(200010);
void lca() {
  vector<int> root;
  int num = 0, deep = 0;
  root.push_back(0);
  while (root.size() != 0) {
    int u = root[root.size() - 1];
    if (v[u].f == 0) {
      v[u].l = num;
      v[u].deep = deep;
      v[u].f = 1;
    }
    v[u].r = num;
    seg.update(num, deep, u);
    num++;
    rep(j, vec[u].size()) {
      if (v[vec[u][j]].f == 0) {
        root.push_back(vec[u][j]);
        vec[u].erase(vec[u].begin() + j);
        deep++;
        break;
      }
    }
    if (seg.node[seg.n + num - 2] == deep) {
      deep--;
      root.pop_back();
    }
  }
}
int main() {
  cin >> n;
  rep(i, n) {
    cin >> k;
    rep(j, k) {
      cin >> h;
      vec[i].push_back(h);
    }
  }
  lca();
  cin >> q;
  rep(i, q) {
    LL x, y;
    cin >> x >> y;
    if (v[x].l > v[y].l)
      swap(x, y);
    cout << seg.find(0, 0, seg.n - 1, v[x].l, v[y].r).sec << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
int h, w, n, m, k, s, t, q, ans, sum, last, cnt;
struct Edge {
  int to, cost;
};
vector<Edge> vec[100010];
string str;
bool f;
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Seg {
  int n;
  vector<int> node, num;
  Seg(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    node.resize(n * 2 - 1);
    num.resize(n * 2 - 1);
    rep(i, n) node[n + i - 1] = std::numeric_limits<int>::max();
    Mfor(i, n - 2) node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void update(int i, int x, int u) {
    i = i + n - 1;
    node[i] = x;
    num[i] = u;
    while (i > 0) {
      i = (i - 1) / 2;
      LL mn = i * 2 + 1;
      if (node[i * 2 + 1] > node[i * 2 + 2])
        mn = i * 2 + 2;
      node[i] = node[mn];
      num[i] = num[mn];
    }
  }
  Pll find(int i, int a, int b, int l, int r) {
    if (b < l || r < a)
      return Pll(INF, INF);
    if (l <= a && b <= r)
      return Pll(node[i], num[i]);
    Pll v1 = find(i * 2 + 1, a, (a + b) / 2, l, r),
        v2 = find(i * 2 + 2, (a + b + 1) / 2, b, l, r);
    return (v1.fir < v2.fir ? v1 : v2);
  }
};
struct vertex {
  int l, r, deep;
  bool f = 0;
};
vertex v[100010];
Seg seg(200010);
void lca() {
  vector<int> root;
  int num = 0, deep = 0;
  root.push_back(0);
  while (root.size() != 0) {
    int u = root[root.size() - 1];
    if (v[u].f == 0) {
      v[u].l = num;
      v[u].deep = deep;
      v[u].f = 1;
    }
    v[u].r = num;
    seg.update(num, deep, u);
    num++;
    rep(j, vec[u].size()) {
      if (v[vec[u][j].to].f == 0) {
        root.push_back(vec[u][j].to);
        vec[u].erase(vec[u].begin() + j);
        deep++;
        break;
      }
    }
    if (seg.node[seg.n + num - 2] == deep) {
      deep--;
      root.pop_back();
    }
  }
}
int main() {
  cin >> n;
  rep(i, n) {
    cin >> k;
    rep(j, k) {
      cin >> h;
      vec[i].push_back(Edge{h, 1});
    }
  }
  lca();
  cin >> q;
  rep(i, q) {
    LL x, y;
    cin >> x >> y;
    if (v[x].l > v[y].l)
      swap(x, y);
    cout << seg.find(0, 0, seg.n - 1, v[x].l, v[y].r).sec << endl;
  }
  return 0;
}