Problem p03221 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
                   _ooOoo_
                  o8888888o
                  88" . "88　AC!AC!
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         pass System Test!
*/
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ul(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ul(var) = (max)-1; (var) + 1 > (min); --(var))
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll MOD = 1e9 + 7;
// vector<ld> vec;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VV vec(n + 1);
  VP reg(k);
  rep(i, k) {
    int x, y;
    cin >> x >> y;
    vec[x].push_back(y);
    reg[i] = make_pair(x, y);
  }
  rep(i, n + 1) { sort(all(vec[i])); }
  rep(i, k) {
    int x =
        find(all(vec[reg[i].first]), reg[i].second) - vec[reg[i].first].begin();
    x++;
    string s1 = to_string(reg[i].first);
    string s2 = to_string(x);
    rep(i, 6 - s1.size()) { cout << '0'; }
    cout << s1;
    rep(i, 6 - s2.size()) { cout << '0'; }
    cout << s2 << endl;
  }
}


Fixed submission:
/*
                   _ooOoo_
                  o8888888o
                  88" . "88　AC!AC!
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         pass System Test!
*/
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ul(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ul(var) = (max)-1; (var) + 1 > (min); --(var))
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll MOD = 1e9 + 7;
// vector<ld> vec;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VV vec(n + 1);
  VP reg(k);
  rep(i, k) {
    int x, y;
    cin >> x >> y;
    vec[x].push_back(y);
    reg[i] = make_pair(x, y);
  }
  rep(i, n + 1) { sort(all(vec[i])); }
  rep(i, k) {
    int x = lower_bound(all(vec[reg[i].first]), reg[i].second) -
            vec[reg[i].first].begin();
    x++;
    string s1 = to_string(reg[i].first);
    string s2 = to_string(x);
    rep(i, 6 - s1.size()) { cout << '0'; }
    cout << s1;
    rep(i, 6 - s2.size()) { cout << '0'; }
    cout << s2 << endl;
  }
}