Problem p01138 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define S second
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;

int di[3] = {3600, 60, 1};

int getTime(string s) {
  rep(i, s.length()) if (s[i] == ':') s[i] = ' ';
  stringstream ss;
  ss << s;
  int ret = 0;
  for (int i = 0; i < 3; i++) {
    string pre;
    ss >> pre;
    int tim = (atoi)(pre.c_str());
    ret += tim * di[i];
  }

  return ret;
}

int main() {
  int n;

  while (true) {
    cin >> n;
    if (n == 0)
      break;
    string star, endar;
    vector<P> vec;
    rep(i, n) {
      cin >> star >> endar;
      int st, ed;
      st = getTime(star);
      ed = getTime(endar);
      vec.pb(P(st, ed));
    }
    sort(all(vec));
    int cnt = 0;
    int mex = 0;

    rep(i, n) {
      cnt = 1;
      for (int j = i - 1; j >= 0; j--) {
        if (vec[j].S <= vec[i].F)
          continue;
        cnt++;
      }
      mex = max(cnt, mex);
    }

    cout << mex << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
#define all(n) n.begin(), n.end()
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define INF (1 << 27)
#define EPS 1e-10
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define MAX_N 1001000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> Pp;
typedef pair<P, P> PP;
typedef unsigned long long ull;
typedef long long ll;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

int get_time(string s) {
  for (int i = 0; i < s.length(); i++)
    if (s[i] == ':')
      s[i] = ' ';
  stringstream ss;
  ss << s;
  int three[3];
  ss >> three[0] >> three[1] >> three[2];

  int sum = 0;
  sum += three[0] * 60 * 60 + three[1] * 60 + three[2];
  return sum;
}

bool cmp(const P &p, const P &q) {
  if (p.F != q.F)
    return p.F < q.F;
  return p.F < q.S;
}

template <class T> class RMQ_segment {
private:
  int n;
  T dat[2 * MAX_N - 1];

public:
  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      dat[i] = 0;
  }

  void update(int k, T a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    else if (a <= l && r <= b)
      return dat[k];

    T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }

  T query(int a, int b) { return _query(a, b, 0, 0, n); }
};

int main() {
  int n;
  // clock_t start,end;
  // start = clock();

  while (cin >> n && n) {
    vector<P> vec;
    vector<P> sor;
    rep(i, n) {
      string st, ed;
      cin >> st >> ed;
      vec.pb(P(get_time(st), get_time(ed)));
      sor.pb(P(get_time(ed) - get_time(st), i));
    }
    sort(all(sor), cmp);
    // cout<< "---------" << endl;
    // rep(i,n)cout<< vec[i].F << " " << vec[i].S << endl;
    // cout << "--------" << endl;
    RMQ_segment<int> rmq;
    rmq.init(get_time("24:00:01"));

    rep(i, n) {
      int index = sor[i].S;
      int res = rmq.query(vec[index].F, vec[index].S);
      REP(j, vec[index].F, vec[index].S) rmq.update(j, res + 1);
    }
    cout << rmq.query(0, get_time("24:00:01")) << endl;
  }
  // end = clock();
  // cout << setiosflags(ios::fixed) << setprecision(10) <<
  // (double)(end-start)/CLOCKS_PER_SEC << endl;
  return 0;
}

/*
Way to check the time of processing speed

clock_t start,end;
start = clock();

------function-------

end = clock();
cout << setiosflags(ios::fixed) << setprecision(10) <<
(double)(end-start)/CLOCKS_PER_SEC << endl;


*/