Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = int(A); I < int(B); ++I)
#define FORR(I, A, B) for (ll I = int((B)-1); I >= int(A); --I)
#define TO(x, t, f) ((x) ? (t) : (f))
#define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x, v)                                                             \
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v  x is sorted
#define POSU(x, v)                                                             \
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v  x is sorted
#define NUM(x, v) (POSU(x, v) - POSL(x, v))        // x is sorted
#define REV(x) (reverse(x.begin(), x.end()))       // reverse
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll c = gcd(a, b);
  return ((a / c) * (b / c) * c);
} // saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(), x.end())
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

class Seg_Tree {
public:
  vector<int> dat;
  int n;
  void initsize(int n0) {
    int k = 1;
    while (1) {
      if (n0 <= k) {
        n = k;
        dat.resize(2 * n - 1);
        for (int i = 0; i < 2 * n - 1; i++)
          dat[i] = 1;
        break;
      }
      k *= 2;
    }
  }

  // i banme wo x nisuru
  void update(int i, int x) {
    i += n - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = gcd(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }

  //[a,b)
  int query0(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return dat[a + n - 1];
    if (a <= l && r <= b)
      return dat[k];
    else {
      int vl = query0(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query0(a, b, k * 2 + 2, (l + r) / 2, r);
      return gcd(vl, vr);
    }
  }

  // return min [a,b)
  int query(int a, int b) { return query0(a, b, 0, 0, n); }
};

int main() {
  int N;
  cin >> N;
  Seg_Tree sg;
  sg.initsize(N);
  FOR(i, 0, N) {
    ll k;
    cin >> k;
    sg.update(i, k);
  }

  int ans = max(sg.query(1, N), sg.query(0, N - 1));
  FOR(i, 0, N) {
    int p = gcd(sg.query(0, i), sg.query(i + 1, N));
    ans = max(ans, p);
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = int(A); I < int(B); ++I)
#define FORR(I, A, B) for (ll I = int((B)-1); I >= int(A); --I)
#define TO(x, t, f) ((x) ? (t) : (f))
#define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x, v)                                                             \
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v  x is sorted
#define POSU(x, v)                                                             \
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v  x is sorted
#define NUM(x, v) (POSU(x, v) - POSL(x, v))        // x is sorted
#define REV(x) (reverse(x.begin(), x.end()))       // reverse
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll c = gcd(a, b);
  return ((a / c) * (b / c) * c);
} // saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(), x.end())
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

class Seg_Tree {
public:
  vector<int> dat;
  int n;
  void initsize(int n0) {
    int k = 1;
    while (1) {
      if (n0 <= k) {
        n = k;
        dat.resize(2 * n - 1);
        for (int i = 0; i < 2 * n - 1; i++)
          dat[i] = 1;
        break;
      }
      k *= 2;
    }
  }

  // i banme wo x nisuru
  void update(int i, int x) {
    i += n - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = gcd(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }

  //[a,b)
  int query0(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return dat[a + n - 1];
    if (a <= l && r <= b)
      return dat[k];
    else {
      int vl = query0(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query0(a, b, k * 2 + 2, (l + r) / 2, r);
      return gcd(vl, vr);
    }
  }

  // return min [a,b)
  int query(int a, int b) { return query0(a, b, 0, 0, n); }
};

int main() {
  int N;
  cin >> N;
  Seg_Tree sg;
  sg.initsize(N);
  FOR(i, 0, N) {
    ll k;
    cin >> k;
    sg.update(i, k);
  }

  int ans = max(sg.query(1, N), sg.query(0, N - 1));
  FOR(i, 1, N - 1) {
    int p = gcd(sg.query(0, i), sg.query(i + 1, N));
    ans = max(ans, p);
  }

  cout << ans << endl;
}