Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x)                                                      \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template <typename T> void readv(vector<T> &a) { REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

int add0(int n) {
  int k = 0, s = 1;
  while (n >= s) {
    s *= 10;
    k++;
  }
  // string S = "";
  // REP(i, 6 - k) S += '0';
  return 6 - k;
}

signed main() {

  int N, M;
  cin >> N >> M;
  vec P(N), Y(N);
  REP(i, M) cin >> P[i] >> Y[i];

  map<int, vector<Pii>> m;
  set<int> s;
  REP(i, M) {
    m[P[i]].push_back(Pii(Y[i], i));
    s.insert(P[i]);
  }
  for (int n : s)
    SORT(m[n]);

  vec ans(M);
  for (int n : s) {
    REP(i, m[n].size()) ans[m[n][i].second] = i + 1;
  }

  REP(i, M) {
    REP(k, add0(P[i])) cout << '0';
    cout << P[i];
    REP(k, add0(ans[i])) cout << '0';
    cout << ans[i];
    cout << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x)                                                      \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template <typename T> void readv(vector<T> &a) { REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

int add0(int n) {
  int k = 0, s = 1;
  while (n >= s) {
    s *= 10;
    k++;
  }
  // string S = "";
  // REP(i, 6 - k) S += '0';
  return 6 - k;
}

signed main() {

  int N, M;
  cin >> N >> M;
  vec P(M), Y(M);
  REP(i, M) cin >> P[i] >> Y[i];

  map<int, vector<Pii>> m;
  set<int> s;
  REP(i, M) {
    m[P[i]].push_back(Pii(Y[i], i));
    s.insert(P[i]);
  }
  for (int n : s)
    SORT(m[n]);

  vec ans(M);
  for (int n : s) {
    REP(i, m[n].size()) ans[m[n][i].second] = i + 1;
  }

  REP(i, M) {
    REP(k, add0(P[i])) cout << '0';
    cout << P[i];
    REP(k, add0(ans[i])) cout << '0';
    cout << ans[i];
    cout << endl;
  }

  return 0;
}