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

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

/***** ?´??????? *****/

// ????¬?????´???????
template <typename T> vector<T> calc_csum(vector<T> &val) {
  vector<T> sum(val.size() + 1);
  partial_sum(val.begin(), val.end(), sum.begin() + 1);
  return sum;
}

template <typename T> T sum_csum(vector<T> &sum, int begin, int end) {
  return sum[end] - sum[begin];
}

// ????¬?????´???????
template <typename T> vector<vector<T>> calc_csum2(vector<vector<T>> &val) {
  int H = val.size();
  int W = val[0].size();
  vector<vector<T>> sum(H + 1, vector<T>(W + 1));

  for (int y = 0; y < H; y++) {
    partial_sum(val[y].begin(), val[y].end(), sum[y + 1].begin() + 1);
  }

  for (int y = 0; y < H; y++) {
    for (int x = 0; x <= W; x++) {
      sum[y + 1][x] += sum[y][x];
    }
  }

  return sum;
}

template <typename T>
T sum_csum2(vector<vector<T>> &sum, int y1, int y2, int x1, int x2) {
  return sum[y2][x2] - sum[y2][x1] - sum[y1][x2] + sum[y1][x1];
}

/***** /?´??????? *****/

struct Query {
  Query() {}
  int x1;
  int x2;
  int y1;
  int y2;
};

VI uniq_xs;
VI uniq_ys;

VI uniq_xs_r;
VI uniq_ys_r;

void my_uniq(VI &vec) {
  sort(ALL(vec));
  auto it = unique(ALL(vec));
  vec.resize(distance(vec.begin(), it));
}

int trans_left(VI &vec, int value) {
  auto it = lower_bound(ALL(vec), value);
  int pos = it - vec.begin();
  return pos;
}

int trans_right(VI &vec_r, int value) {
  auto it = lower_bound(ALL(vec_r), value, greater<int>());
  int pos = it - vec_r.begin();
  return (int)vec_r.size() - pos - 1;
}

