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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<int>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli n;
vll sx, sy, tx, ty;
map<lli, int> m;
mat mt;

lli xf, xt;
lli yf, yt;
lli ans = 0;

int main() {
  cin >> n;
  sx = sy = tx = ty = vll(n);
  for (lli i = 0; i < n; i++)
    cin >> sx[i] >> sy[i] >> tx[i] >> ty[i];
  for (lli i = 0; i < n; i++) {
    m[sx[i]] = 0;
    m[sy[i]] = 0;
    m[tx[i]] = 0;
    m[ty[i]] = 0;
  }
  lli k = 0;
  for (auto itr = m.begin(); itr != m.end(); itr++) {
    itr->second = k++;
  }
  mt = mat(k + 1, vector<int>(k + 1));
  for (lli i = 0; i < n; i++) {
    mt[m[sx[i]]][m[sy[i]]]++;
    mt[m[sx[i]]][m[ty[i]]]--;
    mt[m[tx[i]]][m[sy[i]]]--;
    mt[m[tx[i]]][m[ty[i]]]++;
  }
  for (lli i = 0; i < k; i++)
    for (lli j = 0; j < k; j++)
      mt[i][j + 1] += mt[i][j];
  for (lli j = 0; j < k; j++)
    for (lli i = 0; i < k; i++)
      mt[i + 1][j] += mt[i][j];
  /*
  for(lli i = 0;i < k;i++){
      for(lli j = 0;j < k;j++){
          cout << mt[i][j];
      }
      cout << endl;
  }
  */
  for (auto itr = m.begin();;) {
    xf = itr->first;
    itr++;
    if (itr == m.end())
      break;
    xt = itr->first;
    for (auto jtr = m.begin();;) {
      yf = jtr->first;
      jtr++;
      if (jtr == m.end())
        break;
      yt = jtr->first;
      if (mt[itr->second - 1][jtr->second - 1]) {
        ans += (xt - xf) * (yt - yf);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli n;
vll sx, sy, tx, ty;
map<lli, lli> m;
mat mt;

lli xf, xt;
lli yf, yt;
lli ans = 0;

int main() {
  cin >> n;
  sx = sy = tx = ty = vll(n);
  for (lli i = 0; i < n; i++)
    cin >> sx[i] >> sy[i] >> tx[i] >> ty[i];
  for (lli i = 0; i < n; i++) {
    m[sx[i]] = 0;
    m[sy[i]] = 0;
    m[tx[i]] = 0;
    m[ty[i]] = 0;
  }
  lli k = 0;
  for (auto itr = m.begin(); itr != m.end(); itr++) {
    itr->second = k++;
  }
  mt = mat(k + 1, vll(k + 1));
  for (lli i = 0; i < n; i++) {
    mt[m[sx[i]]][m[sy[i]]]++;
    mt[m[sx[i]]][m[ty[i]]]--;
    mt[m[tx[i]]][m[sy[i]]]--;
    mt[m[tx[i]]][m[ty[i]]]++;
  }
  for (lli i = 0; i < k; i++)
    for (lli j = 0; j < k; j++)
      mt[i][j + 1] += mt[i][j];
  for (lli j = 0; j < k; j++)
    for (lli i = 0; i < k; i++)
      mt[i + 1][j] += mt[i][j];
  /*
  for(lli i = 0;i < k;i++){
      for(lli j = 0;j < k;j++){
          cout << mt[i][j];
      }
      cout << endl;
  }
  */
  for (auto itr = m.begin();;) {
    xf = itr->first;
    itr++;
    if (itr == m.end())
      break;
    xt = itr->first;
    for (auto jtr = m.begin();;) {
      yf = jtr->first;
      jtr++;
      if (jtr == m.end())
        break;
      yt = jtr->first;
      if (mt[itr->second - 1][jtr->second - 1]) {
        ans += (xt - xf) * (yt - yf);
      }
    }
  }
  cout << ans << endl;
  return 0;
}