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

// 27
// 19
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int x[5000], y[5000];
  vector<int> xp(n), yp(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    xp[i] = x[i];
    yp[i] = y[i];
  }
  sort(xp.begin(), xp.end());
  sort(yp.begin(), yp.end());
  xp.erase(unique(xp.begin(), xp.end()), xp.end());
  yp.erase(unique(yp.begin(), yp.end()), yp.end());
  vector<vector<int>> g(yp.size(), vector<int>(xp.size()));
  for (int i = 0; i < n; i++) {
    g[lower_bound(yp.begin(), yp.end(), y[i]) - yp.begin()]
     [lower_bound(xp.begin(), xp.end(), x[i]) - xp.begin()]++;
  }
  vector<vector<int>> ag(yp.size() + 1, vector<int>(xp.size() + 1));
  for (int i = 0; i < yp.size(); i++) {
    for (int j = 0; j < xp.size(); j++) {
      ag[i + 1][j + 1] = ag[i][j + 1] + ag[i + 1][j] - ag[i][j] + g[i][j];
    }
  }
  while (m--) {
    int xl, yl, xh, yh;
    cin >> xl >> yl >> xh >> yh;
    int xxl, yyl, xxh, yyh;
    xxl = lower_bound(xp.begin(), xp.end(), xl) - xp.begin();
    yyl = lower_bound(yp.begin(), yp.end(), yl) - yp.begin();
    xxh = upper_bound(xp.begin(), xp.end(), xh) - xp.begin();
    yyh = upper_bound(yp.begin(), yp.end(), yh) - yp.begin();
    cout << ag[yyh][xxh] - ag[yyh][xxl] - ag[yyl][xxh] + ag[yyl][xxl] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 27
// 19
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int x[5000], y[5000];
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  for (int i = 0; i < m; i++) {
    int r = 0;
    int xi[2], yi[2];
    cin >> xi[0] >> yi[0] >> xi[1] >> yi[1];
    for (int j = 0; j < n; j++) {
      r += xi[0] <= x[j] && x[j] <= xi[1] && yi[0] <= y[j] && y[j] <= yi[1];
    }
    cout << r << endl;
  }
  return 0;
}

int main2() {
  int n, m;
  cin >> n >> m;
  int x[5000], y[5000];
  vector<int> xp(n), yp(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    xp[i] = x[i];
    yp[i] = y[i];
  }
  sort(xp.begin(), xp.end());
  sort(yp.begin(), yp.end());
  xp.erase(unique(xp.begin(), xp.end()), xp.end());
  yp.erase(unique(yp.begin(), yp.end()), yp.end());
  vector<vector<int>> g(yp.size(), vector<int>(xp.size()));
  for (int i = 0; i < n; i++) {
    g[lower_bound(yp.begin(), yp.end(), y[i]) - yp.begin()]
     [lower_bound(xp.begin(), xp.end(), x[i]) - xp.begin()]++;
  }
  vector<vector<int>> ag(yp.size() + 1, vector<int>(xp.size() + 1));
  for (int i = 0; i < yp.size(); i++) {
    for (int j = 0; j < xp.size(); j++) {
      ag[i + 1][j + 1] = ag[i][j + 1] + ag[i + 1][j] - ag[i][j] + g[i][j];
    }
  }
  while (m--) {
    int xl, yl, xh, yh;
    cin >> xl >> yl >> xh >> yh;
    int xxl, yyl, xxh, yyh;
    xxl = lower_bound(xp.begin(), xp.end(), xl) - xp.begin();
    yyl = lower_bound(yp.begin(), yp.end(), yl) - yp.begin();
    xxh = upper_bound(xp.begin(), xp.end(), xh) - xp.begin();
    yyh = upper_bound(yp.begin(), yp.end(), yh) - yp.begin();
    cout << ag[yyh][xxh] - ag[yyh][xxl] - ag[yyl][xxh] + ag[xxl][yyl] << endl;
  }
  return 0;
}