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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  int x, y;
};

int **table;

int main() {

  int N, M;
  scanf("%d %d", &N, &M);

  Info info[N];

  vector<int> First_X, First_Y;

  First_X.push_back(-BIG_NUM);
  First_Y.push_back(-BIG_NUM);
  First_X.push_back(BIG_NUM);
  First_Y.push_back(BIG_NUM);

  for (int i = 0; i < N; i++) {
    scanf("%d %d", &info[i].x, &info[i].y);

    First_X.push_back(info[i].x);
    First_Y.push_back(info[i].y);
  }

  sort(First_X.begin(), First_X.end());
  sort(First_Y.begin(), First_Y.end());

  First_X.erase(unique(First_X.begin(), First_X.end()), First_X.end());
  First_Y.erase(unique(First_Y.begin(), First_Y.end()), First_Y.end());

  int H = (int)First_Y.size(), W = (int)First_X.size();

  table = new int *[H];
  for (int row = 0; row < H; row++) {
    table[row] = new int[W];
    for (int col = 0; col < W; col++) {
      table[row][col] = 0;
    }
  }

  int tmp_x, tmp_y;

  for (int i = 0; i < N; i++) {

    for (int k = 0; k < W; k++) {
      if (First_X[k] == info[i].x) {
        tmp_x = k;
        break;
      }
    }

    for (int k = 0; k < H; k++) {
      if (First_Y[k] == info[i].y) {
        tmp_y = k;
        break;
      }
    }

    table[tmp_y][tmp_x]++;
  }

  for (int row = 0; row < H; row++) {
    for (int col = 1; col < W; col++) {
      table[row][col] += table[row][col - 1];
    }
  }

  for (int col = 0; col < W; col++) {
    for (int row = 1; row < H; row++) {
      table[row][col] += table[row - 1][col];
    }
  }

  int x1, y1, x2, y2;
  int calc_x1, calc_y1, calc_x2, calc_y2;
  int left, right, m;

  for (int loop = 0; loop < M; loop++) {
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

    calc_y1 = 0;
    left = 0, right = H - 1, m = (left + right) / 2;

    while (left <= right) {
      if (First_Y[m] >= y1) {
        calc_y1 = m;
        right = m - 1;
      } else {
        left = m + 1;
      }
      m = (left + right) / 2;
    }

    calc_y1--;

    calc_y2 = H - 1;
    left = 0, right = H - 1, m = (left + right) / 2;

    while (left <= right) {
      if (First_Y[m] <= y2) {
        calc_y2 = m;
        left = m + 1;
      } else {
        right = m - 1;
      }
      m = (left + right) / 2;
    }

    calc_x1 = 0;
    left = 0, right = W - 1, m = (left + right) / 2;

    while (left <= right) {
      if (First_X[m] >= x1) {
        calc_x1 = m;
        right = m - 1;
      } else {
        left = m + 1;
      }
      m = (left + right) / 2;
    }

    calc_x1--;

    calc_x2 = W - 1;
    left = 0, right = W - 1, m = (left + right) / 2;

    while (left <= right) {
      if (First_X[m] <= x2) {
        calc_x2 = m;
        left = m + 1;
      } else {
        right = m - 1;
      }
      m = (left + right) / 2;
    }

    int A, B, C;
    if (calc_y1 >= 0) {
      A = table[calc_y1][calc_x2];
    } else {
      A = 0;
    }

    if (calc_x1 >= 0) {
      B = table[calc_y2][calc_x1];
    } else {
      B = 0;
    }

    if (calc_y1 >= 0 && calc_x1 >= 0) {
      C = table[calc_y1][calc_x1];
    } else {
      C = 0;
    }

    printf("%d\\n", table[calc_y2][calc_x2] - A - B + C);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Node {
  int location;
  int p, left, right;
};

struct Point {
  Point() { id = x = y = 0; }

  Point(int arg_id, int arg_x, int arg_y) {
    id = arg_id;
    x = arg_x;
    y = arg_y;
  }
  bool operator<(const Point &arg) const { return id < arg.id; }
  void print() { printf("%d\\n", id); }
  int id, x, y;
};

static const int MAX = 10000;
static const int NIL = -BIG_NUM;

int N;
Point P[MAX];
Node T[MAX];
int number;

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

int makeKDTree(int left, int right, int depth) {

  if (!(left < right))
    return NIL;

  int mid = (left + right) / 2;

  int node_id = number++;

  if (depth % 2 == 0) {
    sort(P + left, P + right, lessX);
  } else {
    sort(P + left, P + right, lessY);
  }

  T[node_id].location = mid;
  T[node_id].left = makeKDTree(left, mid, depth + 1);
  T[node_id].right = makeKDTree(mid + 1, right, depth + 1);

  return node_id;
}

void find(int node_id, int sx, int tx, int sy, int ty, int depth,
          vector<Point> &ans) {

  int x = P[T[node_id].location].x;
  int y = P[T[node_id].location].y;

  if (sx <= x && x <= tx && sy <= y && y <= ty) {
    ans.push_back(P[T[node_id].location]);
  }

  if (depth % 2 == 0) {
    if (T[node_id].left != NIL) {
      if (sx <= x)
        find(T[node_id].left, sx, tx, sy, ty, depth + 1, ans);
    }
    if (T[node_id].right != NIL) {
      if (x <= tx)
        find(T[node_id].right, sx, tx, sy, ty, depth + 1, ans);
    }
  } else {
    if (T[node_id].left != NIL) {
      if (sy <= y)
        find(T[node_id].left, sx, tx, sy, ty, depth + 1, ans);
    }
    if (T[node_id].right != NIL) {
      if (y <= ty)
        find(T[node_id].right, sx, tx, sy, ty, depth + 1, ans);
    }
  }
}

int main() {

  int M;

  int x, y;
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &x, &y);
    P[i] = Point(i, x, y);
    T[i].left = T[i].right = T[i].p = NIL;
  }

  number = 0;

  int root = makeKDTree(0, N, 0);

  int sx, tx, sy, ty;
  vector<Point> ans;
  for (int i = 0; i < M; i++) {
    scanf("%d %d %d %d", &sx, &sy, &tx, &ty);
    ans.clear();
    find(root, sx, tx, sy, ty, 0, ans);
    // sort(ans.begin(),ans.end());
    printf("%lld\\n", ans.size());
  }
  return 0;
}