Problem p03504 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

int N, C;
int num[210000];

typedef pair<int, int> pint;
vector<pint> inp[35];

int main() {
  while (cin >> N >> C) {
    for (int i = 0; i < 35; ++i)
      inp[i].clear();
    for (int i = 0; i < N; ++i) {
      int s, t, c;
      cin >> s >> t >> c;
      inp[c].push_back(pint(s, t));
    }
    for (int i = 0; i < 35; ++i) {
      sort(inp[i].begin(), inp[i].end());
    }

    for (int i = 0; i < 35; ++i) {
      for (int j = 0; j < (int)inp[i].size() - 1; ++j) {
        if (inp[i][j].second == inp[i][j + 1].first) {
          inp[i][j].second = inp[i][j + 1].second;
          inp[i].erase(inp[i].begin() + (j + 1));
          --j;
        }
      }
    }

    for (int i = 0; i < 35; ++i) {
      for (int j = 0; j < inp[i].size(); ++j) {
        inp[i][j].first--;
      }

      if (!inp[i].empty()) {
        // COUT(i); COUT(inp[i]);
      }
    }

    memset(num, 0, sizeof(num));
    for (int i = 0; i < 35; ++i) {
      for (int j = 0; j < inp[i].size(); ++j) {
        num[inp[i][j].first]++;
        num[inp[i][j].second]--;
      }
    }

    for (int i = 0; i <= 100000; ++i) {
      num[i + 1] += num[i];
    }

    int res = 0;
    for (int i = 0; i <= 100000; ++i) {
      chmax(res, num[i]);
      // if (i < 100) cout << i << ": " << num[i] << endl;
    }

    cout << res << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

int N, C;
int num[210000];

typedef pair<int, int> pint;
vector<pint> inp[35];

int main() {
  while (cin >> N >> C) {
    for (int i = 0; i < 35; ++i)
      inp[i].clear();
    for (int i = 0; i < N; ++i) {
      int s, t, c;
      cin >> s >> t >> c;
      inp[c].push_back(pint(s, t));
    }
    for (int i = 0; i < 35; ++i) {
      sort(inp[i].begin(), inp[i].end());
    }

    for (int i = 0; i < 35; ++i) {
      for (int j = 0; j < (int)inp[i].size() - 1; ++j) {
        if (inp[i][j].second == inp[i][j + 1].first) {
          inp[i][j + 1].first = inp[i][j].first;
          inp[i][j] = pint(1, 0);
        }
      }
    }

    for (int i = 0; i < 35; ++i) {
      for (int j = 0; j < inp[i].size(); ++j) {
        inp[i][j].first--;
      }

      if (!inp[i].empty()) {
        // COUT(i); COUT(inp[i]);
      }
    }

    memset(num, 0, sizeof(num));
    for (int i = 0; i < 35; ++i) {
      for (int j = 0; j < inp[i].size(); ++j) {
        num[inp[i][j].first]++;
        num[inp[i][j].second]--;
      }
    }

    for (int i = 0; i <= 100000; ++i) {
      num[i + 1] += num[i];
    }

    int res = 0;
    for (int i = 0; i <= 100000; ++i) {
      chmax(res, num[i]);
      // if (i < 100) cout << i << ": " << num[i] << endl;
    }

    cout << res << endl;
  }
}