Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  vll A(N);
  ll B = 316;
  vll backet(10000 / B + 1, -1);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (backet.at(i / B) == -1)
      backet.at(i / B) = A.at(i);
    else
      backet.at(i / B) = __gcd(backet.at(i / B), A.at(i));
  }
  auto func = [&](int left, int right) {
    ll res = -1;
    while (left < right && left % B != 0) {
      if (res == -1)
        res = A.at(left);
      else
        res = __gcd(res, A.at(left));
      left++;
    }
    while (left < right && right % B != 0) {
      right--;
      if (res == -1)
        res = A.at(right);
      else
        res = __gcd(res, A.at(right));
    }
    for (int i = left; i < right; i += B) {
      if (res == -1)
        res = backet.at(i / B);
      else
        res = __gcd(res, backet.at(i / B));
    }
    return res;
  };

  ll ans = -1;
  for (int i = 0; i < N; i++) {
    ll res = -1;
    if (i == 0)
      res = func(1, N);
    else if (i == N - 1)
      res = func(0, N - 1);
    else
      res = __gcd(func(0, i), func(i + 1, N));
    chmax(ans, res);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  vll A(N);
  ll B = 316;
  vll backet(100000 / B + 1, -1);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (backet.at(i / B) == -1)
      backet.at(i / B) = A.at(i);
    else
      backet.at(i / B) = __gcd(backet.at(i / B), A.at(i));
  }
  auto func = [&](int left, int right) {
    ll res = -1;
    while (left < right && left % B != 0) {
      if (res == -1)
        res = A.at(left);
      else
        res = __gcd(res, A.at(left));
      left++;
    }
    while (left < right && right % B != 0) {
      right--;
      if (res == -1)
        res = A.at(right);
      else
        res = __gcd(res, A.at(right));
    }
    for (int i = left; i < right; i += B) {
      if (res == -1)
        res = backet.at(i / B);
      else
        res = __gcd(res, backet.at(i / B));
    }
    return res;
  };

  ll ans = -1;
  for (int i = 0; i < N; i++) {
    ll res = -1;
    if (i == 0)
      res = func(1, N);
    else if (i == N - 1)
      res = func(0, N - 1);
    else
      res = __gcd(func(0, i), func(i + 1, N));
    chmax(ans, res);
  }
  cout << ans << endl;
}