You are given an array a consisting of n elements. The imbalance value of some subsegment of this array is the difference between the maximum and minimum element from this segment. The imbalance value of the array is the sum of imbalance valuesof all subsegments of this array.

For example, the imbalance value of array [1, 4, 1] is 9, because there are 6different subsegments of this array:

  • [1] (from index 1 to index 1), imbalance value is 0;
  • [1, 4] (from index 1 to index 2), imbalance value is 3;
  • [1, 4, 1] (from index 1 to index 3), imbalance value is 3;
  • [4] (from index 2 to index 2), imbalance value is 0;
  • [4, 1] (from index 2 to index 3), imbalance value is 3;
  • [1] (from index 3 to index 3), imbalance value is 0;

You have to determine the imbalance value of the array a.

Input

The first line contains one integer n (1 ≤ n ≤ 106) — size of the array a.

The second line contains n integers a1, a2... an (1 ≤ ai ≤ 106) — elements of the array.

Output

Print one integer — the imbalance value of a.

Example

Input
3
1 4 1
Output
9

题意:
给定一个含有N个数的数组,求这个数组的所有连续区间的不平衡值的总和。
一个区间的不平衡值为这个区间中的最大值减去最小值。
思路:
我们可以这样思考,这个题目的答案可以这样得来,
对于每一个a[i],它对答案的贡献值为以a[i]为区间最大值的区间数量*a[i]-a[i]*以a[i]为区间最小值的区间数量。
ans=sum{ contribution(a[i])} 那么我们的难题为如何计算以a[i]为区间最大/最小值的区间数量。
这种模型我们很容易想到利用单调栈来O(N)求出。
我们定义两个数组,l[n+5],r[n+5],l[i]和r[i] 分别维护的是从a[i]元素开始向左和向右第一个比a[i]小的元素的位置。(注意边界)
然后我们可以通过l[i]和r[i]来求出区间数量,然后我们再利用单调栈求出从a[i]元素开始向左和向右第一个比a[i]大的元素的位置
然后我们愉快的计算每一个的贡献然后加起来就是我们要求的答案。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
int l[maxn];
int r[maxn];
ll ans=0ll;
int main()
{
gg(n);
repd(i,,n)
{
gg(a[i]);
}
stack<int> s;
repd(i,,n)
{
while(s.size()&&a[s.top()]>a[i])
{
s.pop();
}
if(s.size())
{
l[i]=s.top();
}else
{
l[i]=;
}
s.push(i);
}
while(s.size())
{
s.pop();
}
for(int i=n;i>=;i--)
{
while(s.size()&&a[s.top()]>=a[i])
{
s.pop();
}
if(s.size())
{
r[i]=s.top();
}else
{
r[i]=n+;
}
s.push(i);
}
repd(i,,n)
{
ans=ans-1ll*a[i]*(i-l[i])*(r[i]-i);
}
// db(ans);
while(s.size())
{
s.pop();
}
repd(i,,n)
{
while(s.size()&&a[s.top()]<a[i])
{
s.pop();
}
if(s.size())
{
l[i]=s.top();
}else
{
l[i]=;
}
s.push(i);
}
while(s.size())
{
s.pop();
}
for(int i=n;i>=;i--)
{
while(s.size()&&a[s.top()]<=a[i])
{
s.pop();
}
if(s.size())
{
r[i]=s.top();
}else
{
r[i]=n+;
}
s.push(i);
} repd(i,,n)
{
ans=ans+1ll*a[i]*(i-l[i])*(r[i]-i);
}
printf("%lld\n",ans );
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}


最新文章

  1. 推荐一个iOS关于颜色的库-Wonderful
  2. JavaScript的事件绑定及深入
  3. Android---WebView显示Html乱码问题
  4. JavaSE思维导图(六)
  5. js获取浏览器地址栏传递的参数
  6. volley请求原理
  7. 利用unittest+ddt进行接口测试(一):简单demo
  8. 基于 Webhooks gitlab 自动化构建
  9. centos7 安装部署zabbix
  10. 贝叶斯公式52张牌猜黑桃A策略
  11. 基于saltstack自动化部署高可用kubernetes集群
  12. [转帖]SAP一句话入门:Material Management
  13. 转)VCSA 6.5重启无法访问,报错“503 Service Unavailable”的解决方法
  14. Call actvity after viewpager is finished
  15. #pragma pack(push) 和#pragma pack(pop) 以及#pragma pack()
  16. dp练习(4)——过河卒
  17. 【问题】解决在微信公众号里面网站无法访问:oops something went wrong:(
  18. JAVA字符编码测试
  19. Python3 urllib 爬取 花瓣网图片
  20. Whether to hide the cookie from JavaScript

热门文章

  1. 手动搭建Docker本地私有镜像仓库
  2. react &amp; vue 项目创建的方式
  3. &quot;敏捷革命&quot;读书笔记
  4. WIndows 使用VS编译 Lua5
  5. nodejs websocket
  6. 获取Spring容器Bean对象工具类
  7. python脚本后台启动
  8. SQLite的原子提交--单文件场景
  9. 0、原生jdbc工厂类
  10. [Micropython]TPYBoard v202 利用单片机快速实现家庭智能控制平台