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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
  int n, m;
  cin >> n >> m;
  vector<pii> ab(m);
  REP(i, m) { cin >> ab[i].first >> ab[i].second; }

  int prev = 0;
  int cnt = 0;
  while (prev < n) {
    int best = -1;
    REP(i, m) {
      if (ab[i].first <= prev + 1 &&
          (best == -1 || ab[best].second < ab[i].second)) {
        best = i;
      }
    }
    if (best == -1 || prev == ab[best].second) {
      cout << "Impossible" << endl;
      exit(0);
    }
    prev = ab[best].second;
    cnt++;
  }
  if (prev == n) {
    cout << cnt << endl;
  } else {
    cout << "Impossible" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
  int n, m;
  cin >> n >> m;
  vector<pii> ab(m);
  REP(i, m) { cin >> ab[i].first >> ab[i].second; }

  bool flag = false;
  REP(i, m) {
    if (ab[i].second == n) {
      flag = true;
      break;
    }
  }
  if (!flag) {
    cout << "Impossible" << endl;
    exit(0);
  }

  int prev = 0;
  int cnt = 0;
  while (prev < n) {
    int best = -1;
    REP(i, m) {
      if (ab[i].first <= prev + 1 &&
          (best == -1 || ab[best].second < ab[i].second)) {
        best = i;
      }
    }
    if (best == -1) {
      cout << "Impossible" << endl;
      exit(0);
    }
    prev = ab[best].second;
    cnt++;
  }
  if (prev == n) {
    cout << cnt << endl;
  } else {
    cout << "Impossible" << endl;
  }
}