Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n * m) / gcd(n, m); // gl=xy
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;

// ここから

using P = pair<int, int>;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<P>> v(n);
  rep(i, m) {
    int p, y;
    cin >> p >> y;
    p--;
    v[p].eb(y, i);
  }
  vector<P> ans(m);
  rep(i, m) {
    if (v[i].size() > 1)
      sort(v[i].begin(), v[i].end());
    rep(j, v[i].size()) { ans[v[i][j].second] = P{i + 1, j + 1}; }
  }
  rep(i, m) {
    string s = to_string(ans[i].first), t = to_string(ans[i].second);
    while (s.size() < 6)
      s = "0" + s;
    while (t.size() < 6)
      t = "0" + t;
    cout << s << t << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n * m) / gcd(n, m); // gl=xy
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;

// ここから

using P = pair<int, int>;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<P>> v(n);
  rep(i, m) {
    int p, y;
    cin >> p >> y;
    p--;
    v[p].eb(y, i);
  }
  vector<P> ans(m);
  rep(i, n) {
    if (v[i].size() > 1)
      sort(v[i].begin(), v[i].end());
    rep(j, v[i].size()) { ans[v[i][j].second] = P{i + 1, j + 1}; }
  }
  rep(i, m) {
    string s = to_string(ans[i].first), t = to_string(ans[i].second);
    while (s.size() < 6)
      s = "0" + s;
    while (t.size() < 6)
      t = "0" + t;
    cout << s << t << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}