Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <climits>
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define srep(i, begin, end) for (__typeof(end) i = begin; i != end; i++)
#define si(x) int x = scanInt();
#define sll(x) LL x = scanLong();
#define sci(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x);
#define scll(x)                                                                \
  LL x;                                                                        \
  scanf("%lld", &x);
#define pi(x) printf("%d ", x)
#define pll(x) printf("%lld ", x)
#define nl printf("\n")
#define clr(a) memset(a, 0, sizeof(a))
#define PB push_back
#define MP make_pair
using namespace std;
typedef unsigned int UI;            // 32 bit integer
typedef long int LI;                // 32 bit integer
typedef unsigned long int ULI;      // 32 bit unsigned integer
typedef long long int LL;           // 64 bit integer
typedef unsigned long long int ULL; // 64 bit unsigned integer
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
LL mod = 1e9 + 7;

/* Fast I/O */
inline int scanInt() {
  int n = 0;
  char ch = getchar();
  int sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    n = (n << 1) + (n << 3) + (int)(ch - '0');
    ch = getchar();
  }
  return n * sign;
}

inline LL scanLong() {
  LL n = 0;
  char ch = getchar();
  LL sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    n = (n << 1) + (n << 3) + (LL)(ch - '0');
    ch = getchar();
  }
  return n * sign;
}

const LL MAXN = 1e5;

string getSixDigit(LL n) {
  string ret = "";
  while (ret.length() != 6) {
    ret = char('0' + n % 10) + ret;
    n /= 10;
  }
  return ret;
}

struct City {
  LL idx, prefect, year;
  string id;
  void setId(LL x) { id = getSixDigit(prefect) + getSixDigit(x); }
} cities[MAXN];

bool sortByYear(City a, City b) { return a.year < b.year; }

int main() {
  sll(n);
  sll(m);
  vector<City> pre_city[n + 1];
  rep(i, 1, m + 1) {
    cities[i].idx = i;
    cities[i].prefect = scanLong();
    cities[i].year = scanLong();
    pre_city[cities[i].prefect].push_back(cities[i]);
  }
  rep(i, 1, n + 1) {
    sort(pre_city[i].begin(), pre_city[i].end(), sortByYear);
    rep(x, 0, pre_city[i].size()) cities[pre_city[i][x].idx].setId(x + 1);
  }
  rep(i, 1, m + 1) cout << cities[i].id << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <climits>
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define srep(i, begin, end) for (__typeof(end) i = begin; i != end; i++)
#define si(x) int x = scanInt();
#define sll(x) LL x = scanLong();
#define sci(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x);
#define scll(x)                                                                \
  LL x;                                                                        \
  scanf("%lld", &x);
#define pi(x) printf("%d ", x)
#define pll(x) printf("%lld ", x)
#define nl printf("\n")
#define clr(a) memset(a, 0, sizeof(a))
#define PB push_back
#define MP make_pair
using namespace std;
typedef unsigned int UI;            // 32 bit integer
typedef long int LI;                // 32 bit integer
typedef unsigned long int ULI;      // 32 bit unsigned integer
typedef long long int LL;           // 64 bit integer
typedef unsigned long long int ULL; // 64 bit unsigned integer
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
LL mod = 1e9 + 7;

/* Fast I/O */
inline int scanInt() {
  int n = 0;
  char ch = getchar();
  int sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    n = (n << 1) + (n << 3) + (int)(ch - '0');
    ch = getchar();
  }
  return n * sign;
}

inline LL scanLong() {
  LL n = 0;
  char ch = getchar();
  LL sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    n = (n << 1) + (n << 3) + (LL)(ch - '0');
    ch = getchar();
  }
  return n * sign;
}

const LL MAXN = 1e5 + 10;

string getSixDigit(LL n) {
  string ret = "";
  while (ret.length() != 6) {
    ret = char('0' + n % 10) + ret;
    n /= 10;
  }
  return ret;
}

struct City {
  LL idx, prefect, year;
  string id;
  void setId(LL x) { id = getSixDigit(prefect) + getSixDigit(x); }
} cities[MAXN];

bool sortByYear(City a, City b) { return a.year < b.year; }

int main() {
  sll(n);
  sll(m);
  vector<City> pre_city[n + 1];
  rep(i, 1, m + 1) {
    cities[i].idx = i;
    cities[i].prefect = scanLong();
    cities[i].year = scanLong();
    pre_city[cities[i].prefect].push_back(cities[i]);
  }
  rep(i, 1, n + 1) {
    sort(pre_city[i].begin(), pre_city[i].end(), sortByYear);
    rep(x, 0, pre_city[i].size()) cities[pre_city[i][x].idx].setId(x + 1);
  }
  rep(i, 1, m + 1) cout << cities[i].id << endl;
}