Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#pragma region _define

#define f(i, a, b) for (int i = a; i < b; i++)
#define f_vI(v, n) f(i, 0, n) cin >> v[i]
#define f_v2I(v1, v2, n) f(i, 0, n) cin >> v1[i] >> v2[i]
#define f_v3I(v1, v2, v3, n) f(i, 0, n) cin >> v1[i] >> v2[i] >> v3[i]
#define f_vO(v, n) f(i, 0, n) cout << v[i] << endl
#define all(a) a.begin(), a.end()
#define size(s) s.size()
#define endl "\n"
#define _y() cout << "Yes" << endl
#define _Y() cout << "YES" << endl
#define _n() cout << "No" << endl
#define _N() cout << "NO" << endl

#pragma endregion

#pragma region _using

using namespace std;

using LL = long long;
using st = string;
using vi = vector<LL>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vs = vector<st>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using si = stack<LL>;
using pi = pair<LL, LL>;
using v_pi = vector<pi>;

#pragma endregion

#pragma region _graph

#define node 50
// bool graph[node][node]; bool visited[node] = { false };
#define link(a, b)                                                             \
  f(i, 0, n) {                                                                 \
    cin >> a[i] >> b[i];                                                       \
    a[i]--;                                                                    \
    b[i]--;                                                                    \
    graph[a[i]][b[i]] = graph[b[i]][a[i]] = true;                              \
  }

#pragma endregion

LL gcd(LL a, LL b) {
  LL r;
  while ((r = a % b) != 0) {
    a = b;
    b = r;
  }
  return b;
}
LL lcm(LL a, LL b) { return (a / gcd(a, b) * b); }

/*****************************************************************************/

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vi p(m), y(m);
  vvi y_num(m + 1);
  f(i, 0, m) {
    cin >> p[i] >> y[i];
    y_num[p[i]].push_back(y[i]);
  }
  f(i, 0, n) { sort(all(y_num[i + 1])); }
  f(i, 0, m) {
    cout << setw(6) << setfill('0') << p[i];
    cout << setw(6) << setfill('0')
         << LL(lower_bound(all(y_num[p[i]]), y[i]) - y_num[p[i]].begin() + 1);
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#pragma region _define

#define f(i, a, b) for (int i = a; i < b; i++)
#define f_vI(v, n) f(i, 0, n) cin >> v[i]
#define f_v2I(v1, v2, n) f(i, 0, n) cin >> v1[i] >> v2[i]
#define f_v3I(v1, v2, v3, n) f(i, 0, n) cin >> v1[i] >> v2[i] >> v3[i]
#define f_vO(v, n) f(i, 0, n) cout << v[i] << endl
#define all(a) a.begin(), a.end()
#define size(s) s.size()
#define endl "\n"
#define _y() cout << "Yes" << endl
#define _Y() cout << "YES" << endl
#define _n() cout << "No" << endl
#define _N() cout << "NO" << endl

#pragma endregion

#pragma region _using

using namespace std;

using LL = long long;
using st = string;
using vi = vector<LL>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vs = vector<st>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using si = stack<LL>;
using pi = pair<LL, LL>;
using v_pi = vector<pi>;

#pragma endregion

#pragma region _graph

#define node 50
// bool graph[node][node]; bool visited[node] = { false };
#define link(a, b)                                                             \
  f(i, 0, n) {                                                                 \
    cin >> a[i] >> b[i];                                                       \
    a[i]--;                                                                    \
    b[i]--;                                                                    \
    graph[a[i]][b[i]] = graph[b[i]][a[i]] = true;                              \
  }

#pragma endregion

LL gcd(LL a, LL b) {
  LL r;
  while ((r = a % b) != 0) {
    a = b;
    b = r;
  }
  return b;
}
LL lcm(LL a, LL b) { return (a / gcd(a, b) * b); }

/*****************************************************************************/

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vi p(100000), y(100000);
  vvi y_num(100001);
  f(i, 0, m) {
    cin >> p[i] >> y[i];
    y_num[p[i]].push_back(y[i]);
  }
  f(i, 0, n) { sort(all(y_num[i + 1])); }
  f(i, 0, m) {
    cout << setw(6) << setfill('0') << p[i];
    cout << setw(6) << setfill('0')
         << LL(lower_bound(all(y_num[p[i]]), y[i]) - y_num[p[i]].begin() + 1);
    cout << endl;
  }
  return 0;
}