Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define iter(c) __typeof((c).begin())
#define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++)
#define mem(a) memset(a, 0, sizeof(a))
#define pd(a) printf("%.10f\n", a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin >>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout << '\n'
template <C T> void pr(T a) {
  cout << a;
  ln;
}
template <C T, C T2> void pr(T a, T2 b) {
  cout << a << ' ' << b;
  ln;
}
template <C T, C T2, C T3> void pr(T a, T2 b, T3 c) {
  cout << a << ' ' << b << ' ' << c;
  ln;
}
template <C T> void PR(T a, int n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  ln;
}
bool check(int n, int m, int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < m;
}
const ll MAX = 1000000007, MAXL = 1LL << 60, dx[4] = {-1, 0, 1, 0},
         dy[4] = {0, 1, 0, -1};
typedef pair<int, int> P;
class RMQ {
public:
  int n, dat[500010];
  void init(int _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    fill(dat, dat + 2 * n - 1, MAX);
  }
  void update(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  int query(int a, int b) { return query(a, b, 0, 0, n); }
  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return MAX;
    if (a <= l && r <= b)
      return dat[k];
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};

void Main() {
  ll n;
  R n;
  ll a[n];
  RMQ t;
  t.init(n + 1);
  rep(i, n) {
    R a[i];
    t.update(i + 1, a[i]);
  }
  ll ans = 0;
  rep(i, n) {
    ll l = 0, r = i + 1;
    while (l + 1 < r) {
      int m = (l + r) / 2;
      if (t.query(m, i + 2) < a[i])
        l = m;
      else
        r = m;
    }
    ll x = i - l + 1;
    l = i, r = n;
    while (l + 1 < r) {
      int m = (l + r) / 2;
      if (t.query(i + 1, m + 2) < a[i])
        r = m;
      else
        l = m;
    }
    ll y = l - i + 1;
    ans += x * y * a[i];
  }
  pr(ans);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define iter(c) __typeof((c).begin())
#define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++)
#define mem(a) memset(a, 0, sizeof(a))
#define pd(a) printf("%.10f\n", a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin >>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout << '\n'
template <C T> void pr(T a) {
  cout << a;
  ln;
}
template <C T, C T2> void pr(T a, T2 b) {
  cout << a << ' ' << b;
  ln;
}
template <C T, C T2, C T3> void pr(T a, T2 b, T3 c) {
  cout << a << ' ' << b << ' ' << c;
  ln;
}
template <C T> void PR(T a, int n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  ln;
}
bool check(int n, int m, int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < m;
}
const ll MAX = 1000000007, MAXL = 1LL << 60, dx[4] = {-1, 0, 1, 0},
         dy[4] = {0, 1, 0, -1};
typedef pair<int, int> P;
class RMQ {
public:
  int n, dat[555555];
  void init(int _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    fill(dat, dat + 2 * n - 1, MAX);
  }
  void update(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  int query(int a, int b) { return query(a, b, 0, 0, n); }
  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return MAX;
    if (a <= l && r <= b)
      return dat[k];
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};

void Main() {
  ll n;
  R n;
  ll a[n];
  RMQ t;
  t.init(n + 1);
  rep(i, n) {
    R a[i];
    t.update(i + 1, a[i]);
  }
  ll ans = 0;
  rep(i, n) {
    ll l = 0, r = i + 1;
    while (l + 1 < r) {
      int m = (l + r) / 2;
      if (t.query(m, i + 2) < a[i])
        l = m;
      else
        r = m;
    }
    ll x = i - l + 1;
    l = i, r = n;
    while (l + 1 < r) {
      int m = (l + r) / 2;
      if (t.query(i + 1, m + 2) < a[i])
        r = m;
      else
        l = m;
    }
    ll y = l - i + 1;
    ans += x * y * a[i];
  }
  pr(ans);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
  return 0;
}