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

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

vector<int> X, Y;
vector<int> SX, TX, SY, TY;
vector<int> data;

void dfs(vector<int> vec, int k, bool xaxis) {
  vector<pair<int, int>> cvec;
  if (xaxis) {
    for (auto v : vec)
      cvec.push_back({X[v], v});
  } else {
    for (auto v : vec)
      cvec.push_back({Y[v], v});
  }
  sort(cvec.begin(), cvec.end());

  int m = cvec.size();
  data[k] = cvec[m / 2].second;

  vector<int> lvec;
  for (int i = 0; i < m / 2; i++) {
    lvec.push_back(cvec[i].second);
  }
  if (lvec.size() > 0) {
    dfs(lvec, 2 * k + 1, xaxis ^ true);
  }
  vector<int> rvec;
  for (int i = m / 2 + 1; i < m; i++) {
    rvec.push_back(cvec[i].second);
  }
  if (rvec.size() > 0) {
    dfs(rvec, 2 * k + 2, xaxis ^ true);
  }
}

void buildtree() {
  int N_ = X.size();
  int N = 1;
  while (N <= N_)
    N <<= 1;
  data.assign(2 * N - 1, -1);

  vector<int> vec;
  for (int i = 0; i < N_; i++)
    vec.push_back(i);

  dfs(vec, 0, true);
}

void query(int k, int sx, int tx, int sy, int ty, bool xaxis,
           vector<int> &ans) {
  if (data[k] == -1)
    return;

  if (xaxis) {
    if (sx <= X[data[k]] && X[data[k]] <= tx && sy <= Y[data[k]] &&
        Y[data[k]] <= ty)
      ans.push_back(data[k]);

    if (sx <= X[data[k]])
      query(2 * k + 1, sx, tx, sy, ty, xaxis ^ true, ans);
    if (X[data[k]] <= tx)
      query(2 * k + 2, sx, tx, sy, ty, xaxis ^ true, ans);
  } else {
    if (sx <= X[data[k]] && X[data[k]] <= tx && sy <= Y[data[k]] &&
        Y[data[k]] <= ty)
      ans.push_back(data[k]);

    if (sy <= Y[data[k]])
      query(2 * k + 1, sx, tx, sy, ty, xaxis ^ true, ans);
    if (Y[data[k]] <= ty)
      query(2 * k + 2, sx, tx, sy, ty, xaxis ^ true, ans);
  }
}

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

  int N;
  //    cin >> N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int x, y;
    //        cin >> x >> y;
    scanf("%d %d", &x, &y);
    X.push_back(x);
    Y.push_back(y);
  }

  buildtree();

  int Q;
  //    cin >> Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int sx, tx, sy, ty;
    //        cin >> sx >> tx >> sy >> ty;
    scanf("%d %d %d %d", &sx, &tx, &sy, &ty);

    vector<int> ans;
    query(0, sx, tx, sy, ty, true, ans);
    sort(ans.begin(), ans.end());
    for (auto v : ans) {
      //            cout << v << endl;
      printf("%d\\n", v);
    }
    printf("\\n");

    //        cout << endl;

    //        SX.push_back(sx);
    //        TX.push_back(tx);
    //        SY.push_back(sy);
    //        TY.push_back(ty);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> X, Y;
vector<int> SX, TX, SY, TY;
vector<int> data;

void dfs(vector<int> vec, int k, bool xaxis) {
  vector<pair<int, int>> cvec;
  if (xaxis) {
    for (auto v : vec)
      cvec.push_back({X[v], v});
  } else {
    for (auto v : vec)
      cvec.push_back({Y[v], v});
  }
  sort(cvec.begin(), cvec.end());

  int m = cvec.size();
  data[k] = cvec[m / 2].second;

  vector<int> lvec;
  for (int i = 0; i < m / 2; i++) {
    lvec.push_back(cvec[i].second);
  }
  if (lvec.size() > 0) {
    dfs(lvec, 2 * k + 1, xaxis ^ true);
  }
  vector<int> rvec;
  for (int i = m / 2 + 1; i < m; i++) {
    rvec.push_back(cvec[i].second);
  }
  if (rvec.size() > 0) {
    dfs(rvec, 2 * k + 2, xaxis ^ true);
  }
}

void buildtree() {
  int N_ = X.size();
  int N = 1;
  while (N <= N_)
    N <<= 1;
  data.assign(2 * N - 1, -1);

  vector<int> vec;
  for (int i = 0; i < N_; i++)
    vec.push_back(i);

  dfs(vec, 0, true);
}

void query(int k, int sx, int tx, int sy, int ty, bool xaxis,
           vector<int> &ans) {
  if (data[k] == -1)
    return;

  if (xaxis) {
    if (sx <= X[data[k]] && X[data[k]] <= tx && sy <= Y[data[k]] &&
        Y[data[k]] <= ty)
      ans.push_back(data[k]);

    if (sx <= X[data[k]])
      query(2 * k + 1, sx, tx, sy, ty, xaxis ^ true, ans);
    if (X[data[k]] <= tx)
      query(2 * k + 2, sx, tx, sy, ty, xaxis ^ true, ans);
  } else {
    if (sx <= X[data[k]] && X[data[k]] <= tx && sy <= Y[data[k]] &&
        Y[data[k]] <= ty)
      ans.push_back(data[k]);

    if (sy <= Y[data[k]])
      query(2 * k + 1, sx, tx, sy, ty, xaxis ^ true, ans);
    if (Y[data[k]] <= ty)
      query(2 * k + 2, sx, tx, sy, ty, xaxis ^ true, ans);
  }
}

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

  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    X.push_back(x);
    Y.push_back(y);
  }

  buildtree();

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int sx, tx, sy, ty;
    cin >> sx >> tx >> sy >> ty;

    vector<int> ans;
    query(0, sx, tx, sy, ty, true, ans);
    sort(ans.begin(), ans.end());
    for (auto v : ans)
      cout << v << endl;
    cout << endl;

    //        SX.push_back(sx);
    //        TX.push_back(tx);
    //        SY.push_back(sy);
    //        TY.push_back(ty);
  }

  return 0;
}