Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
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();
}
template <class T> inline T sqr(T x) { return x * x; }
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 PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) sort((c).rbegin(), (c).rend())
#define UNIQUE(c)                                                              \
  SORT(c);                                                                     \
  (c).erase(unique((c).begin(), (c).end()), (c).end());
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double INF = 1e7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

void solve(LL N, LL M, vector<LL> a, vector<LL> b) {
  vector<PII> S;
  S.resize(M);
  REP(i, M) { S[i] = MP(a[i], b[i]); }
  sort(S.begin(), S.end(),
       [](const PII &x, const PII &y) { return x.second < y.second; });

  int index = 0;
  LL r = 0;
  int sum = 0;
  while (index < M) {
    r = S[index].second - 1;
    sum++;
    while (S[index].first <= r) {
      index++;
    }
  }
  cout << sum << endl;
}

int main() {
  LL N;
  LL M;
  scanf("%lld", &N);
  scanf("%lld", &M);
  vector<LL> b(M - 1 + 1);
  vector<LL> a(M - 1 + 1);
  for (int i = 0; i <= M - 1; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
  }
  solve(N, M, a, b);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
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();
}
template <class T> inline T sqr(T x) { return x * x; }
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 PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) sort((c).rbegin(), (c).rend())
#define UNIQUE(c)                                                              \
  SORT(c);                                                                     \
  (c).erase(unique((c).begin(), (c).end()), (c).end());
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double INF = 1e7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

void solve(LL N, LL M, vector<LL> a, vector<LL> b) {
  vector<PII> S;
  S.resize(M);
  REP(i, M) { S[i] = MP(a[i], b[i]); }
  SORT(S);
  stable_sort(S.begin(), S.end(),
              [](const PII &x, const PII &y) { return x.second < y.second; });

  int index = 0;
  LL r = 0;
  int sum = 0;
  while (index < M) {
    r = S[index].second - 1;
    sum++;
    while (S[index].first <= r) {
      index++;
    }
  }
  cout << sum << endl;
}

int main() {
  LL N;
  LL M;
  scanf("%lld", &N);
  scanf("%lld", &M);
  vector<LL> b(M - 1 + 1);
  vector<LL> a(M - 1 + 1);
  for (int i = 0; i <= M - 1; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
  }
  solve(N, M, a, b);
  return 0;
}