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

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for (auto i = (m).begin(); i != (m).end(); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template <typename T> void printv(vector<T> const &s) {
  REP(i, SZ(s)) { cout << s[i] << " "; }
  cout << endl;
}

int main() {
  cin.tie(0);
  cout << setprecision(10);

  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;
    vi user(n);
    vector<vector<P>> record(m);

    int r;
    cin >> r;
    REP(i, r) {
      int tr, nr, mr, sr;
      cin >> tr >> nr >> mr >> sr;
      if (sr) {
        user[nr - 1] = tr;
      } else {
        record[mr - 1].pb(make_pair(user[nr - 1], tr));
      }
    }

    int q;
    cin >> q;
    REP(i, q) {
      int ts, te, mq;
      cin >> ts >> te >> mq;
      int ans = 0;
      FOR(i, ts, te) {
        REP(j, SZ(record[mq - 1])) {
          P p = record[mq - 1][j];
          if (p.first <= i && i < p.second) {
            ans++;
            break;
          }
        }
      }
      cout << ans << endl;
    }
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for (auto i = (m).begin(); i != (m).end(); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template <typename T> void printv(vector<T> const &s) {
  REP(i, SZ(s)) { cout << s[i] << " "; }
  cout << endl;
}

int main() {
  cin.tie(0);
  cout << setprecision(10);

  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<vector<vector<P>>> a(m);
    vector<vector<P>> b(m);
    vector<vector<P>> c(m);
    REP(i, m) { a[i].resize(n); }

    int r;
    cin >> r;
    REP(i, r) {
      int nowt, nown, nowm, nows;
      cin >> nowt >> nown >> nowm >> nows;
      nown--;
      nowm--;
      if (nows == 1) {
        a[nowm][nown].pb(P(nowt, 0));
      } else {
        a[nowm][nown][a[nowm][nown].size() - 1].se = nowt;
      }
    }

    REP(i, m) {
      REP(j, n) {
        REP(k, SZ(a[i][j])) { b[i].pb(a[i][j][k]); }
      }
    }

    if (1)
      vector<vector<vector<P>>>().swap(a);

    REP(i, m) {
      // cout << "b" << ":" << SZ(b[i]) << endl;
      if (SZ(b[i])) {
        sort(ALL(b[i]));
        c[i].pb(b[i][0]);
      }

      FOR(j, 1, SZ(b[i])) {
        // cout << i << ":" << j << endl;
        if (c[i][SZ(c[i]) - 1].se <= b[i][j].fi) {
          c[i].pb(b[i][j]);
        } else {
          c[i][SZ(c[i]) - 1].se = max(c[i][SZ(c[i]) - 1].se, b[i][j].se);
        }
      }
      // cout << "c:" << SZ(c[i]) << endl;
    }

    if (1)
      vector<vector<P>>().swap(b);

    // REP(i, m) {
    //   REP(j, SZ(c[i])) {
    //     // cout << i << ":" << j << ":" << c[i][j].fi << ":" << c[i][j].se <<
    //     endl;
    //   }
    // }

    int q;
    cin >> q;
    REP(i, q) {
      int ts, te, nowm;
      cin >> ts >> te >> nowm;
      nowm--;
      int ans = 0;
      REP(j, SZ(c[nowm])) {
        int tmp = min(te, c[nowm][j].se) - max(ts, c[nowm][j].fi);
        if (tmp > 0)
          ans += tmp;
      }
      cout << ans << endl;
    }
  }
}