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

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

class Imos {
public:
  int H, W;
  vector<vector<int>> d;

public:
  // 1-indexed
  Imos(vector<vector<int>> &field) {
    H = field.size();
    W = field[0].size();
    d = vector<vector<int>>(H + 1, vector<int>(W + 1));

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        d[i + 1][j + 1] = d[i + 1][j] + d[i][j + 1] - d[i][j] + field[i][j];
      }
    }
  }
  int area(int h1, int w1, int h2, int w2) {
    // size of area s.t. [h1, h2) and [w1, w2)
    return d[h2][w2] - d[h2][w1] - d[h1][w2] + d[h1][w1];
  }
};

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  // cout.precision(16);

  int N, M;
  cin >> N >> M;

  vector<int> xs(N), ys(N);
  vector<pair<int, int>> p(N);

  for (int i = 0; i < N; i++) {
    scanf("%d %d", &xs[i], &ys[i]);
    p[i] = make_pair(xs[i], ys[i]);
  }

  sort(xs.begin(), xs.end());
  sort(ys.begin(), ys.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  ys.erase(unique(ys.begin(), ys.end()), ys.end());

  vector<vector<int>> d(xs.size(), vector<int>(ys.size()));
  for (int i = 0; i < N; i++) {
    int x = p[i].first, y = p[i].second;
    int xi = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
    int yi = lower_bound(ys.begin(), ys.end(), y) - ys.begin();

    d[xi][yi]++;
  }

  Imos imos = Imos(d);

  while (M--) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

    int x1i = lower_bound(xs.begin(), xs.end(), x1) - xs.begin();
    int y1i = lower_bound(ys.begin(), ys.end(), y1) - ys.begin();
    int x2i = upper_bound(xs.begin(), xs.end(), x2) - xs.begin();
    int y2i = upper_bound(ys.begin(), ys.end(), y2) - ys.begin();

    printf("%d\\n", imos.area(x1i, y1i, x2i, y2i));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int xs[5000];
int ys[5000];

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  // cout.precision(16);

  int N, M;
  cin >> N >> M;

  for (int i = 0; i < N; i++)
    scanf("%d %d", xs + i, ys + i);

  while (M--) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    int count = 0;
    for (int i = 0; i < N; i++)
      count += (x1 <= xs[i] && xs[i] <= x2 && y1 <= ys[i] && ys[i] <= y2);

    printf("%d\\n", count);
  }
  return 0;
}