Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef list<int> liint;
typedef pair<int, int> pint;
const int INF = int(2e9);
const ll LINF = ll(2e20);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }

bool comp(int a, int b) { return a > b; }

int main() {
  int N;
  cin >> N;
  vint A(N);
  unordered_map<int, int> pos_map;
  rep(i, N) {
    cin >> A[i];
    pos_map[A[i]] = i;
  }
  set<int> pos;
  pos.insert(-1);
  pos.insert(N);
  ll count = 0;
  REP(i, 1, N + 1) {
    auto itR = lower_bound(pos.begin(), pos.end(), pos_map[i]);
    auto itL = prev(itR);
    pos.insert(itR, pos_map[i]);
    ll R = *itR - pos_map[i];
    ll L = pos_map[i] - *itL;
    count += (R * L) * i;
  }
  cout << count << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef list<int> liint;
typedef pair<int, int> pint;
const int INF = int(2e9);
const ll LINF = ll(2e20);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }

bool comp(int a, int b) { return a > b; }

int main() {
  int N;
  cin >> N;
  vint A(N);
  unordered_map<int, int> pos_map;
  rep(i, N) {
    cin >> A[i];
    pos_map[A[i]] = i;
  }
  set<int> pos;
  pos.insert(-1);
  pos.insert(N);
  ll count = 0;
  REP(i, 1, N + 1) {
    auto itR = pos.lower_bound(pos_map[i]);
    auto itL = prev(itR);
    pos.insert(itR, pos_map[i]);
    ll R = *itR - pos_map[i];
    ll L = pos_map[i] - *itL;
    count += (R * L) * i;
  }
  cout << count << endl;
  return 0;
}