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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"

struct po {
  int index;
  int coors[2];
  po(int _d) {
    index = 0;
    coors[0] = 0;
    coors[1] = 0;
  }
  po() {}
};
template <class T> class axisSorter {
  int k;

public:
  axisSorter(int _k) : k(_k) {}
  bool operator()(const T &a, const T &b) { return a.coors[k] < b.coors[k]; }
};
template <class T, int Dim = 2> struct kdtree {
public:
  T val;
  kdtree<T> *ltree, *rtree;
  int depth;
  int axis;
  kdtree(const T &p_) : val(p_), ltree(nullptr), rtree(nullptr) {}

  kdtree(vector<T> &ps_, const int &l, const int &r, const int depth_ = 0)
      : ltree(nullptr), rtree(nullptr), depth(depth_), axis(depth % Dim) {
    init(ps_, l, r);
  }
  vector<T> query(const T &amin, const T &amax) {
    vector<T> ans;
    bool aok = true;
    for (int i = 0; i < Dim; ++i) {
      if (amin.coors[i] <= val.coors[i] && val.coors[i] <= amax.coors[i]) {

      } else {
        aok = false;
        break;
      }
    }
    if (aok) {
      ans.emplace_back(val);
    }
    axisSorter<T> as(axis);
    if (as(val, amax) || val.coors[axis] == amax.coors[axis]) {
      if (rtree != nullptr) {
        vector<T> tans(rtree->query(amin, amax));
        ans.insert(ans.end(), tans.begin(), tans.end());
      }
    }
    if (as(amin, val) || val.coors[axis] == amin.coors[axis]) {
      if (ltree != nullptr) {
        vector<T> tans(ltree->query(amin, amax));
        ans.insert(ans.end(), tans.begin(), tans.end());
      }
    }
    return ans;
  }

private:
  void init(vector<T> &ps, const int &l, const int &r) {
    if (l >= r) {
      return;
    }
    sort(ps.begin() + l, ps.begin() + r, axisSorter<T>(axis));
    const int mid = (l + r) / 2;
    val = ps[mid];
    ltree = make_kdtree(ps, l, mid, depth + 1);
    rtree = make_kdtree(ps, mid + 1, r, depth + 1);
  }
};
template <class T>
kdtree<T> *make_kdtree(vector<T> &ps_, const int &l, const int &r,
                       const int &depth = 0) {
  if (l >= r)
    return nullptr;
  else {
    return new kdtree<T>(ps_, l, r, depth);
  }
}
int main() {
  vector<po> ps(0);
  {
    int N;
    scanf("%d", &N);
    for (int i = 0; i < N; ++i) {
      int x, y;
      scanf("%d", &x);
      scanf("%d", &y);
      po d(2);
      d.index = i;
      d.coors[0] = x;
      d.coors[1] = y;
      ps.push_back(d);
    }
  }

  kdtree<po> kd(ps, 0, ps.size());
  int M;
  scanf("%d", &M);
  for (int i = 0; i < M; ++i) {
    int sx, tx, sy, ty;
    scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
    po s(2);
    s.coors[0] = sx;
    s.coors[1] = sy;
    po t(2);
    t.coors[0] = tx;
    t.coors[1] = ty;
    vector<int> is;
    vector<po> anss(kd.query(s, t));
    for (auto a : anss) {
      is.emplace_back(a.index);
    }
    sort(is.begin(), is.end());
    for (auto i : is) {
      printf("%d", i);
      printf("\\n");
    }
    printf("\\n");
  }
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"

struct po {
  int index;
  vector<int> coors;
  po(int _d) : coors(_d) {}
  po() {}
};
template <class T> class axisSorter {
  int k;

public:
  axisSorter(int _k) : k(_k) {}
  bool operator()(const T &a, const T &b) { return a.coors[k] < b.coors[k]; }
};
template <class T, int Dim = 2> struct kdtree {
public:
  T val;
  kdtree<T> *ltree, *rtree;
  int depth;
  int axis;
  kdtree(const T &p_) : val(p_), ltree(nullptr), rtree(nullptr) {}

  kdtree(vector<T> &ps_, const int &l, const int &r, const int depth_ = 0)
      : ltree(nullptr), rtree(nullptr), depth(depth_), axis(depth % Dim) {
    init(ps_, l, r);
  }
  vector<T> query(const T &amin, const T &amax) {
    vector<T> ans;
    bool aok = true;
    for (int i = 0; i < Dim; ++i) {
      if (amin.coors[i] <= val.coors[i] && val.coors[i] <= amax.coors[i]) {

      } else {
        aok = false;
        break;
      }
    }
    if (aok) {
      ans.emplace_back(val);
    }
    axisSorter<T> as(axis);
    if (as(val, amax) || val.coors[axis] == amax.coors[axis]) {
      if (rtree != nullptr) {
        vector<T> tans(rtree->query(amin, amax));
        ans.insert(ans.end(), tans.begin(), tans.end());
      }
    }
    if (as(amin, val) || val.coors[axis] == amin.coors[axis]) {
      if (ltree != nullptr) {
        vector<T> tans(ltree->query(amin, amax));
        ans.insert(ans.end(), tans.begin(), tans.end());
      }
    }
    return ans;
  }

private:
  void init(vector<T> &ps, const int &l, const int &r) {
    if (l >= r) {
      return;
    }
    sort(ps.begin() + l, ps.begin() + r, axisSorter<T>(axis));
    const int mid = (l + r) / 2;
    val = ps[mid];
    ltree = make_kdtree(ps, l, mid, depth + 1);
    rtree = make_kdtree(ps, mid + 1, r, depth + 1);
  }
};
template <class T>
kdtree<T> *make_kdtree(vector<T> &ps_, const int &l, const int &r,
                       const int &depth = 0) {
  if (l >= r)
    return nullptr;
  else {
    return new kdtree<T>(ps_, l, r, depth);
  }
}
int main() {
  vector<po> ps(0);
  {
    int N;
    scanf("%d", &N);
    for (int i = 0; i < N; ++i) {
      int x, y;
      scanf("%d", &x);
      scanf("%d", &y);
      po d(2);
      d.index = i;
      d.coors = vector<int>{x, y};
      ps.push_back(d);
    }
  }

  kdtree<po> kd(ps, 0, ps.size());
  int M;
  scanf("%d", &M);
  for (int i = 0; i < M; ++i) {
    int sx, tx, sy, ty;
    scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
    po s(2);
    s.coors = vector<int>{sx, sy};
    po t(2);
    t.coors = vector<int>{tx, ty};
    vector<int> is;
    vector<po> anss(kd.query(s, t));
    for (auto a : anss) {
      is.emplace_back(a.index);
    }
    sort(is.begin(), is.end());
    for (auto i : is) {
      printf("%d", i);
      printf("\\n");
    }
    printf("\\n");
  }
  return 0;
}