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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Point {
public:
  int id;
  int x;
  int y;
  Point() {}
  Point(int i, int j, int k) : id(i), x(j), y(k) {}
};

bool cmpX(Point a, Point b) { return a.x < b.x; }

bool cmpY(Point a, Point b) { return a.y < b.y; }

class RangeSearch {
public:
  int n;
  vector<Point> d;
  int solve();
  RangeSearch() {}
  RangeSearch(int i) : n(i) {}
  void mk2dTree(int st, int ed, int dp);
  void findPoint(int sx, int tx, int sy, int ty, int st, int ed, int dp,
                 set<int> &ret);
};

void RangeSearch::mk2dTree(int st, int ed, int dp) {
  if (st + 2 > ed)
    return;

  int md = (st + ed) / 2;

  if (dp % 2 == 0)
    sort(d.begin() + st, d.begin() + ed, cmpX);
  else
    sort(d.begin() + st, d.begin() + ed, cmpY);

  mk2dTree(st, md, dp + 1);
  mk2dTree(md + 1, ed, dp + 1);
}

void RangeSearch::findPoint(int sx, int tx, int sy, int ty, int st, int ed,
                            int dp, set<int> &ret) {
  if (st + 1 > ed)
    return;

  int mid = (st + ed) / 2;

  if (sx <= d[mid].x && tx >= d[mid].x && sy <= d[mid].y && ty >= d[mid].y)
    ret.insert(d[mid].id);

  if (dp % 2 == 0) {
    if (d[mid].x >= sx)
      findPoint(sx, tx, sy, ty, st, mid, dp + 1, ret);
    if (d[mid].x <= tx)
      findPoint(sx, tx, sy, ty, mid + 1, ed, dp + 1, ret);
  } else {
    if (d[mid].y >= sy)
      findPoint(sx, tx, sy, ty, st, mid, dp + 1, ret);
    if (d[mid].y <= ty)
      findPoint(sx, tx, sy, ty, mid + 1, ed, dp + 1, ret);
  }
}

int RangeSearch::solve() {
  for (auto i = 0; i < n; ++i)
    cout << " " << d[i].x;
  cout << endl;
  for (auto i = 0; i < n; ++i)
    cout << " " << d[i].y;
  cout << endl;

  return 0;
}

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

  int n;
  // cin >> n;
  scanf("%d\\n", &n);

  RangeSearch rs(n);

  for (auto i = 0; i < n; ++i) {
    int x, y;
    //      cin >> x >> y;
    scanf("%d %d\\n", &x, &y);
    Point p(i, x, y);
    rs.d.push_back(p);
  }

  rs.mk2dTree(0, n, 0);

  int q;
  // cin >> q;
  scanf("%d\\n", &q);

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

    set<int> ret;
    rs.findPoint(sx, tx, sy, ty, 0, n, 0, ret);

    for (auto it = ret.begin(); it != ret.end(); ++it)
      // cout << *it << endl;
      printf("%d\\n", *it);
    // cout << endl;
    printf("\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Point {
public:
  int id;
  int x;
  int y;
  Point() {}
  Point(int i, int j, int k) : id(i), x(j), y(k) {}
};

bool cmpX(Point a, Point b) { return a.x < b.x; }

bool cmpY(Point a, Point b) { return a.y < b.y; }

class RangeSearch {
public:
  int n;
  vector<Point> d;
  int solve();
  RangeSearch() {}
  RangeSearch(int i) : n(i) {}
  void mk2dTree(int st, int ed, int dp);
  void findPoint(int sx, int tx, int sy, int ty, int st, int ed, int dp,
                 set<int> &ret);
};

void RangeSearch::mk2dTree(int st, int ed, int dp) {
  if (st + 2 > ed)
    return;

  int md = (st + ed) / 2;

  if (dp % 2 == 0)
    sort(d.begin() + st, d.begin() + ed, cmpX);
  else
    sort(d.begin() + st, d.begin() + ed, cmpY);

  mk2dTree(st, md, dp + 1);
  mk2dTree(md + 1, ed, dp + 1);
}

void RangeSearch::findPoint(int sx, int tx, int sy, int ty, int st, int ed,
                            int dp, set<int> &ret) {
  if (st + 1 > ed)
    return;

  int mid = (st + ed) / 2;

  if (sx <= d[mid].x && tx >= d[mid].x && sy <= d[mid].y && ty >= d[mid].y)
    ret.insert(d[mid].id);

  if (dp % 2 == 0) {
    if (d[mid].x >= sx)
      findPoint(sx, tx, sy, ty, st, mid, dp + 1, ret);
    if (d[mid].x <= tx)
      findPoint(sx, tx, sy, ty, mid + 1, ed, dp + 1, ret);
  } else {
    if (d[mid].y >= sy)
      findPoint(sx, tx, sy, ty, st, mid, dp + 1, ret);
    if (d[mid].y <= ty)
      findPoint(sx, tx, sy, ty, mid + 1, ed, dp + 1, ret);
  }
}

int RangeSearch::solve() {
  for (auto i = 0; i < n; ++i)
    cout << " " << d[i].x;
  cout << endl;
  for (auto i = 0; i < n; ++i)
    cout << " " << d[i].y;
  cout << endl;

  return 0;
}

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

  int n;
  cin >> n;

  RangeSearch rs(n);

  for (auto i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    Point p(i, x, y);
    rs.d.push_back(p);
  }

  rs.mk2dTree(0, n, 0);

  int q;
  cin >> q;

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

    set<int> ret;
    rs.findPoint(sx, tx, sy, ty, 0, n, 0, ret);

    for (auto it = ret.begin(); it != ret.end(); ++it)
      cout << *it << endl;
    cout << endl;
  }

  return 0;
}