Problem p02919 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int M = 1e3 + 5;
int seg[M * 4], arr[M], o[M], n;

int build(int l, int r, int i) {
  if (l == r)
    seg[i] = arr[l];
  else
    seg[i] = max(build(l, (l + r) / 2, i * 2 + 1),
                 build((l + r) / 2 + 1, r, i * 2 + 2));
  return seg[i];
}

int q(int l, int r, int L, int R, int i) {
  if (L >= l && R <= r)
    return seg[i];
  if (L > r || R < l)
    return -1;
  return max(q(l, r, L, (L + R) / 2, i * 2 + 1),
             q(l, r, (L + R) / 2 + 1, R, i * 2 + 2));
}

int firstGreater(int l, int r, int x) {
  if (l == r)
    return (arr[l] > x ? arr[l] : -1);

  int mid = (l + r) / 2;

  if (q(l, mid, 0, n + 3, 0) > x)
    return firstGreater(l, mid, x);
  return firstGreater(mid + 1, r, x);
}

int lastGreater(int l, int r, int x) {
  if (l == r)
    return (arr[l] > x ? arr[l] : -1);

  int mid = (l + r) / 2;

  if (q(mid + 1, r, 0, n + 3, 0) > x)
    return lastGreater(mid + 1, r, x);
  return lastGreater(l, mid, x);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;

  long long ans = 0;

  arr[0] = n + 1;
  arr[1] = n + 2;
  arr[n + 2] = n + 3;
  arr[n + 3] = n + 4;

  o[n + 1] = 0;
  o[n + 2] = 1;
  o[n + 3] = n + 2;
  o[n + 4] = n + 3;

  for (int i = 2; i <= n + 1; i++)
    cin >> arr[i], o[arr[i]] = i;
  build(0, n + 3, 0);

  for (int i = 2; i <= n + 1; i++) {
    int j, r, l;
    j = o[firstGreater(i + 1, n + 3, arr[i])];
    r = o[firstGreater(j + 1, n + 3, arr[i])];
    l = o[lastGreater(0, i - 1, arr[i])];

    if (j <= n + 1)
      ans += 1ll * (r - j) * (i - l) * arr[i];

    j = o[lastGreater(0, i - 1, arr[i])];
    l = o[lastGreater(0, j - 1, arr[i])];
    r = o[firstGreater(i + 1, n + 3, arr[i])];

    if (j > 1)
      ans += 1ll * (r - i) * (j - l) * arr[i];
  }

  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int M = 1e5 + 5;
int seg[M * 4], arr[M], o[M], n;

int build(int l, int r, int i) {
  if (l == r)
    seg[i] = arr[l];
  else
    seg[i] = max(build(l, (l + r) / 2, i * 2 + 1),
                 build((l + r) / 2 + 1, r, i * 2 + 2));
  return seg[i];
}

int q(int l, int r, int L, int R, int i) {
  if (L >= l && R <= r)
    return seg[i];
  if (L > r || R < l)
    return -1;
  return max(q(l, r, L, (L + R) / 2, i * 2 + 1),
             q(l, r, (L + R) / 2 + 1, R, i * 2 + 2));
}

int firstGreater(int l, int r, int x) {
  if (l == r)
    return (arr[l] > x ? arr[l] : -1);

  int mid = (l + r) / 2;

  if (q(l, mid, 0, n + 3, 0) > x)
    return firstGreater(l, mid, x);
  return firstGreater(mid + 1, r, x);
}

int lastGreater(int l, int r, int x) {
  if (l == r)
    return (arr[l] > x ? arr[l] : -1);

  int mid = (l + r) / 2;

  if (q(mid + 1, r, 0, n + 3, 0) > x)
    return lastGreater(mid + 1, r, x);
  return lastGreater(l, mid, x);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;

  long long ans = 0;

  arr[0] = n + 1;
  arr[1] = n + 2;
  arr[n + 2] = n + 3;
  arr[n + 3] = n + 4;

  o[n + 1] = 0;
  o[n + 2] = 1;
  o[n + 3] = n + 2;
  o[n + 4] = n + 3;

  for (int i = 2; i <= n + 1; i++)
    cin >> arr[i], o[arr[i]] = i;
  build(0, n + 3, 0);

  for (int i = 2; i <= n + 1; i++) {
    int j, r, l;
    j = o[firstGreater(i + 1, n + 3, arr[i])];
    r = o[firstGreater(j + 1, n + 3, arr[i])];
    l = o[lastGreater(0, i - 1, arr[i])];

    if (j <= n + 1)
      ans += 1ll * (r - j) * (i - l) * arr[i];

    j = o[lastGreater(0, i - 1, arr[i])];
    l = o[lastGreater(0, j - 1, arr[i])];
    r = o[firstGreater(i + 1, n + 3, arr[i])];

    if (j > 1)
      ans += 1ll * (r - i) * (j - l) * arr[i];
  }

  cout << ans << '\n';

  return 0;
}