Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <time.h>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 1000000007LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a)                                                                \
  REP(d, a.size()) {                                                           \
    cout << a[d];                                                              \
    if (d != a.size() - 1)                                                     \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v)                                                              \
  sort(ALL(v));                                                                \
  v.erase(unique(ALL(v)), v.end());
#define pb push_back

/* --------------------------------------- */

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, q;
  cin >> n >> q;
  REP(i, q) {
    ll v, w;
    cin >> v >> w;
    v--;
    w--;
    while (1) {
      if (v == w) {
        cout << v + 1 << endl;
        break;
      }
      if (v > w)
        v = (v - 1) / n;
      else
        w = (w - 1) / n;
    }
  }
  return 0;
}

/* --------------------------------------- */


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <time.h>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 1000000007LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a)                                                                \
  REP(d, a.size()) {                                                           \
    cout << a[d];                                                              \
    if (d != a.size() - 1)                                                     \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v)                                                              \
  sort(ALL(v));                                                                \
  v.erase(unique(ALL(v)), v.end());
#define pb push_back

/* --------------------------------------- */

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, q;
  cin >> n >> q;
  REP(i, q) {
    ll v, w;
    cin >> v >> w;
    if (n == 1) {
      cout << min(v, w) << endl;
      continue;
    }
    v--;
    w--;
    while (1) {
      if (v == w) {
        cout << v + 1 << endl;
        break;
      }
      if (v > w)
        v = (v - 1) / n;
      else
        w = (w - 1) / n;
    }
  }
  return 0;
}

/* --------------------------------------- */