Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
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();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#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 (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define UNTIL(p) while (!(p))

// constant
const double EPS = 1e-5;
const double PI = acos(-1.0);

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

#define PUTS(x) cout << (x) << endl;

class Data {
public:
  int n, p, y, id;
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<Data> data(M);
  REP(i, M) {
    data[i].n = i;
    cin >> data[i].p >> data[i].y;
  }
  sort(ALL(data), [](const Data &a, const Data &b) {
    return a.p < b.p ? (1) : (a.y < b.y);
  });

  int x = 1;
  int pre_p = -1;
  REP(i, M) {
    if (data[i].p == pre_p) {
      x++;
    } else {
      x = 1;
    }
    data[i].id = x;
    pre_p = data[i].p;
  }

  sort(ALL(data), [](const Data &a, const Data &b) { return a.n < b.n; });
  REP(i, M) { printf("%06d%06d\n", data[i].p, data[i].id); }
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
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();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#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 (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define UNTIL(p) while (!(p))

// constant
const double EPS = 1e-5;
const double PI = acos(-1.0);

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

#define PUTS(x) cout << (x) << endl;

class Data {
public:
  int n, p, y, id;
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<Data> data(M);
  REP(i, M) {
    data[i].n = i;
    cin >> data[i].p >> data[i].y;
  }
  sort(ALL(data), [](const Data &a, const Data &b) {
    if (a.p == b.p) {
      return a.y < b.y;
    } else {
      return a.p < b.p;
    }
  });

  int x = 1;
  int pre_p = -1;
  REP(i, M) {
    if (data[i].p == pre_p) {
      x++;
    } else {
      x = 1;
    }
    data[i].id = x;
    pre_p = data[i].p;
  }

  sort(ALL(data), [](const Data &a, const Data &b) { return a.n < b.n; });
  REP(i, M) { printf("%06d%06d\n", data[i].p, data[i].id); }
}