Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define GET_REP(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_REP(__VA_ARGS__, irep, _rep)(__VA_ARGS__)
#define rep1(...) GET_REP(__VA_ARGS__, irep1, _rep1)(__VA_ARGS__)
#define _rep(i, n) irep(i, 0, n)
#define _rep1(i, n) irep1(i, 1, n)
#define irep(i, a, n) for (int i = a; i < (int)(n); ++i)
#define irep1(i, a, n) for (int i = a; i <= (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; --i)
#define allrep(X, x) for (auto &&X : x)
#define all(x) begin(x), end(x)
#ifdef LOCAL
#include "../../Lib/cout_container.hpp"
#define debug(x) cout << #x " => " << (x) << endl
#else
#define debug(x) 0
#endif
using lint = long long;
constexpr int MOD = (int)1e9 + 7;
constexpr double EPS = 1e-9;
using namespace std;
namespace {
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} INIT;
} // namespace

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> py(m);
  rep(i, m) cin >> py[i].first >> py[i].second;
  vector<vector<int>> ken(n + 1);
  rep(i, m) ken[py[i].first].push_back(py[i].second);
  rep(i, m) sort(all(ken[i]));
  vector<string> ans(m);
  rep(i, m) {
    int rank = lower_bound(all(ken[py[i].first]), py[i].second) -
               ken[py[i].first].begin() + 1;
    stringstream ss;
    ss << setfill('0') << right << setw(6) << py[i].first;
    ss << setfill('0') << right << setw(6) << rank;
    ss >> ans[i];
  }
  rep(i, m) cout << ans[i] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define GET_REP(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_REP(__VA_ARGS__, irep, _rep)(__VA_ARGS__)
#define rep1(...) GET_REP(__VA_ARGS__, irep1, _rep1)(__VA_ARGS__)
#define _rep(i, n) irep(i, 0, n)
#define _rep1(i, n) irep1(i, 1, n)
#define irep(i, a, n) for (int i = a; i < (int)(n); ++i)
#define irep1(i, a, n) for (int i = a; i <= (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; --i)
#define allrep(X, x) for (auto &&X : x)
#define all(x) begin(x), end(x)
#ifdef LOCAL
#include "../../Lib/cout_container.hpp"
#define debug(x) cout << #x " => " << (x) << endl
#else
#define debug(x) 0
#endif
using lint = long long;
constexpr int MOD = (int)1e9 + 7;
constexpr double EPS = 1e-9;
using namespace std;
namespace {
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} INIT;
} // namespace

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> py(m);
  rep(i, m) cin >> py[i].first >> py[i].second;
  vector<vector<int>> ken(n + 1);
  rep(i, m) ken[py[i].first].push_back(py[i].second);
  rep1(i, n) sort(all(ken[i]));
  vector<string> ans(m);
  rep(i, m) {
    int rank = lower_bound(all(ken[py[i].first]), py[i].second) -
               ken[py[i].first].begin() + 1;
    stringstream ss;
    ss << setfill('0') << right << setw(6) << py[i].first;
    ss << setfill('0') << right << setw(6) << rank;
    ss >> ans[i];
  }
  rep(i, m) cout << ans[i] << "\n";
  return 0;
}