Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long
#define ld long double
#define MOD 1000000007
#define u128 __int128
#define ff first
#define ss second
#define INTMAX 1000000009
#define N 100050

ll bmexp(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

void solve() {}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll seg[2 * N];
ll a[N], n;
void build(int node, int l, int r) {
  if (l == r)
    seg[node] = a[l];
  else {
    int mid = l + r >> 1;
    build(2 * node + 1, l, mid);
    build(2 * node + 2, mid + 1, r);
    seg[node] = gcd(seg[2 * node + 1], seg[2 * node + 2]);
  }
}

ll q(int node, int l, int r, int x, int y) {
  if (x <= l && y >= r)
    return seg[node];
  else {
    ll ans = 0;
    int mid = l + r >> 1;
    if (x <= mid)
      ans = (ans == 0 ? q(2 * node + 1, l, mid, x, y)
                      : gcd(ans, q(2 * node + 1, l, mid, x, y)));
    if (y > mid)
      ans = (ans == 0 ? q(2 * node + 2, mid + 1, r, x, y)
                      : gcd(ans, q(2 * node + 2, mid + 1, r, x, y)));
    return ans;
  }
}

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

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  build(0, 0, n - 1);
  // for (int i = 0; i < 2 *n - 1; ++i)
  // {
  // 	cout << seg[i] << " ";
  // }
  ll gc = seg[0];
  // cout << gc << " " << q(0, 0, n - 1, 1, n - 1) << " ";
  gc = max(gc, q(0, 0, n - 1, 1, n - 1));
  for (int i = 1; i < n; ++i) {
    ll k = gcd(q(0, 0, n - 1, 0, i - 1), q(0, 0, n - 1, i + 1, n - 1));
    gc = max(gc, k);
  }
  cout << gc;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long
#define ld long double
#define MOD 1000000007
#define u128 __int128
#define ff first
#define ss second
#define INTMAX 1000000009
#define N 100050

ll bmexp(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

void solve() {}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll seg[3 * N];
ll a[N], n;
void build(int node, int l, int r) {
  if (l == r)
    seg[node] = a[l];
  else {
    int mid = l + r >> 1;
    build(2 * node + 1, l, mid);
    build(2 * node + 2, mid + 1, r);
    seg[node] = gcd(seg[2 * node + 1], seg[2 * node + 2]);
  }
}

ll q(int node, int l, int r, int x, int y) {
  if (x <= l && y >= r)
    return seg[node];
  else {
    ll ans = 0;
    int mid = l + r >> 1;
    if (x <= mid)
      ans = (ans == 0 ? q(2 * node + 1, l, mid, x, y)
                      : gcd(ans, q(2 * node + 1, l, mid, x, y)));
    if (y > mid)
      ans = (ans == 0 ? q(2 * node + 2, mid + 1, r, x, y)
                      : gcd(ans, q(2 * node + 2, mid + 1, r, x, y)));
    return ans;
  }
}

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

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  build(0, 0, n - 1);
  // for (int i = 0; i < 2 *n - 1; ++i)
  // {
  // 	cout << seg[i] << " ";
  // }
  ll gc = seg[0];
  // cout << gc << " " << q(0, 0, n - 1, 1, n - 1) << " ";
  gc = max(gc, q(0, 0, n - 1, 1, n - 1));
  for (int i = 1; i < n; ++i) {
    ll k = gcd(q(0, 0, n - 1, 0, i - 1), q(0, 0, n - 1, i + 1, n - 1));
    gc = max(gc, k);
  }
  cout << gc;
  return 0;
}