int main(void) {
  const int MAX_N = 1000 * 1000 * 1000 + 10;

  // input
  int n, m;
  cin >> n >> m;

  VI xs(n), ys(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i];
    uniq_xs.PB(xs[i]);
    uniq_ys.PB(ys[i]);
  }

  // ??§?¨???§???
  uniq_xs.PB(0);
  uniq_xs.PB(MAX_N);
  uniq_ys.PB(0);
  uniq_ys.PB(MAX_N);
  my_uniq(uniq_xs);
  my_uniq(uniq_ys);

  // ??????????????\\?????????????????????
  uniq_xs_r = VI(ALL(uniq_xs));
  uniq_ys_r = VI(ALL(uniq_ys));
  sort(ALL(uniq_xs_r), greater<int>());
  sort(ALL(uniq_ys_r), greater<int>());

  // map?????????
  int w = uniq_xs.size();
  int h = uniq_ys.size();
  VVI ts(h, VI(w));

  for (int i = 0; i < n; i++) {
    int x = trans_left(uniq_xs, xs[i]);
    int y = trans_left(uniq_ys, ys[i]);
    ts[y][x]++;
  }

  // ?´??????????

  auto ts_sum = calc_csum2(ts);

  // query
  Query qs;

  for (int i = 0; i < m; i++) {
    cin >> qs.x1 >> qs.y1 >> qs.x2 >> qs.y2;

    int y1 = trans_left(uniq_ys, qs.y1);
    int x1 = trans_left(uniq_xs, qs.x1);
    int y2 = trans_right(uniq_ys_r, qs.y2);
    int x2 = trans_right(uniq_xs_r, qs.x2);

    int ans = sum_csum2(ts_sum, y1, y2 + 1, x1, x2 + 1);

    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

/***** ?´??????? *****/

// ????¬?????´???????
template <typename T> vector<T> calc_csum(vector<T> &val) {
  vector<T> sum(val.size() + 1);
  partial_sum(val.begin(), val.end(), sum.begin() + 1);
  return sum;
}

template <typename T> T sum_csum(vector<T> &sum, int begin, int end) {
  return sum[end] - sum[begin];
}

// ????¬?????´???????
template <typename T> vector<vector<T>> calc_csum2(vector<vector<T>> &val) {
  int H = val.size();
  int W = val[0].size();
  vector<vector<T>> sum(H + 1, vector<T>(W + 1));

  for (int y = 0; y < H; y++) {
    partial_sum(val[y].begin(), val[y].end(), sum[y + 1].begin() + 1);
  }

  for (int y = 0; y < H; y++) {
    for (int x = 0; x <= W; x++) {
      sum[y + 1][x] += sum[y][x];
    }
  }

  return sum;
}

template <typename T>
T sum_csum2(vector<vector<T>> &sum, int y1, int y2, int x1, int x2) {
  return sum[y2][x2] - sum[y2][x1] - sum[y1][x2] + sum[y1][x1];
}

/***** /?´??????? *****/

struct Query {
  Query() {}
  int x1;
  int x2;
  int y1;
  int y2;
};

VI uniq_xs;
VI uniq_ys;

VI uniq_xs_r;
VI uniq_ys_r;

void my_uniq(VI &vec) {
  sort(ALL(vec));
  auto it = unique(ALL(vec));
  vec.resize(distance(vec.begin(), it));
}

int trans_left(VI &vec, int value) {
  auto it = lower_bound(ALL(vec), value);
  int pos = it - vec.begin();
  return pos;
}

int trans_right(VI &vec_r, int value) {
  auto it = lower_bound(ALL(vec_r), value, greater<int>());
  int pos = it - vec_r.begin();
  return (int)vec_r.size() - pos - 1;
}

int main(void) {
  const int MAX_N = 1000 * 1000 * 1000 + 10;

  // input
  int n, m;
  cin >> n >> m;

  VI xs(n), ys(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i];
    uniq_xs.PB(xs[i]);
    uniq_ys.PB(ys[i]);
  }

  vector<Query> qs(m);
  for (int i = 0; i < m; i++) {
    cin >> qs[i].x1 >> qs[i].y1 >> qs[i].x2 >> qs[i].y2;
  }

  // ??§?¨???§???
  uniq_xs.PB(0);
  uniq_xs.PB(MAX_N);
  uniq_ys.PB(0);
  uniq_ys.PB(MAX_N);
  my_uniq(uniq_xs);
  my_uniq(uniq_ys);

  // ??????????????\\?????????????????????
  uniq_xs_r = VI(ALL(uniq_xs));
  uniq_ys_r = VI(ALL(uniq_ys));
  sort(ALL(uniq_xs_r), greater<int>());
  sort(ALL(uniq_ys_r), greater<int>());

  /*
          cout << "uniq_xs: ";
          for (auto x : uniq_xs) cout << x << " ";
          cout << endl;

          cout << "uniq_ys: ";
          for (auto y : uniq_ys) cout << y << " ";
          cout << endl;
  */

  // map?????????
  int w = uniq_xs.size();
  int h = uniq_ys.size();
  VVI ts(h, VI(w));

  for (int i = 0; i < n; i++) {
    int x = trans_left(uniq_xs, xs[i]);
    int y = trans_left(uniq_ys, ys[i]);
    ts[y][x]++;
  }
  /*
          for (int y = 0; y < h; y++) {
                  for (int x = 0; x < w; x++) {
                          cout << ts[y][x] << " ";
                  }
                  cout << endl;
          }
          cout << endl;
  */
  // ?´??????????

  auto ts_sum = calc_csum2(ts);

  // query

  for (int i = 0; i < m; i++) {
    int y1 = trans_left(uniq_ys, qs[i].y1);
    int x1 = trans_left(uniq_xs, qs[i].x1);
    int y2 = trans_right(uniq_ys_r, qs[i].y2);
    int x2 = trans_right(uniq_xs_r, qs[i].x2);

    //		printf("(%d, %d) ~ (%d, %d)\\n", x1, y1, x2, y2);

    int ans = sum_csum2(ts_sum, y1, y2 + 1, x1, x2 + 1);

    cout << ans << endl;
  }

  return 0;
}