Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
// #include<bits/stdc++.h>

using namespace std;
#define ok1 printf("ok1\n");
#define ok2 printf("ok2\n");
#define M 1000000000000000000LL
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = (s); i < (n); i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define REPR(i, s, n) for (int i = (s); i >= (n); (i)--)
#define all(a) (a).begin(), (a).end()
#define reall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define pf push_front
#define MIN(a, b) a = min((a), (b))
#define MAX(a, b) a = max((a), (b))
#define SIZE(v) (int)v.size()
const double pi = acos(-1.0);

typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef deque<ll> dll;
typedef pair<ll, ll> P;
#define mod 1e9 + 7;
ll gcd(ll x, ll y) {
  ll r;
  while ((r = x % y) != 0) {
    x = y;
    y = r;
  }
  return y;
}
ll lcm(ll x, ll y) {
  x /= gcd(x, y);
  y /= gcd(x, y);
  return (x * y);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  vector<P> vec[10005];
  vi p(m), y(m), a(m), b(m);
  rep(i, m) {
    cin >> p[i] >> y[i];
    a[i] = p[i];
    vec[p[i] - 1].pb(P(y[i], i));
  }
  rep(i, n) {
    sort(all(vec[i]));
    rep(j, vec[i].size()) { b[vec[i][j].second] = j; }
  }
  rep(i, m) { printf("%06d%06d\n", a[i], b[i] + 1); }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
// #include<bits/stdc++.h>

using namespace std;
#define ok1 printf("ok1\n");
#define ok2 printf("ok2\n");
#define M 1000000000000000000LL
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = (s); i < (n); i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define REPR(i, s, n) for (int i = (s); i >= (n); (i)--)
#define all(a) (a).begin(), (a).end()
#define reall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define pf push_front
#define MIN(a, b) a = min((a), (b))
#define MAX(a, b) a = max((a), (b))
#define SIZE(v) (int)v.size()
const double pi = acos(-1.0);

typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef deque<ll> dll;
typedef pair<ll, ll> P;
#define mod 1e9 + 7;
ll gcd(ll x, ll y) {
  ll r;
  while ((r = x % y) != 0) {
    x = y;
    y = r;
  }
  return y;
}
ll lcm(ll x, ll y) {
  x /= gcd(x, y);
  y /= gcd(x, y);
  return (x * y);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  vector<P> vec[100005];
  vi p(m), y(m), a(m), b(m);
  rep(i, m) {
    cin >> p[i] >> y[i];
    a[i] = p[i];
    vec[p[i] - 1].pb(P(y[i], i));
  }
  rep(i, n) {
    sort(all(vec[i]));
    rep(j, vec[i].size()) { b[vec[i][j].second] = j; }
  }
  rep(i, m) { printf("%06d%06d\n", a[i], b[i] + 1); }
  return 0;
}