Language: C++

#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

struct para
{
    int m, M;
};

vector<para> T;
int l, size;

void Stworz(int n)
{
    int p = 1;
    while (p < n)
        p *= 2;
    l = p;
    size = 2 * p - 1;
    for (int i = 0; i <= size; ++i)
        T.push_back({1e9, -1e9});
}

void Dodaj(int a, int x)
{
    a += l;
    T[a] = {x, x};
    a /= 2;
    while (a != 0)
    {
        T[a].m = min(T[2 * a].m, T[2 * a + 1].m);
        T[a].M = max(T[2 * a].M, T[2 * a + 1].M);
        a /= 2;
    }
}

int Min(int a, int b)
{
    if (a > b)
        return 1e9;
    a += l;
    b += l;
    int odp = min(T[a].m, T[b].m);
    while (a / 2 != b / 2)
    {
        if (a % 2 == 0)
            odp = min(odp, T[a + 1].m);
        if (b % 2 == 1)
            odp = min(odp, T[b - 1].m);
        a /= 2;
        b /= 2;
    }
    return odp;
}

int Max(int a, int b)
{
    if (a > b)
        return -1e9;
    a += l;
    b += l;
    int odp = max(T[a].M, T[b].M);
    while (a / 2 != b / 2)
    {
        if (a % 2 == 0)
            odp = max(odp, T[a + 1].M);
        if (b % 2 == 1)
            odp = max(odp, T[b - 1].M);
        a /= 2;
        b /= 2;
    }
    return odp;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
        --a[i];
    }
    Stworz(n);
    vector<int> L(n);
    for (int i = 0; i < n; ++i)
    {
        L[i] = Max(0, a[i] - 1);
        if (L[i] == -1e9)
            L[i] = 0;
        else
            ++L[i];
        Dodaj(a[i], i);
    }
    T.clear();
    Stworz(n);
    vector<int> P(n);
    for (int i = n - 1; i >= 0; --i)
    {
        P[i] = Min(0, a[i] - 1);
        if (P[i] == 1e9)
            P[i] = n - 1;
        else
            --P[i];
        Dodaj(a[i], i);
    }
    ll odp = 0;
    for (int i = 0; i < n; ++i)
    {
        ll l = i - L[i] + 1;
        ll p = P[i] - i + 1;
        odp += l * p * (a[i] + 1);
    }
    cout << odp;
    return 0;
}


Compiler output:
a.cc: In function 'void Stworz(int)':
a.cc:22:5: error: reference to 'size' is ambiguous
   22 |     size = 2 * p - 1;
      |     ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:14:8: note:                 'int size'
   14 | int l, size;
      |        ^~~~
a.cc:23:26: error: reference to 'size' is ambiguous
   23 |     for (int i = 0; i <= size; ++i)
      |                          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:14:8: note:                 'int size'
   14 | int l, size;
      |        ^~~~
a.cc:24:22: error: narrowing conversion of '1.0e+9' from 'double' to 'int' [-Wnarrowing]
   24 |         T.push_back({1e9, -1e9});
      |                      ^~~