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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define dump(o)                                                                \\
  {                                                                            \\
    cerr << #o;                                                                \\
    for (auto e : (o))                                                         \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

// pair?????????
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

//??§?¨???§???
// v: ??§???????????§?¨??????? ????????????
template <typename T> void compress(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

//??§?¨???§???(map)
// v: ??§???????????§?¨??????? ????????????
//?????????: zip[??§???????????§?¨?]:??§???????????§?¨?
//(zip.size()<=10^5?????????????????????)
template <typename T> map<T, T> compressMap(vector<T> &v) {
  map<T, T> zip;
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (T i = 0; i < v.size(); i++)
    zip[v[i]] = i;
  return zip;
}

//??§???????????§?¨?
// index(??§???????????§?¨?, ??§???????????§?¨??????????(??????) )
template <typename T> int index(vector<T> v, T i) {
  return lower_bound(v.begin(), v.end(), i) - v.begin();
}

// 2?¬????imos???
class Imos {
public:
  using vvi = vector<vector<int>>;
  int X, Y;
  vvi s; //?´???????
  // f:field
  Imos(vvi &f) {
    X = f.size();
    Y = f[0].size();
    // one-based
    //(x,y)?????§????´??????????(x+1,y+1)
    s = vvi(X + 1, vector<int>(Y + 1));
    for (int y = 0; y < Y; y++)
      for (int x = 0; x < X; x++)
        s[x + 1][y + 1] += s[x + 1][y] + s[x][y + 1] - s[x][y] + f[x][y];
  }
  //[x1,x2)??????[y1,y2)???field??????????±???????
  // one-based????????????????§?
  int sum(int x1, int y1, int x2, int y2) {
    return s[x2][y2] - s[x2][y1] - s[x1][y2] + s[x1][y1];
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> xs(n), ys(n);
  vector<int> x(n), y(n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &xs[i], &ys[i]);
    x[i] = xs[i];
    y[i] = ys[i];
  }
  compress(x);
  compress(y);

  // map<int, int> zipx(compressMap(x));
  // map<int, int> zipy(compressMap(y));
  // dump(x);
  // dump(zipx);
  // dump(y);
  // dump(zipy);

  vector<vector<int>> f(x.size(), vector<int>(y.size()));

  for (int i = 0; i < n; i++) {
    int vx = index(x, xs[i]);
    int vy = index(y, ys[i]);
    f[vx][vy]++;
  }

  // for (int vx = 0; vx < x.size(); vx++) dump(f[vx]);

  Imos imos(f);

  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    x1 = index(x, x1);
    y1 = index(y, y1);
    x2 = index(x, x2 + 1); // Imos???sum???one-based?????????+1
    y2 = index(y, y2 + 1);
    cout << imos.sum(x1, y1, x2, y2) << endl;
  }
  return 0;
}

// Sample2
// https://gyazo.com/39c67d1abe7a6a686acb52285f8a0bc9

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define dump(o)                                                                \\
  {                                                                            \\
    cerr << #o;                                                                \\
    for (auto e : (o))                                                         \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

// pair?????????
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

//??§?¨???§???
// v: ??§???????????§?¨??????? ????????????
template <typename T> void compress(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

//??§?¨???§???(map)
// v: ??§???????????§?¨??????? ????????????
//?????????: zip[??§???????????§?¨?]:??§???????????§?¨?
//(zip.size()<=10^5?????????????????????)
template <typename T> map<T, T> compressMap(vector<T> &v) {
  map<T, T> zip;
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (T i = 0; i < v.size(); i++)
    zip[v[i]] = i;
  return zip;
}

//??§???????????§?¨?
// index(??§???????????§?¨?, ??§???????????§?¨??????????(??????) )
template <typename T> int index(vector<T> v, T i) {
  return lower_bound(v.begin(), v.end(), i) - v.begin();
}

// 2?¬????imos???
class Imos {
public:
  using vvi = vector<vector<int>>;
  int X, Y;
  vvi s; //?´???????
  // f:field
  Imos(vvi f) {
    X = f.size();
    Y = f[0].size();
    // one-based
    //(x,y)?????§????´??????????(x+1,y+1)
    s = vvi(X + 1, vector<int>(Y + 1));
    for (int y = 0; y < Y; y++)
      for (int x = 0; x < X; x++)
        s[x + 1][y + 1] += s[x + 1][y] + s[x][y + 1] - s[x][y] + f[x][y];
  }
  //[x1,x2)??????[y1,y2)???field??????????±???????
  // one-based????????????????§?
  int sum(int x1, int y1, int x2, int y2) {
    return s[x2][y2] - s[x2][y1] - s[x1][y2] + s[x1][y1];
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> xs(n), ys(n);
  vector<int> x(n), y(n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &xs[i], &ys[i]);
    x[i] = xs[i];
    y[i] = ys[i];
  }
  compress(x);
  compress(y);

  // map<int, int> zipx(compressMap(x));
  // map<int, int> zipy(compressMap(y));
  // dump(x);
  // dump(zipx);
  // dump(y);
  // dump(zipy);

  vector<vector<int>> f(x.size(), vector<int>(y.size()));

  for (int i = 0; i < n; i++) {
    int vx = index(x, xs[i]);
    int vy = index(y, ys[i]);
    f[vx][vy]++;
  }

  // for (int vx = 0; vx < x.size(); vx++) dump(f[vx]);

  x.shrink_to_fit();
  y.shrink_to_fit();
  xs.shrink_to_fit();
  ys.shrink_to_fit();
  Imos imos(f);

  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    x1 = index(x, x1);
    y1 = index(y, y1);
    x2 = index(x, x2 + 1); // Imos???sum???one-based?????????+1
    y2 = index(y, y2 + 1);
    cout << imos.sum(x1, y1, x2, y2) << endl;
  }
  return 0;
}

// Sample2
// https://gyazo.com/39c67d1abe7a6a686acb52285f8a0bc9