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


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <limits.h>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;
const int ninf = -(1000000000 + 1);

int solve(void);
int getTreasureNum(vector<vector<int>> &clist, vector<vector<int>> &mp,
                   vector<vector<int>> &orig, vector<map<int, int>> &trans);
int getSmallerID(vector<int> &list, int target);
int getSmallerID(vector<vector<int>> &orig, int &x, int &y, int target_x,
                 int target_y);

int main(void) {

  // FILE *fpin = freopen("data.txt", "r", stdin);
  while (solve())
    ;
  return 0;
}

int solve(void) {

  int n, m;
  cin >> n >> m;

  /*
          ?´?????????¨??§?¨???§???
  */

  vector<vector<int>> clist(2);
  vector<pair<int, int>> plist(n);
  vector<vector<int>> mp, orig;
  vector<map<int, int>> trans(2);
  /*
          input
  */
  for (int i = 0; i < n; i++) {
    cin >> plist[i].first >> plist[i].second;
    clist[1].push_back(plist[i].first);
    clist[0].push_back(plist[i].second);
  }

  for (int i = 0; i < 2; i++) {
    clist[i].push_back(ninf);
    sort(clist[i].begin(), clist[i].end());
    clist[i].erase(unique(clist[i].begin(), clist[i].end()), clist[i].end());
    for (int j = 0; j < clist[i].size(); j++) {
      trans[i][clist[i][j]] = j;
    }
  }
  mp = vector<vector<int>>(clist[0].size(), vector<int>(clist[1].size(), 0));

  for (int i = 0; i < n; i++)
    mp[trans[0][plist[i].second]][trans[1][plist[i].first]]++;
  orig = mp;

  for (int x = 0; x < mp[0].size(); x++) {
    for (int y = 0; y + 1 < mp.size(); y++) {
      mp[y + 1][x] = mp[y + 1][x] + mp[y][x];
    }
  }

  for (int y = 0; y < mp.size(); y++) {
    for (int x = 0; x + 1 < mp[0].size(); x++) {
      mp[y][x + 1] = mp[y][x + 1] + mp[y][x];
    }
  }

  for (int i = 0; i < m; i++)
    cout << getTreasureNum(clist, mp, orig, trans) << endl;

  return 0;
}

int getTreasureNum(vector<vector<int>> &clist, vector<vector<int>> &mp,
                   vector<vector<int>> &orig, vector<map<int, int>> &trans) {
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  if (x2 < clist[1][0] || x1 > clist[1][clist[1].size() - 1] ||
      y2 < clist[0][0] || y1 > clist[0][clist[0].size() - 1]) {
    return 0;
  }

  if (trans[1].find(x1) == trans[1].end())
    x1 = min(getSmallerID(clist[1], x1) + 1, (int)clist[1].size() - 1);
  else
    x1 = getSmallerID(clist[1], x1);

  x2 = getSmallerID(clist[1], x2);

  if (trans[0].find(y1) == trans[0].end())
    y1 = min(getSmallerID(clist[0], y1) + 1, (int)clist[0].size() - 1);
  else
    y1 = getSmallerID(clist[0], y1);

  y2 = getSmallerID(clist[0], y2);

  x1--;
  y1--;
  return mp[y2][x2] - mp[y1][x2] - mp[y2][x1] + mp[y1][x1];
}

int getSmallerID(vector<int> &list, int target) {
  int low = 0;
  int high = list.size();
  int mid = 0;

  while (abs(low - high) > 1) {
    mid = (low + high) / 2;
    if (list[mid] <= target) {
      low = mid;
    } else {
      high = mid;
    }
  }
  mid = (high + low) / 2;
  return mid;
}

int getSmallerID(vector<vector<int>> &orig, int &x, int &y, int target_x,
                 int target_y) {
  return 0;
}

Error: MLE

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <limits.h>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;
const int ninf = -(1000000000 + 1);

int solve(void);
int getTreasureNum(vector<vector<int>> &clist, vector<vector<int>> &mp,
                   vector<vector<int>> &orig, vector<map<int, int>> &trans,
                   vector<vector<int>> &xsum, vector<vector<int>> &ysum);
int getSmallerID(vector<int> &list, int target);
int getSmallerID(vector<vector<int>> &orig, int &x, int &y, int target_x,
                 int target_y);

