Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9 : 1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
  int b = 0;
  for (auto itr : a) {
    if (b++ != 0)
      cout << " ";
    cout << itr;
  }
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
#define int ll
signed main() {
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> indexOf(n);
  set<int> S;
  rep(i, n) cin >> v[i];
  rep(i, n) indexOf[--v[i]] = i;
  int ans = 0;
  S.insert(-1);
  S.insert(n);
  rep(i, n) {
    auto itr1 = lower_bound(all(S), indexOf[i]);
    int r_val = *itr1--;
    int l_val = *itr1;
    int l, r;
    r = r_val - indexOf[i];
    l = indexOf[i] - l_val;
    S.insert(indexOf[i]);
    ans += r * l * (i + 1);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9 : 1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
  int b = 0;
  for (auto itr : a) {
    if (b++ != 0)
      cout << " ";
    cout << itr;
  }
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
#define int ll
signed main() {
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> indexOf(n);
  set<int> S;
  rep(i, n) cin >> v[i];
  rep(i, n) indexOf[--v[i]] = i;
  int ans = 0;
  S.insert(-1);
  S.insert(n);
  rep(i, n) {
    auto itr1 = S.lower_bound(indexOf[i]);
    auto itr2 = prev(itr1);
    int r_val = *itr1;
    int l_val = *itr2;
    int l, r;
    r = r_val - indexOf[i];
    l = indexOf[i] - l_val;
    S.insert(indexOf[i]);
    ans += r * l * (i + 1);
  }
  cout << ans << endl;
}