提交时间:2025-02-07 15:10:40
运行 ID: 36070
#include<bits/stdc++.h> #include<bits/extc++.h> #define int short using namespace std; using namespace __gnu_pbds; unordered_map<int,__int32_t>ht; int n,p[10005],a[10005],s[10005]; void read(int &x){ x=0; char c=getchar_unlocked(); while(!isdigit(c))c=getchar_unlocked(); while(isdigit(c))x=x*10+(c^48),c=getchar_unlocked(); return; } signed main(){ read(n); for(int i=1;i<=n;i++)read(p[i]); long long ans=0; for(int i=1;i<=n;i++){ ht.clear(); // ht[0]++; for(int j=1;j<=n;j++){ if(p[j]>p[i])a[j]=1; else if(p[j]<p[i])a[j]=-1; else a[j]=0; s[j]=s[j-1]+a[j]; } // for(int j=1;j<=n;j++)cout<<a[j]<<" "; // cout<<endl; // for(int j=1;j<=n;j++)cout<<s[j]<<" "; // cout<<endl; for(int j=0;j<i;j++)ht[s[j]]+=j+1; for(int j=i;j<=n;j++){ // cout<<j<<" "<<p[i]<<" "<<s[j]<<" "<<ht[-s[j]]<<endl; ans+=1ll*j*p[i]*ht[s[j]]; } // cout<<ans<<endl; } cout<<ans<<endl; return 0; }