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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    cout << val << " ";                                                        \\
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

#define N 500007

struct Point {
  int x, y, i;
};

int n;
Point p[N];

bool comp_x(const Point &p1, const Point &p2) { return p1.x < p2.x; }
bool comp_y(const Point &p1, const Point &p2) { return p1.y < p2.y; }

struct Node {
  Node *left, *right;
  Point p;

  Node(Node *left, Node *right, Point p) : left(left), right(right), p(p) {}
};

Node *root;

Node *make(int l, int r, int depth) {
  if (!(l < r)) {
    return nullptr;
  }

  int mid = (l + r) >> 1;
  if (depth % 2) {
    sort(p + l, p + r, comp_x);
  } else {
    sort(p + l, p + r, comp_y);
  }

  return new Node(make(l, mid, depth + 1), make(mid + 1, r, depth + 1), p[mid]);
}

void find(Node *nd, vector<int> &result, int sx, int tx, int sy, int ty,
          int depth) {
  Point &p = nd->p;
  if (sx <= p.x && p.x <= tx && sy <= p.y && p.y <= ty) {
    result.push_back(p.i);
  }

  if (depth % 2) {
    if (nd->left != nullptr && sx <= p.x) {
      find(nd->left, result, sx, tx, sy, ty, depth + 1);
    }
    if (nd->right != nullptr && p.x <= tx) {
      find(nd->right, result, sx, tx, sy, ty, depth + 1);
    }
  } else {
    if (nd->left != nullptr && sy <= p.y) {
      find(nd->left, result, sx, tx, sy, ty, depth + 1);
    }
    if (nd->right != nullptr && p.y <= ty) {
      find(nd->right, result, sx, tx, sy, ty, depth + 1);
    }
  }
}

int main() {
  scanf("%d", &n);
  rep(i, n) {
    scanf("%d %d", &p[i].x, &p[i].y);
    p[i].i = i;
  }
  root = make(0, n, 0);

  int q;
  cin >> q;
  rep(i, q) {
    int sx, tx, sy, ty;
    cin >> sx >> tx >> sy >> ty;
    vector<int> result;
    find(root, result, sx, tx, sy, ty, 0);
    sort(ALL(result));
    repe(v, result) { printf("%d\\n", v); }
    printf("\\n");
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    cout << val << " ";                                                        \\
  cout << endl
#define DEBREPE(obj)                                                           \\
  for (auto e : obj)                                                           \\
    cout << e << " ";                                                          \\
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

#define N 1000003

vector<P> data[N];
class kdTree {
private:
  int n;

  void init(int k, int l, int r, P *d) {
    if (r - l == 1) {
      data[k].push_back(d[l]);
    } else {
      int lch = 2 * k + 1, rch = 2 * k + 2;
      init(lch, l, (l + r) / 2, d);
      init(rch, (l + r) / 2, r, d);
      data[k].resize(r - l);

      merge(data[lch].begin(), data[lch].end(), data[rch].begin(),
            data[rch].end(), data[k].begin());
    }
  }
  int r_query(int a, int b, int x0, int x1, int k, int l, int r,
              set<int> *result) {
    if (b <= l || r <= a) {
      return 0;
    } else if (a <= l && r <= b) {
      vector<P>::iterator it0 =
          upper_bound(data[k].begin(), data[k].end(), P(x0, -1));
      vector<P>::iterator it1 =
          upper_bound(data[k].begin(), data[k].end(), P(x1, -1));
      int sz = it1 - it0;
      while (it0 != it1) {
        (*result).insert((*it0).second);
        ++it0;
      }
      return sz;
    } else {
      int lc = r_query(a, b, x0, x1, 2 * k + 1, l, (l + r) / 2, result);
      int rc = r_query(a, b, x0, x1, 2 * k + 2, (l + r) / 2, r, result);

      return lc + rc;
    }
  }

public:
  kdTree(int n, P *d) {
    this->n = n;
    init(0, 0, n, d);
  }
  // [a, b) * [x0, x1)
  int query(int a, int b, int x0, int x1, set<int> *result) {
    return r_query(a, b, x0, x1, 0, 0, n, result);
  }
};

IP ps[N];
int xs[N];
P ys[N];

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    ps[i] = IP(x, P(y, i));
  }
  sort(ps, ps + n);
  repr(i, n) {
    IP &p = ps[i];
    xs[i] = p.first;
    ys[i] = p.second;
  }
  kdTree kdt(n, ys);

  int q;
  cin >> q;
  while (q--) {
    int sx, tx, sy, ty;
    cin >> sx >> tx >> sy >> ty;
    int left = lower_bound(xs, xs + n, sx) - xs;
    int right = lower_bound(xs, xs + n, tx + 1) - xs;
    set<int> ss;
    kdt.query(left, right, sy, ty + 1, &ss);

    for (set<int>::iterator it = ss.begin(); it != ss.end(); ++it) {
      cout << *it << endl;
    }
    cout << endl;
  }
  return 0;
}