int main(void) {

  // FILE *fpin = freopen("data.txt", "r", stdin);
  while (solve())
    ;
  return 0;
}

int solve(void) {

  int n, m;
  cin >> n >> m;

  /*
          ?´?????????¨??§?¨???§???
  */

  vector<vector<int>> clist(2);
  vector<pair<int, int>> plist(n);
  vector<vector<int>> mp, orig, xsum, ysum;
  vector<map<int, int>> trans(2);
  /*
          input
  */
  for (int i = 0; i < n; i++) {
    cin >> plist[i].first >> plist[i].second;
    clist[1].push_back(plist[i].first);
    clist[0].push_back(plist[i].second);
  }

  for (int i = 0; i < 2; i++) {
    clist[i].push_back(ninf);
    sort(clist[i].begin(), clist[i].end());
    clist[i].erase(unique(clist[i].begin(), clist[i].end()), clist[i].end());
    for (int j = 0; j < clist[i].size(); j++) {
      trans[i][clist[i][j]] = j;
    }
  }
  mp = vector<vector<int>>(clist[0].size(), vector<int>(clist[1].size(), 0));

  for (int i = 0; i < n; i++)
    mp[trans[0][plist[i].second]][trans[1][plist[i].first]]++;
  orig = mp;
  xsum = mp;
  ysum = mp;

  for (int x = 0; x < mp[0].size(); x++) {
    for (int y = 0; y + 1 < mp.size(); y++) {
      mp[y + 1][x] = mp[y + 1][x] + mp[y][x];
    }
  }
  ysum = mp;

  for (int y = 0; y < mp.size(); y++) {
    for (int x = 0; x + 1 < mp[0].size(); x++) {
      mp[y][x + 1] = mp[y][x + 1] + mp[y][x];
      xsum[y][x + 1] = xsum[y][x + 1] + xsum[y][x];
    }
  }

  for (int i = 0; i < m; i++)
    cout << getTreasureNum(clist, mp, orig, trans, xsum, ysum) << endl;

  return 0;
}

int getTreasureNum(vector<vector<int>> &clist, vector<vector<int>> &mp,
                   vector<vector<int>> &orig, vector<map<int, int>> &trans,
                   vector<vector<int>> &xsum, vector<vector<int>> &ysum) {
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  if (x2 < clist[1][0] || x1 > clist[1][clist[1].size() - 1] ||
      y2 < clist[0][0] || y1 > clist[0][clist[0].size() - 1]) {
    return 0;
  }

  if (trans[1].find(x1) == trans[1].end())
    x1 = min(getSmallerID(clist[1], x1) + 1, (int)clist[1].size() - 1);
  else
    x1 = getSmallerID(clist[1], x1);

  x2 = getSmallerID(clist[1], x2);

  if (trans[0].find(y1) == trans[0].end())
    y1 = min(getSmallerID(clist[0], y1) + 1, (int)clist[0].size() - 1);
  else
    y1 = getSmallerID(clist[0], y1);

  y2 = getSmallerID(clist[0], y2);

  if (x1 == x2 && y1 == y2) {
    return orig[y1][x1];
  } else if (x1 == x2) {
    return ysum[y2][x1] - ysum[y1][x1] + orig[y1][x1];
  } else if (y1 == y2) {
    return xsum[y2][x2] - xsum[y1][x1] + orig[y1][x1];
  } else {
    int z1 = mp[y2][x2] - mp[y1][x2] - mp[y2][x1] + mp[y1][x1];
    int z2 = xsum[y1][x2] - xsum[y1][x1];
    int z3 = ysum[y2][x1] - ysum[y1][x1];
    return z1 + z2 + z3 + orig[y1][x1];
  }
}

int getSmallerID(vector<int> &list, int target) {
  int low = 0;
  int high = list.size();
  int mid = 0;

  while (abs(low - high) > 1) {
    mid = (low + high) / 2;
    if (list[mid] <= target) {
      low = mid;
    } else {
      high = mid;
    }
  }
  mid = (high + low) / 2;
  return mid;
}

int getSmallerID(vector<vector<int>> &orig, int &x, int &y, int target_x,
                 int target_y) {
  return 0;
}