Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int64_t i = 0; i < (int64_t)n; i++)
#define REP_FOR(i, a, b) for (int64_t i = (int64_t)a; i < (int64_t)b; i++)
#define REP_REV(i, a, b) for (int64_t i = (int64_t)a; i > (int64_t)b; i--)
#define FORALL(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define eb(x) emplace_back(x)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int64_t, int64_t> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int64_t> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int64_t infi = 1147483600;
const ll infl = 4e18 + 5;
const char EOL = '\n';
const ll MOD = 998244353;

int main() {
  ll n, m;
  ;
  cin >> n >> m;
  vector<tuple<ll, ll, ll>> P(m);
  for (ll i = 0; i < m; ++i) {
    auto &p = P[i];
    cin >> get<0>(p) >> get<1>(p);
    get<2>(p) = i;
  }

  sort(P.begin(), P.end(), [&](auto &left, auto &right) -> bool {
    return get<1>(left) < get<1>(right);
  });

  vector<ll> counter(n, 0);
  for (auto &p : P) {
    counter[get<0>(p)]++;
    get<1>(p) = counter[get<0>(p)];
  }

  sort(P.begin(), P.end(), [&](auto &left, auto &right) -> bool {
    return get<2>(left) < get<2>(right);
  });

  for (auto &p : P) {
    printf("%06lld%06lld\n", get<0>(p), get<1>(p));
  }
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int64_t i = 0; i < (int64_t)n; i++)
#define REP_FOR(i, a, b) for (int64_t i = (int64_t)a; i < (int64_t)b; i++)
#define REP_REV(i, a, b) for (int64_t i = (int64_t)a; i > (int64_t)b; i--)
#define FORALL(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define eb(x) emplace_back(x)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int64_t, int64_t> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int64_t> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int64_t infi = 1147483600;
const ll infl = 4e18 + 5;
const char EOL = '\n';
const ll MOD = 998244353;

int main() {
  ll n, m;
  ;
  cin >> n >> m;
  vector<tuple<ll, ll, ll>> P(m);
  for (ll i = 0; i < m; ++i) {
    auto &p = P[i];
    cin >> get<0>(p) >> get<1>(p);
    get<2>(p) = i;
  }

  sort(P.begin(), P.end(), [&](auto &left, auto &right) -> bool {
    return get<1>(left) < get<1>(right);
  });

  map<ll, ll> counter;
  for (auto &p : P) {
    counter[get<0>(p)]++;
    get<1>(p) = counter[get<0>(p)];
  }

  sort(P.begin(), P.end(), [&](auto &left, auto &right) -> bool {
    return get<2>(left) < get<2>(right);
  });

  for (auto &p : P) {
    printf("%06lld%06lld\n", get<0>(p), get<1>(p));
  }
}