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

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

const int INF = (1LL << 31) - 1;

class RMQ {
private:
  int N;
  vector<int> data;
  vector<int> rdata;

  int find(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return -1;
    if (a <= l && r <= b)
      return data[k];
    int x1 = find(a, b, 2 * k + 1, l, (l + r) / 2);
    int x2 = find(a, b, 2 * k + 2, (l + r) / 2, r);

    if (x1 == -1)
      return x2;
    if (x2 == -1)
      return x1;
    if (rdata[x1] < rdata[x2])
      return x1;
    else
      return x2;
  }

public:
  RMQ(vector<int> vec) {
    N = 1;
    while (N < vec.size())
      N <<= 1;
    data.assign(2 * N - 1, -1);
    rdata.assign(N, INF);

    for (int i = 0; i < vec.size(); i++) {
      data[i + N - 1] = i;
      rdata[i] = vec[i];
    }
    for (int p = N - 2; p >= 0; p--) {
      if (rdata[data[p * 2 + 1]] < rdata[data[p * 2 + 2]])
        data[p] = data[p * 2 + 1];
      else
        data[p] = data[p * 2 + 2];
    }
  }
  int find(int a, int b) { return find(a, b, 0, 0, N); }
};

void euler_tour(vector<vector<int>> &G, int u, int d, vector<int> &tour,
                vector<int> &depth) {
  tour.push_back(u);
  depth.push_back(d);
  for (auto v : G[u]) {
    euler_tour(G, v, d + 1, tour, depth);
    tour.push_back(u);
    depth.push_back(d);
  }
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(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].push_back(c);
    }
  }

  vector<int> tour, depth;
  euler_tour(G, 0, 0, tour, depth);
  vector<int> rightmsot(N);
  for (int i = 0; i < tour.size(); i++)
    rightmsot[tour[i]] = i;

  RMQ rmq(tour);

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int u, v;
    cin >> u >> v;
    int ru = rightmsot[u], rv = rightmsot[v];
    if (ru > rv)
      swap(ru, rv);
    rv++;
    int d = rmq.find(ru, rv);
    cout << tour[d] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

const int INF = (1LL << 31) - 1;

class RMQ {
private:
  int N;
  vector<int> data;

  int find(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return data[k];
    int x1 = find(a, b, 2 * k + 1, l, (l + r) / 2);
    int x2 = find(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(x1, x2);
  }

public:
  RMQ(int N_) {
    N = 1;
    while (N < N_)
      N <<= 1;
    data.assign(2 * N - 1, INF);
  }

  void update(int x, int a) {
    x += N - 1;
    data[x] = a;
    while (x) {
      x = (x - 1) / 2;
      data[x] = min(data[2 * x + 1], data[2 * x + 2]);
    }
  }
  int find(int a, int b) { return find(a, b, 0, 0, N); }
};

void euler_tour(vector<vector<int>> &G, int u, int d, vector<int> &tour,
                vector<int> &depth) {
  tour.push_back(u);
  depth.push_back(d);
  for (auto v : G[u]) {
    euler_tour(G, v, d + 1, tour, depth);
    tour.push_back(u);
    depth.push_back(d);
  }
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(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].push_back(c);
    }
  }

  vector<int> tour, depth;
  euler_tour(G, 0, 0, tour, depth);
  vector<int> rightmsot(N);
  for (int i = 0; i < tour.size(); i++)
    rightmsot[tour[i]] = i;

  RMQ rmq(tour.size());
  for (int i = 0; i < tour.size(); i++) {
    //        cout << tour[i] << " " << depth[i] << endl;
    rmq.update(i, depth[i]);
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int u, v;
    cin >> u >> v;
    int ru = rightmsot[u], rv = rightmsot[v];
    if (ru > rv)
      swap(ru, rv);
    rv++;
    int d = rmq.find(ru, rv);
    for (int j = ru; j < rv; j++) {
      if (depth[j] == d) {
        cout << tour[j] << endl;
        break;
      }
    }
  }

  return 0;
}