Language: C++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
 
#define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
 
#define LOWBIT(x) ((x)&(-x))
 
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c 
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
 
#define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a))

#define MP make_pair
#define PB push_back
#define ft first
#define sd second
 
template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
    in >> p.first >> p.second;
    return in;
}
 
template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
    for (auto &x: v)
        in >> x;
    return in;
}

template<typename T>
ostream &operator<<(ostream &out, vector<T> &v) {
    Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
    return out;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
    out << "[" << p.first << ", " << p.second << "]" << "\n";
    return out;
}

inline int gc(){
    static const int BUF = 1e7;
    static char buf[BUF], *bg = buf + BUF, *ed = bg;
    
    if(bg == ed) fread(bg = buf, 1, BUF, stdin);
    return *bg++;
} 

inline int ri(){
    int x = 0, f = 1, c = gc();
    for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
    for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
    return x*f;
}

template<class T>
inline string toString(T x) {
    ostringstream sout;
    sout << x;
    return sout.str();
}

inline int toInt(string s) {
    int v;
    istringstream sin(s);
    sin >> v;
    return v;
}

//min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
    return min <= aim && aim <= max;
}
 
typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef set< PII > SPII;
typedef vector< int > VI;
typedef vector< double > VD;
typedef vector< VI > VVI;
typedef vector< SI > VSI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, string > MIS;
typedef map< int, PII > MIPII;
typedef map< PII, int > MPIII;
typedef map< string, int > MSI;
typedef map< string, string > MSS;
typedef map< PII, string > MPIIS;
typedef map< PII, PII > MPIIPII;
typedef multimap< int, int > MMII;
typedef multimap< string, int > MMSI;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-8;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + 7;
const int maxN = 1e5 + 7;
const LL ONE = 1;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555;

int N;
int P[maxN];     // P数组为1~N的一组排列 
int pos[maxN];     // pos[i]为i在P数组中的位置 
int leftFirstMaxPos[maxN];     // leftFirstMaxPos[i]为i位置的数左边第一个比i大的数的位置,没有则为0
int rightFirstMaxPos[maxN]; // rightFirstMaxPos[i]为i位置的数右边第一个比i大的数的位置,没有则为N+1 
LL ans = 0; 

int main(){
    //freopen("MyOutput.txt","w",stdout);
    //freopen("input.txt","r",stdin);
    //INIT();
    scanf("%d", &N);
    For(i, 1, N) {
        scanf("%d", &P[i]);
        pos[P[i]] = i; 
        // 后面是从1开始枚举,而1是最小的,所以1的左右最近大正好是相邻的两个数 
        leftFirstMaxPos[i] = i - 1;
        rightFirstMaxPos[i] = i + 1;
    }
    
    // 计算每个数i为第二大的时候对答案的贡献 
    For(i, 1, N) {
        int Lpos1 = leftFirstMaxPos[pos[i]];     // i左边第一个比他大的数的位置 
        int Lpos2 = leftFirstMaxPos[Lpos1];        // i左边第二个比他大的数的位置 
        int Rpos1 = rightFirstMaxPos[pos[i]];    // i右边第一个比他大的数的位置 
        int Rpos2 = rightFirstMaxPos[Rpos1];        // i右边第二个比他大的数的位置 
        
        if(Lpos1 != 0) ans += ONE * i * (Lpos1 - Lpos2) * (Rpos1 - pos[i]);
        if(Rpos1 != N + 1) ans += ONE * i * (pos[i] - Lpos1) * (Rpos2 - Rpos1);
        
        // 更新 
        /*
            例如这个片段:
                  1 2 3 4 5 6 7 
                P 6 3 1 2 5 4 7
                此时以i = 1为例
                本来leftFirstMaxPos[4] = 3, rightFirstMaxPos[2] = 3的
                计算完i = 1的贡献之后, leftFirstMaxPos[4] = 2, rightFirstMaxPos[2] = 4
                这一步更新相当于把1给砍掉了,剩下的数中2又是最小的数,于是2的左右最近大又正好是相邻的两个数 
                没错,非常像链表删除一个值的操作 
                leftFirstMaxPos就相当于静态链表的前驱数组
                rightFirstMaxPos就相当于静态链表的后继数组 
        */ 
        leftFirstMaxPos[Rpos1] = Lpos1;
        rightFirstMaxPos[Lpos1] = Rpos1;
    }
    
    printf("%lld", ans);
    return 0;
}

