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

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

inline bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef long long ll;
typedef unsigned long long ull;

#define For(i, a, b) for (int(i) = (a); i < (b); (i)++)
#define rep(i, n) For(i, 0, n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (sizeof(a))
#define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v)

const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};
const int mod = 1000000007;
const int INF = 1e9 + 2;

vector<vector<short>> d;

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

  vector<int> ix(n), iy(n);

  //??§?¨???§?????¨????????????
  vector<int> ax, ay;
  map<int, int> mx, my;

  rep(i, n) {
    cin >> ix[i] >> iy[i];
    rep(j, 2) {
      ax.push_back(ix[i] + j - 1);
      ay.push_back(iy[i] + j - 1);
    }
  }
  ax.push_back(-INF - 1);
  ay.push_back(-INF - 1);
  ax.push_back(INF + 1);
  ay.push_back(INF + 1);

  sort(all(ax));
  sort(all(ay));

  rep(i, ax.size()) { mx[ax[i]] = i; }
  rep(i, ay.size()) { my[ay[i]] = i; }

  // d[x][y] 0 - x  0 - y ??????????????????????±?????????????
  d.resize(ax.size() + 1, vector<short>(ay.size() + 1, 0));
  rep(i, n) { d[mx[ix[i]]][my[iy[i]]]++; }
  For(x, 1, ax.size() + 1) For(y, 1, ay.size() + 1) {
    d[x][y] = d[x - 1][y] + d[x][y - 1] - d[x - 1][y - 1] + d[x][y];
  }

  rep(i, m) {
    int qx1, qy1, qx2, qy2;
    cin >> qx1 >> qy1 >> qx2 >> qy2;
    int rx, ry, lx, ly;
    qx1--;
    qy1--;
    if (mx.count(qx1)) {
      lx = mx[qx1];
    } else {
      mx[qx1] = -1;
      auto p = mx.find(qx1);
      lx = prev(p)->second;
      mx.erase(qx1);
    }
    if (my.count(qy1)) {
      ly = my[qy1];
    } else {
      my[qy1] = -1;
      auto p = my.find(qy1);
      ly = prev(p)->second;
      my.erase(qy1);
    }

    if (mx.count(qx2)) {
      rx = mx[qx2];
    } else {
      mx[qx2] = -1;
      auto p = mx.find(qx2);
      rx = next(p)->second;
      mx.erase(qx2);
    }

    if (my.count(qy2)) {
      ry = my[qy2];
    } else {
      my[qy2] = -1;
      auto p = my.find(qy2);
      ry = next(p)->second;
      my.erase(qy2);
    }

    cout << d[rx][ry] + d[lx][ly] - d[lx][ry] - d[rx][ly] << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

inline bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef long long ll;
typedef unsigned long long ull;

#define For(i, a, b) for (int(i) = (a); i < (b); (i)++)
#define rep(i, n) For(i, 0, n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (sizeof(a))
#define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v)

const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};
const int mod = 1000000007;
const int INF = 1e9 + 2;

vector<vector<short>> d;

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

  vector<int> ix(n), iy(n);

  //??§?¨???§?????¨????????????
  vector<int> ax, ay;
  map<int, int> mx, my;

  rep(i, n) {
    cin >> ix[i] >> iy[i];
    rep(j, 3) {
      ax.push_back(ix[i] + j - 1);
      ay.push_back(iy[i] + j - 1);
    }
  }
  ax.push_back(-INF - 1);
  ay.push_back(-INF - 1);
  ax.push_back(INF + 1);
  ay.push_back(INF + 1);

  sort(all(ax));
  sort(all(ay));

  rep(i, ax.size()) { mx[ax[i]] = i; }
  rep(i, ay.size()) { my[ay[i]] = i; }

  // d[x][y] 0 - x  0 - y ??????????????????????±?????????????
  d.resize(ax.size() + 1, vector<short>(ay.size() + 1, 0));
  rep(i, n) { d[mx[ix[i]]][my[iy[i]]]++; }
  For(x, 1, ax.size() + 1) For(y, 1, ay.size() + 1) {
    d[x][y] = d[x - 1][y] + d[x][y - 1] - d[x - 1][y - 1] + d[x][y];
  }

  rep(i, m) {
    int qx1, qy1, qx2, qy2;
    cin >> qx1 >> qy1 >> qx2 >> qy2;
    int rx, ry, lx, ly;
    qx1--;
    qy1--;
    if (mx.count(qx1)) {
      lx = mx[qx1];
    } else {
      mx[qx1] = -1;
      auto p = mx.find(qx1);
      lx = prev(p)->second;
      mx.erase(qx1);
    }
    if (my.count(qy1)) {
      ly = my[qy1];
    } else {
      my[qy1] = -1;
      auto p = my.find(qy1);
      ly = prev(p)->second;
      my.erase(qy1);
    }

    if (mx.count(qx2)) {
      rx = mx[qx2];
    } else {
      mx[qx2] = -1;
      auto p = mx.find(qx2);
      rx = next(p)->second;
      mx.erase(qx2);
    }

    if (my.count(qy2)) {
      ry = my[qy2];
    } else {
      my[qy2] = -1;
      auto p = my.find(qy2);
      ry = next(p)->second;
      my.erase(qy2);
    }

    cout << d[rx][ry] + d[lx][ly] - d[lx][ry] - d[rx][ly] << endl;
  }

  return 0;
}