Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)

#define ln '\n'
#define pb push_back
#define eb emplace_back
#define prique priority_queue
#define umap unordered_map
#define BIG 2000000000
#define VERYBIG 1000000000000000ll
#define PI 3.1415926
#define coutdb cout << fixed << setprecision(15)

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
const long long MOD = 1e9 + 7;

// typedef int_fast64_t ll;
#define int int_fast64_t
using Graph = vector<vector<int>>;
typedef pair<int, int> P;

template <typename T> inline T GCD(T a, T b) {
  T c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
template <typename T> inline T LCM(T a, T b) {
  T c = GCD(a, b);
  a /= c;
  return a * b;
}
template <typename T> inline T nCr(T a, T b) {
  T i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}
template <typename T> inline T nHr(T a, T b) { return nCr(a + b - 1, b); }

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> P(N), idx(N + 1);
  REP(i, N) {
    cin >> P[i];
    idx[P[i]] = i;
  }

  multiset<int> S;
  S.insert(-1);
  S.insert(-1);
  S.insert(N);
  S.insert(N);
  int ans = 0, index = idx[N];
  for (int i = N; i > 0; i--) {
    auto itr = lower_bound(ALL(S), index);
    int tmin = *itr++;
    int tsec = *itr--;
    itr--;
    int smax = *itr--;
    int ssec = *itr;
    ans += i * (smax - ssec) * (tmin - index);
    ans += i * (index - smax) * (tsec - tmin);
    S.insert(index);
    if (i == 1)
      break;
    index = idx[i - 1];
  }
  cout << ans << ln;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)

#define ln '\n'
#define pb push_back
#define eb emplace_back
#define prique priority_queue
#define umap unordered_map
#define BIG 2000000000
#define VERYBIG 1000000000000000ll
#define PI 3.1415926
#define coutdb cout << fixed << setprecision(15)

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
const long long MOD = 1e9 + 7;

// typedef int_fast64_t ll;
#define int int_fast64_t
using Graph = vector<vector<int>>;
typedef pair<int, int> P;

template <typename T> inline T GCD(T a, T b) {
  T c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
template <typename T> inline T LCM(T a, T b) {
  T c = GCD(a, b);
  a /= c;
  return a * b;
}
template <typename T> inline T nCr(T a, T b) {
  T i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}
template <typename T> inline T nHr(T a, T b) { return nCr(a + b - 1, b); }

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> P(N), idx(N + 1);
  REP(i, N) {
    cin >> P[i];
    idx[P[i]] = i;
  }

  multiset<int> S;
  S.insert(-1);
  S.insert(-1);
  S.insert(N);
  S.insert(N);
  int ans = 0, index = idx[N];
  for (int i = N; i > 0; i--) {
    auto itr = S.lower_bound(index);
    int tmin = *itr++;
    int tsec = *itr--;
    itr--;
    int smax = *itr--;
    int ssec = *itr;
    ans += i * (smax - ssec) * (tmin - index);
    ans += i * (index - smax) * (tsec - tmin);
    S.insert(index);
    if (i == 1)
      break;
    index = idx[i - 1];
  }
  cout << ans << ln;
}