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

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define allof(a) a.begin(), a.end()

typedef pair<int, int> pii;

int n, m, X[5050], Y[5050], X1[500050], X2[500050], Y1[500050], Y2[500050];

int compress(int *x) {
  vector<int> xs;

  for_(i, 0, n) xs.push_back(x[i]);

  sort(allof(xs));
  xs.erase(unique(allof(xs)), xs.end());

  for_(i, 0, n) x[i] = find(allof(xs), x[i]) - xs.begin();

  return xs.size();
}

int main() {
  cin >> n >> m;

  vector<pii> vpx, vpy;

  for_(i, 0, n) {
    scanf("%d %d", &X[i], &Y[i]);
    vpx.push_back(pii(X[i], i));
    vpy.push_back(pii(Y[i], i));
  }

  sort(allof(vpx));
  sort(allof(vpy));

  vector<int> vx, vy;
  for_(i, 0, n) {
    vx.push_back(vpx[i].first);
    vy.push_back(vpy[i].first);
  }

  for_(i, 0, m) {
    int x1, x2, y1, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

    int ind = lower_bound(allof(vx), x1) - vx.begin();
    X1[i] = ind < n ? vpx[ind].second : n;

    ind = upper_bound(allof(vx), x2) - vx.begin() - 1;
    X2[i] = ind >= 0 ? vpx[ind].second : -1;

    ind = lower_bound(allof(vy), y1) - vy.begin();
    Y1[i] = ind < n ? vpy[ind].second : n;

    ind = upper_bound(allof(vy), y2) - vy.begin() - 1;
    Y2[i] = ind >= 0 ? vpy[ind].second : -1;
  }

  int W = compress(X), H = compress(Y);
  vector<vector<int>> grid(H + 1, vector<int>(W + 1, 0));

  for_(i, 0, n)++ grid[Y[i]][X[i]];

  for_(y, 0, H + 1) for_(x, 0, W + 1) {
    if (y > 0)
      grid[y][x] += grid[y - 1][x];
    if (x > 0)
      grid[y][x] += grid[y][x - 1];
    if (y > 0 && x > 0)
      grid[y][x] -= grid[y - 1][x - 1];
  }

  for_(i, 0, m) {
    int ans = 0;

    if (X1[i] < n && X2[i] >= 0 && Y1[i] < n && Y2[i] >= 0) {
      int x1 = X[X1[i]], x2 = X[X2[i]], y1 = Y[Y1[i]], y2 = Y[Y2[i]];
      ans += grid[y2][x2];
      if (x1 - 1 >= 0)
        ans -= grid[y2][x1 - 1];
      if (y1 - 1 >= 0)
        ans -= grid[y1 - 1][x2];
      if (x1 - 1 >= 0 && y1 - 1 >= 0)
        ans += grid[y1 - 1][x1 - 1];
    }

    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define allof(a) a.begin(), a.end()

int n, m, X1[505050], X2[505050], Y1[505050], Y2[505050];

int compress(int *x1, int *x2, int w) {
  vector<int> xs;

  for_(i, 0, n + m) {
    for_(d, -1, 2) {
      int tx1 = x1[i] + d, tx2 = x2[i] + d;
      if (-w <= tx1 && tx1 <= w)
        xs.push_back(tx1);
      if (-w <= tx2 && tx2 <= w)
        xs.push_back(tx2);
    }
  }

  sort(allof(xs));
  xs.erase(unique(allof(xs)), xs.end());

  for_(i, 0, n + m) {
    x1[i] = find(allof(xs), x1[i]) - xs.begin();
    x2[i] = find(allof(xs), x2[i]) - xs.begin();
  }

  return xs.size();
}

int main() {
  cin >> n >> m;

  for_(i, 0, n) {
    cin >> X1[i] >> Y1[i];
    X2[i] = X1[i];
    Y2[i] = Y1[i];
  }

  for_(i, 0, m) cin >> X1[i + n] >> Y1[i + n] >> X2[i + n] >> Y2[i + n];

  int W = compress(X1, X2, (int)1e9), H = compress(Y1, Y2, (int)1e9);
  vector<vector<int>> grid(H + 1, vector<int>(W + 1, 0));

  for_(i, 0, n)++ grid[Y1[i]][X1[i]];

  for_(y, 0, H + 1) for_(x, 0, W + 1) {
    if (y > 0)
      grid[y][x] += grid[y - 1][x];
    if (x > 0)
      grid[y][x] += grid[y][x - 1];
    if (y > 0 && x > 0)
      grid[y][x] -= grid[y - 1][x - 1];
  }

  for_(i, 0, m) {
    int x1 = X1[i + n], x2 = X2[i + n], y1 = Y1[i + n], y2 = Y2[i + n];
    int ans = grid[y2][x2];
    if (x1 - 1 >= 0)
      ans -= grid[y2][x1 - 1];
    if (y1 - 1 >= 0)
      ans -= grid[y1 - 1][x2];
    if (x1 - 1 >= 0 && y1 - 1 >= 0)
      ans += grid[y1 - 1][x1 - 1];
    cout << ans << endl;
  }

  return 0;
}