Compiler output:
a.cc:45:34: error: 'vector' has not been declared
   45 | istream &operator>>(istream &in, vector<T> &v) {
      |                                  ^~~~~~
a.cc:45:40: error: expected ',' or '...' before '<' token
   45 | istream &operator>>(istream &in, vector<T> &v) {
      |                                        ^
a.cc: In function 'std::istream& operator>>(std::istream&, int)':
a.cc:46:19: error: 'v' was not declared in this scope
   46 |     for (auto &x: v)
      |                   ^
a.cc: At global scope:
a.cc:52:35: error: 'vector' has not been declared
   52 | ostream &operator<<(ostream &out, vector<T> &v) {
      |                                   ^~~~~~
a.cc:52:41: error: expected ',' or '...' before '<' token
   52 | ostream &operator<<(ostream &out, vector<T> &v) {
      |                                         ^
a.cc: In function 'std::ostream& operator<<(std::ostream&, int)':
a.cc:53:12: error: 'v' was not declared in this scope
   53 |     Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
      |            ^
a.cc:9:39: note: in definition of macro 'Rep'
    9 | #define Rep(i,n) for (int i = 0; i < (n); ++i)
      |                                       ^
a.cc: In function 'std::string toString(T)':
a.cc:80:19: error: 'sout' has incomplete type
   80 |     ostringstream sout;
      |                   ^~~~
In file included from /usr/include/c++/14/ios:40,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/iosfwd:106:11: note: declaration of 'std::ostringstream' {aka 'class std::__cxx11::basic_ostringstream<char>'}
  106 |     class basic_ostringstream;
      |           ^~~~~~~~~~~~~~~~~~~
a.cc: In function 'int toInt(std::string)':
a.cc:87:24: error: variable 'std::istringstream sin' has initializer but incomplete type
   87 |     istringstream sin(s);
      |                        ^
a.cc: At global scope:
a.cc:105:9: error: 'set' does not name a type
  105 | typedef set< int > SI;
      |         ^~~
a.cc:106:9: error: 'set' does not name a type
  106 | typedef set< PII > SPII;
      |         ^~~
a.cc:107:9: error: 'vector' does not name a type
  107 | typedef vector< int > VI;
      |         ^~~~~~
a.cc:108:9: error: 'vector' does not name a type
  108 | typedef vector< double > VD;
      |         ^~~~~~
a.cc:109:9: error: 'vector' does not name a type
  109 | typedef vector< VI > VVI;
      |         ^~~~~~
a.cc:110:9: error: 'vector' does not name a type
  110 | typedef vector< SI > VSI;
      |         ^~~~~~
a.cc:111:9: error: 'vector' does not name a type
  111 | typedef vector< PII > VPII;
      |         ^~~~~~
a.cc:124:9: error: 'vector' does not name a type
  124 | typedef vector< LL > VL;
      |         ^~~~~~
a.cc:125:9: error: 'vector' does not name a type
  125 | typedef vector< VL > VVL;
      |         ^~~~~~
a.cc:126:9: error: 'priority_queue' does not name a type
  126 | typedef priority_queue< int > PQIMax;
      |         ^~~~~~~~~~~~~~
a.cc:127:9: error: 'priority_queue' does not name a type
  127 | typedef priority_queue< int, VI, greater< int > > PQIMin;
      |         ^~~~~~~~~~~~~~