Language: C++

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<ll, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n;
ll a[200010];

// Range Mininum Query
template<typename T>
class SegmentTree {
private:
    int n;
    T dat[2000100];
public:
    SegmentTree(int n_) {
	n = 1;
	while (n < n_) n *= 2;
	for (int i = 0; i < 2*n-1; i++) dat[i] = INT_MAX;
    }

    void update(int k, T 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]);
	}
    }
    
    // call query(a, b)
    // return min value for [a, b)
    T query(int a, int b) { return query(a, b, 0, 0, n); }
    T query(int a, int b, int k, int l, int r) {
	if (r <= a || b <= l) return INT_MAX;

	if (a <= l && r <= b) return dat[k];
	else {
	    T vl = query(a, b, k*2+1, l, (l+r)/2);
	    T vr = query(a, b, k*2+2, (l+r)/2, r);
	    return min(vl, vr);
	}
    }
};

int idx[200010];
SegmentTree<ll> st(200010);

ll calc(int l, int r) {
    if (l >= r) return 0;
    ll v = st.query(l, r);
    int i = idx[v];
    return (v+1) * (i - l + 1) * (r - i) + calc(l, i) + calc(i+1, r);
}

ll solve() {
    for (int i = 0; i < n; i++) {
        idx[a[i]] = i;
        st.update(i, a[i]);
    }
    return calc(0, n);
}

void input() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}


Compiler output:
a.cc: In constructor 'SegmentTree<T>::SegmentTree(int)':
a.cc:59:50: error: 'INT_MAX' was not declared in this scope
   59 |         for (int i = 0; i < 2*n-1; i++) dat[i] = INT_MAX;
      |                                                  ^~~~~~~
a.cc:21:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   20 | #include <random>
  +++ |+#include <climits>
   21 | using namespace std;
a.cc: In member function 'T SegmentTree<T>::query(int, int, int, int, int)':
a.cc:75:38: error: 'INT_MAX' was not declared in this scope
   75 |         if (r <= a || b <= l) return INT_MAX;
      |                                      ^~~~~~~
a.cc:75:38: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'