Problem p03061 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

template <class T, class F> struct segment_tree {
  std::vector<T> data;
  int n;
  F f;
  T identity;
  segment_tree() {}
  segment_tree(int sz, T ide, F _f) : f(_f), identity(ide) {
    n = 1;
    while (n < sz)
      n <<= 1;
    data.resize(2 * n, identity);
  }
  segment_tree(std::vector<T> const &v, T ide, F _f) : f(_f), identity(ide) {
    build(v);
  }
  void build(std::vector<T> const &v) {
    int sz = v.size();
    n = 1;
    while (n < sz)
      n <<= 1;
    data.resize(2 * n, identity);
    for (int i = 0; i < sz; ++i)
      data[i + n] = v[i];
    for (int i = n - 1; i > 0; --i)
      data[i] = f(data[i << 1 | 0], data[i << 1 | 1]);
  }
  void set(int i, T x) {
    i += n;
    data[i] = x;
    while (i, i >>= 1)
      data[i] = f(data[i << 1 | 0], data[i << 1 | 1]);
  }
  T query(int l, int r) const {
    l += n, r += n;
    T x = identity;
    while (l < r) {
      if (l & 1)
        x = f(x, data[l++]);
      if (r & 1)
        x = f(x, data[--r]);
      l >>= 1, r >>= 1;
    }
    return x;
  }
  T operator[](int i) const { return data[i + n]; }
};
template <class T, class F> auto make_segtree(int n, T ide, F f) {
  return segment_tree<T, F>(n, ide, f);
}
template <class T, class F>
auto make_segtree(std::vector<T> const &v, T ide, F f) {
  return segment_tree<T, F>(v, ide, f);
}

signed main() {
  int n;
  cin >> n;
  std::vector<int64_t> a(n);
  for (auto &ai : a)
    cin >> ai;
  auto gcd = [](auto a, auto b) {
    while (1) {
      a %= b;
      std::swap(a, b);
    }
    return a;
  };
  auto seg = make_segtree<int64_t>(a, 0, gcd);
  int64_t ans = 1;
  for (int i = 0; i < n; ++i) {
    seg.set(i, 0);
    ans = std::max(ans, seg.query(0, n));
    seg.set(i, a[i]);
  }
  cout << (ans) << endl;
}

Fixed submission:
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

template <class T, class F> struct segment_tree {
  std::vector<T> data;
  int n;
  F f;
  T identity;
  segment_tree() {}
  segment_tree(int sz, T ide, F _f) : f(_f), identity(ide) {
    n = 1;
    while (n < sz)
      n <<= 1;
    data.resize(2 * n, identity);
  }
  segment_tree(std::vector<T> const &v, T ide, F _f) : f(_f), identity(ide) {
    build(v);
  }
  void build(std::vector<T> const &v) {
    int sz = v.size();
    n = 1;
    while (n < sz)
      n <<= 1;
    data.resize(2 * n, identity);
    for (int i = 0; i < sz; ++i)
      data[i + n] = v[i];
    for (int i = n - 1; i > 0; --i)
      data[i] = f(data[i << 1 | 0], data[i << 1 | 1]);
  }
  void set(int i, T x) {
    i += n;
    data[i] = x;
    while (i, i >>= 1)
      data[i] = f(data[i << 1 | 0], data[i << 1 | 1]);
  }
  T query(int l, int r) const {
    l += n, r += n;
    T x = identity;
    while (l < r) {
      if (l & 1)
        x = f(x, data[l++]);
      if (r & 1)
        x = f(x, data[--r]);
      l >>= 1, r >>= 1;
    }
    return x;
  }
  T operator[](int i) const { return data[i + n]; }
};
template <class T, class F> auto make_segtree(int n, T ide, F f) {
  return segment_tree<T, F>(n, ide, f);
}
template <class T, class F>
auto make_segtree(std::vector<T> const &v, T ide, F f) {
  return segment_tree<T, F>(v, ide, f);
}

signed main() {
  int n;
  cin >> n;
  std::vector<int64_t> a(n);
  for (auto &ai : a)
    cin >> ai;
  auto gcd = [](auto a, auto b) {
    while (b) {
      a %= b;
      std::swap(a, b);
    }
    return a;
  };
  auto seg = make_segtree<int64_t>(a, 0, gcd);
  int64_t ans = 1;
  for (int i = 0; i < n; ++i) {
    seg.set(i, 0);
    ans = std::max(ans, seg.query(0, n));
    seg.set(i, a[i]);
  }
  cout << (ans) << endl;
}