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

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int dp[5001][5001];

int get(const vector<int> &xs, const vector<int> &ys, int x, int y) {
  if (*xs.begin() > x || *ys.begin() > y)
    return 0;
  int px = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
  int py = lower_bound(ys.begin(), ys.end(), y) - ys.begin();
  if (lower_bound(xs.begin(), xs.end(), x) == xs.end() ||
      *lower_bound(xs.begin(), xs.end(), x) != x)
    px--;
  if (lower_bound(ys.begin(), ys.end(), y) == ys.end() ||
      *lower_bound(ys.begin(), ys.end(), y) != y)
    py--;
  // cout<<x<<" "<<y<<" "<<px<<" "<<py<<endl;
  return dp[py][px];
}

int main() {
  int n, m;
  cin >> n >> m;
  set<int> xs, ys;
  vector<int> inx(n), iny(n);
  for (int i = 0; i < n; ++i) {
    cin >> inx[i] >> iny[i];
    xs.insert(inx[i]);
    ys.insert(iny[i]);
  }
  map<int, int> xmap, ymap;
  int cnt = 0;
  vector<int> vx, vy;
  for (set<int>::iterator x = xs.begin(); x != xs.end(); ++x) {
    xmap[*x] = cnt++;
    vx.push_back(*x);
  }
  cnt = 0;
  for (set<int>::iterator y = ys.begin(); y != ys.end(); ++y) {
    ymap[*y] = cnt++;
    vy.push_back(*y);
  }
  for (int i = 0; i < n; ++i)
    dp[ymap[iny[i]]][xmap[inx[i]]] += 1;

  const int ny = ys.size();
  const int nx = xs.size();
  for (int i = 0; i < ny; ++i)
    for (int j = 0; j < nx; ++j) {
      int r = i ? dp[i - 1][j] : 0;
      r += j ? dp[i][j - 1] : 0;
      r -= (i > 0 && j > 0) ? dp[i - 1][j - 1] : 0;
      dp[i][j] += r;
    }

  for (int i = 0; i < m; ++i) {
    int x1, y1, x2, y2;
    cin >> x2 >> y2 >> x1 >> y1;
    x2--, y2--;
    int ans = get(vx, vy, x1, y1) - get(vx, vy, x2, y1) - get(vx, vy, x1, y2) +
              get(vx, vy, x2, y2);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int dp[5001][5001];

int get(const set<int> &xs, const set<int> &ys, int x, int y) {
  if (*xs.begin() > x || *ys.begin() > y)
    return 0;
  int px = distance(xs.begin(), xs.lower_bound(x));
  int py = distance(ys.begin(), ys.lower_bound(y));
  if (xs.lower_bound(x) == xs.end() || *xs.lower_bound(x) != x)
    px--;
  if (ys.lower_bound(y) == ys.end() || *ys.lower_bound(y) != y)
    py--;
  // cout<<x<<" "<<y<<" "<<px<<" "<<py<<endl;
  return dp[py][px];
}

int main() {
  int n, m;
  cin >> n >> m;
  set<int> xs, ys;
  vector<int> inx(n), iny(n);
  for (int i = 0; i < n; ++i) {
    cin >> inx[i] >> iny[i];
    xs.insert(inx[i]);
    ys.insert(iny[i]);
  }
  map<int, int> xmap, ymap;
  int cnt = 0;
  for (set<int>::iterator x = xs.begin(); x != xs.end(); ++x)
    xmap[*x] = cnt++;
  cnt = 0;
  for (set<int>::iterator y = ys.begin(); y != ys.end(); ++y)
    ymap[*y] = cnt++;
  for (int i = 0; i < n; ++i)
    dp[ymap[iny[i]]][xmap[inx[i]]] += 1;

  const int ny = ys.size();
  const int nx = xs.size();
  for (int i = 0; i < ny; ++i)
    for (int j = 0; j < nx; ++j) {
      int r = i ? dp[i - 1][j] : 0;
      r += j ? dp[i][j - 1] : 0;
      r -= (i > 0 && j > 0) ? dp[i - 1][j - 1] : 0;
      dp[i][j] += r;
    }

  for (int i = 0; i < m; ++i) {
    int x1, y1, x2, y2;
    cin >> x2 >> y2 >> x1 >> y1;
    x2--, y2--;
    int ans = get(xs, ys, x1, y1) - get(xs, ys, x2, y1) - get(xs, ys, x1, y2) +
              get(xs, ys, x2, y2);
    cout << ans << endl;
  }
}