Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define ps push
#define pf push_front
#define mp make_pair
#define And &&
#define Or ||
#define ppb pop_back
#define ppf pop_front
#define inf INT_MAX
#define sz(name) name.size()
#define len(name) name.length()
#define ig cin.ignore()
#define Sort(name) sort(name.begin(), name.end())
#define For(name, start, finish) for (int name = start; name < finish; name++)
#define fast_in_out                                                            \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

// int row[]={-1,0,1,0};
// int col[]={0,1,0,-1};

const ll N = 1e5 + 10;
const ll M = 1e9 + 7;

ll Max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll Min(ll a, ll b) {
  if (a > b)
    return b;
  return a;
}
ll big_mul(ll a, ll b, ll m) {
  ll x = 0, y = a % m;
  while (b > 0) {
    if (b & 1)
      x = (x + y) % m;
    y = (2 * y) % m;
    b >>= 1;
  }
  return x % m;
}
ll big_mod(ll a, ll b, ll m) {
  ll x = 1, y = a % m;
  while (b > 0) {
    if (b & 1)
      x = (x * y) % m;
    y = (y * y) % m;
    b >>= 1;
  }
  return x % m;
}
ll Big_mod(ll a, ll b, ll m) {
  ll x = 1, y = a % m;
  while (b > 0) {
    if (b & 1)
      x = big_mul(x, y, m);
    y = big_mul(y, y, m);
    b >>= 1;
  }
  return x % m;
}
ll mod_inverse(ll a, ll m) {
  return big_mod(a, m - 2, m);
} // a^-1 mod m = a^(m-2) mod m

vector<ll> vec;
vector<pair<ll, pair<ll, ll>>> tree(5 * N);

void build_tree(ll index, ll l, ll r) {

  if (l == r) {
    tree[index] = make_pair(vec[l], make_pair(l, r));
    return;
  }

  int mid = (l + r) / 2;

  build_tree(2 * index + 1, l, mid);
  build_tree(2 * index + 2, mid + 1, r);

  tree[index].second.first = l;
  tree[index].second.second = r;
  tree[index].first = Max(tree[2 * index + 1].first, tree[2 * index + 2].first);
}

ll ans(ll index, ll a, ll b) {

  ll l = tree[index].second.first;
  ll r = tree[index].second.second;

  if (a > r || b < l)
    return 0;

  if (a <= l && b >= r)
    return tree[index].first;

  return Max(ans(2 * index + 1, a, b), ans(2 * index + 2, a, b));
}

int main() {
  fast_in_out;

  int n;
  cin >> n;

  For(i, 0, n) {
    ll a;
    cin >> a;
    vec.pb(a);
  }

  build_tree(0, 0, n - 1);

  For(i, 0, n) {
    ll p = ans(0, 0, i - 1);
    ll q = ans(0, i + 1, n - 1);

    cout << Max(p, q) << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define ps push
#define pf push_front
#define mp make_pair
#define And &&
#define Or ||
#define ppb pop_back
#define ppf pop_front
#define inf INT_MAX
#define sz(name) name.size()
#define len(name) name.length()
#define ig cin.ignore()
#define Sort(name) sort(name.begin(), name.end())
#define For(name, start, finish) for (int name = start; name < finish; name++)
#define fast_in_out                                                            \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

// int row[]={-1,0,1,0};
// int col[]={0,1,0,-1};

const ll N = 2e5 + 10;
const ll M = 1e9 + 7;

ll Max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll Min(ll a, ll b) {
  if (a > b)
    return b;
  return a;
}
ll big_mul(ll a, ll b, ll m) {
  ll x = 0, y = a % m;
  while (b > 0) {
    if (b & 1)
      x = (x + y) % m;
    y = (2 * y) % m;
    b >>= 1;
  }
  return x % m;
}
ll big_mod(ll a, ll b, ll m) {
  ll x = 1, y = a % m;
  while (b > 0) {
    if (b & 1)
      x = (x * y) % m;
    y = (y * y) % m;
    b >>= 1;
  }
  return x % m;
}
ll Big_mod(ll a, ll b, ll m) {
  ll x = 1, y = a % m;
  while (b > 0) {
    if (b & 1)
      x = big_mul(x, y, m);
    y = big_mul(y, y, m);
    b >>= 1;
  }
  return x % m;
}
ll mod_inverse(ll a, ll m) {
  return big_mod(a, m - 2, m);
} // a^-1 mod m = a^(m-2) mod m

vector<ll> vec;
vector<pair<ll, pair<ll, ll>>> tree(5 * N);

void build_tree(ll index, ll l, ll r) {

  if (l == r) {
    tree[index] = make_pair(vec[l], make_pair(l, r));
    return;
  }

  int mid = (l + r) / 2;

  build_tree(2 * index + 1, l, mid);
  build_tree(2 * index + 2, mid + 1, r);

  tree[index].second.first = l;
  tree[index].second.second = r;
  tree[index].first = Max(tree[2 * index + 1].first, tree[2 * index + 2].first);
}

ll ans(ll index, ll a, ll b) {

  ll l = tree[index].second.first;
  ll r = tree[index].second.second;

  if (a > r || b < l)
    return 0;

  if (a <= l && b >= r)
    return tree[index].first;

  return Max(ans(2 * index + 1, a, b), ans(2 * index + 2, a, b));
}

int main() {
  fast_in_out;

  int n;
  cin >> n;

  For(i, 0, n) {
    ll a;
    cin >> a;
    vec.pb(a);
  }

  build_tree(0, 0, n - 1);

  For(i, 0, n) {
    ll p = ans(0, 0, i - 1);
    ll q = ans(0, i + 1, n - 1);

    cout << Max(p, q) << endl;
  }

  return 0;
}