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

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

const int My_Inf = 2147483647;
const long long int My_LInf = 9223372036854775807;

int num[5001][5001];
int sum[5001][5001];

int main() {
  memset(num, 0, sizeof(num));
  memset(sum, 0, sizeof(num));
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> tres;
  vector<int> xs, ys;
  for (int i = 0; i < N; ++i) {
    int x, y;
    cin >> x >> y;
    tres.push_back(make_pair(x, y));
    xs.push_back(x);
    ys.push_back(y);
  }
  sort(xs.begin(), xs.end());
  xs.resize(unique(xs.begin(), xs.end()) - xs.begin());
  sort(ys.begin(), ys.end());
  ys.resize(unique(ys.begin(), ys.end()) - ys.begin());
  for (int i = 0; i < N; ++i) {
    tres[i].first =
        lower_bound(xs.begin(), xs.end(), tres[i].first) - xs.begin();
    tres[i].second =
        lower_bound(ys.begin(), ys.end(), tres[i].second) - ys.begin();
  }
  for (int i = 0; i < N; ++i) {
    num[tres[i].first][tres[i].second]++;
  }
  for (int i = 0; i < 5000; ++i) {
    for (int j = 0; j < 5000; ++j) {
      sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + num[i][j];
    }
  }

  for (int i = 0; i < M; ++i) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 = lower_bound(xs.begin(), xs.end(), x1) - xs.begin();
    x2 = upper_bound(xs.begin(), xs.end(), x2) - xs.begin();
    y1 = lower_bound(ys.begin(), ys.end(), y1) - ys.begin();
    y2 = upper_bound(ys.begin(), ys.end(), y2) - ys.begin();
    int ans = sum[x2][y2] - sum[x2][y1] - sum[x1][y2] + sum[x1][y1];
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

// 21:24
struct Zahyou {
  int id;
  int x;
  int y;
};
struct a {
  int id;
  int num;
};
// struct X{
//	int id;
//	int x;
// };
// struct Y{
//	int id;
//	int x;
// };
int main() {
  int n, m;
  cin >> n >> m;
  vector<a> IXCol;
  vector<a> IYCol;
  vector<int> XCol;
  vector<int> YCol;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    IXCol.push_back({i, x});
    IYCol.push_back({i, y});
  }
  sort(IXCol.begin(), IXCol.end(), [](a a1, a a2) { return a1.num < a2.num; });
  sort(IYCol.begin(), IYCol.end(), [](a a1, a a2) { return a1.num < a2.num; });
  for (int i = 0; i < n; ++i) {
    XCol.push_back(IXCol[i].num);
    YCol.push_back(IYCol[i].num);
  }
  // sort(ZaCol.begin(), ZaCol.end(), [](Zahyou Z1, Zahyou Z2){return Z1.x ==
  // Z2.x ? Z1.y > Z2.y:Z1.x > Z2.x; }); sort(XCol.begin(), XCol.end(),
  // [](Zahyou Z1, Zahyou Z2){return Z1.x > Z2.x; }); sort(YCol.begin(),
  // YCol.end(), [](Zahyou Z1, Zahyou Z2){return Z1.y > Z2.y; });
  for (int i = 0; i < m; ++i) {
    vector<int> Selected(n, 1);
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int ans = 0;
    int a = lower_bound(XCol.begin(), XCol.end(), x2 + 1) - XCol.begin();
    int b = upper_bound(XCol.begin(), XCol.end(), x1 - 1) - XCol.begin();
    for (int i = 0; i < b; ++i) {
      Selected[IXCol[i].id] = 0;
    }
    for (int i = a; i < n; ++i) {
      Selected[IXCol[i].id] = 0;
    }
    int c = lower_bound(YCol.begin(), YCol.end(), y2 + 1) - YCol.begin();
    int d = upper_bound(YCol.begin(), YCol.end(), y1 - 1) - YCol.begin();
    for (int i = 0; i < d; ++i) {
      Selected[IYCol[i].id] = 0;
    }

    for (int i = c; i < n; ++i) {
      Selected[IYCol[i].id] = 0;
    }
    /*for (auto it = ZaCol.begin(); it != ZaCol.end(); ++it){
            if ((*it).x >= x1 && (*it).x <= x2 && (*it).y >= y1 && (*it).y <=
    y2){ ans++;
            }
    }*/
    cout << count(Selected.begin(), Selected.end(), 1) << endl;
  }
  return 0;
}