Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-15
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
int main() {
  ll N;
  int Q;
  scan(N);
  scan(Q);
  rep(i, Q) {
    ll v[2];
    // map<ll,bool> visited;
    vll visit;
    scan(v[0]);
    scan(v[1]);
    // visited[v[0]]=true;
    // visited[v[1]]=true;
    visit.push_back(v[0]);
    visit.push_back(v[1]);
    while (1) {
      if (v[0] == 1 && v[1] == 1) {
        break;
      }
      rep(k, 2) {
        if (v[k] > 1) {
          v[k] = (v[k] - 1) / N + ((v[k] - 1) % N != 0);
          /*if(visited[v[k]]==true){
                  prin(v[k]);
                  flag=1;
                  break;
          }
          visited[v[k]]=true;*/
          visit.push_back(v[k]);
        }
      }
      // if(flag) break;
    }
    sort(visit.begin(), visit.end());
    for (int j = visit.size() - 1; j > 0; j--) {
      if (visit[j] == visit[j - 1]) {
        prin(visit[j]);
        break;
      }
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-15
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
int main() {
  ll N;
  int Q;
  scan(N);
  scan(Q);
  rep(i, Q) {
    ll v[2];
    // map<ll,bool> visited;
    vll visit;
    scan(v[0]);
    scan(v[1]);
    // visited[v[0]]=true;
    // visited[v[1]]=true;
    visit.push_back(v[0]);
    visit.push_back(v[1]);
    if (N == 1) {
      prin(min(v[0], v[1]));
      continue;
    }
    while (1) {
      if (v[0] == 1 && v[1] == 1) {
        break;
      }
      rep(k, 2) {
        if (v[k] > 1) {
          v[k] = (v[k] - 1) / N + ((v[k] - 1) % N != 0);
          /*if(visited[v[k]]==true){
                  prin(v[k]);
                  flag=1;
                  break;
          }
          visited[v[k]]=true;*/
          visit.push_back(v[k]);
        }
      }
      // if(flag) break;
    }
    sort(visit.begin(), visit.end());
    for (int j = visit.size() - 1; j > 0; j--) {
      if (visit[j] == visit[j - 1]) {
        prin(visit[j]);
        break;
      }
    }
  }
  return 0;
}