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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using int64 = int64_t;

constexpr int64 MOD = 1000000007;

struct dat {
  int id;
  int z;

  dat() {}
  ~dat() {}

  dat(int a, int b) : id(a), z(b) {}

  bool operator<(const dat &o) const { return z < o.z; }
};

int N, M;
int tx[5000], ty[5000];

dat arr_x[5000];
dat arr_y[5000];

int check[5000];

int solve(int x1, int y1, int x2, int y2) {
  fill(check, check + N, 0);

  for (dat *itx = lower_bound(arr_x, arr_x + N, dat(-1, x1));
       itx < arr_x + N && itx->z <= x2; ++itx) {
    ++check[itx->id];
  }

  int ans = 0;
  for (dat *ity = lower_bound(arr_y, arr_y + N, dat(-1, y1));
       ity < arr_y + N && ity->z <= y2; ++ity) {
    ans += check[ity->id];
  }

  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  for (int j = 0; j < N; ++j) {
    cin >> tx[j] >> ty[j];
  }

  // initialize
  for (int j = 0; j < N; ++j) {
    arr_x[j] = dat(j, tx[j]);
    arr_y[j] = dat(j, ty[j]);
  }
  sort(arr_x, arr_x + N);
  sort(arr_y, arr_y + N);

  for (int j = 0; j < M; ++j) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    cout << solve(x1, y1, x2, y2) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using int64 = int64_t;

constexpr int64 MOD = 1000000007;

struct dat {
  int id;
  int z;

  dat() {}
  ~dat() {}

  dat(int a, int b) : id(a), z(b) {}

  bool operator<(const dat &o) const { return z < o.z; }
};

int N, M;
int tx[5000], ty[5000];

dat arr_x[5000];
dat arr_y[5000];

int check[5000];

int solve(int x1, int y1, int x2, int y2) {
  fill(check, check + N, 0);

  for (dat *itx = lower_bound(arr_x, arr_x + N, dat(-1, x1));
       itx < arr_x + N && itx->z <= x2; ++itx) {
    ++check[itx->id];
  }

  for (dat *ity = lower_bound(arr_y, arr_y + N, dat(-1, y1));
       ity < arr_y + N && ity->z <= y2; ++ity) {
    ++check[ity->id];
  }

  int ans = 0;
  for (int j = 0; j < N; ++j) {
    ans += check[j] >> 1;
  }
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  for (int j = 0; j < N; ++j) {
    cin >> tx[j] >> ty[j];
  }

  // initialize
  for (int j = 0; j < N; ++j) {
    arr_x[j] = dat(j, tx[j]);
    arr_y[j] = dat(j, ty[j]);
  }
  sort(arr_x, arr_x + N);
  sort(arr_y, arr_y + N);

  for (int j = 0; j < M; ++j) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    cout << solve(x1, y1, x2, y2) << endl;
  }
  return 0;
}