Language: C++

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <complex>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL-1
double EPS=1e-9;
using namespace std;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
typedef complex<double> P;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
int n;
vector<ll> a;
const int BT = 1 << 18 ;
struct segtree {//BT = size
    vector<int> seg;

	void init() {
	seg.resize(BT * 2);
	rep(i, BT * 2 - 1) seg[i] = INT_MAX;
    }

    void add(int pos, int x) {//posにxを追加,0-index nodeも0-index
		pos += BT - 1;
		seg[pos] = x;
		
		while (pos > 0) {
			pos = (pos - 1) / 2;
			seg[pos] = min(seg[pos * 2 + 1], seg[pos * 2 + 2]);
		}
    }

    int minQ(int a, int b, int k = 0, int l = 0, int r = BT) {//[a,b)に対するクエリ
		if (b <= l || r <= a) return INT_MAX;//交差しない
		if (a <= l && r <= b) return seg[k];//被覆する
		return min(minQ(a, b, k * 2 + 1, l, (l + r) / 2), minQ(a, b, k * 2 + 2, (l + r) / 2, r));
	}

}segm; 
int main(){
	cin>>n;
	int x;
	segm.init();
	for(int i=0;i<n;i++){
		cin>>x;
		a.pb((ll)x);
		segm.add(i,x);
	}
	ll ans=0;
	//cout<<segm.minQ(0,1)<<endl;
	for(int i=0;i<n;i++){
		int l=0,r=n,p=i,q=i;
		int cnt=25;
		while(cnt--){
			int rmid=(q+r)/2,lmid=(l+p)/2;
			if(segm.minQ(i,rmid+1)<a[i]){
				r=rmid;
			}else{
				q=rmid;
			}
			
			if(segm.minQ(lmid,i+1)<a[i]){
				l=lmid;
			}else{
				p=lmid;
			}
		}
		//cout<<p<<" "<<r<<endl;
		ll R,L;
		R=(ll)(r-i);
		L=(ll)(i-p+1);
		ans+=R*L*a[i];
	}
	cout<<ans<<endl;
	
}
/*3 5 0 3
0 1 1
1 2 1
2 3 2
0 2 3
1 3 2*/


Compiler output:
a.cc: In member function 'void segtree::init()':
a.cc:48:37: error: 'INT_MAX' was not declared in this scope
   48 |         rep(i, BT * 2 - 1) seg[i] = INT_MAX;
      |                                     ^~~~~~~
a.cc:19:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   18 | #include <complex>
  +++ |+#include <climits>
   19 | #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
a.cc: In member function 'int segtree::minQ(int, int, int, int, int)':
a.cc:62:46: error: 'INT_MAX' was not declared in this scope
   62 |                 if (b <= l || r <= a) return INT_MAX;//交差しない
      |                                              ^~~~~~~
a.cc:62:46: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'