D. Imbalanced Array
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 values of all subsegments of this array.

For example, the imbalance value of array [1, 4, 1] is 9, because there are 6 different 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个数,一个区间的贡献是区间最大值-区间最小值,求所有区间贡献和。

思路:首先想到n*n的rmq,明显超时。

   换个角度,枚举每个数作为最小值的区间个数,枚举每个数作为最大值的区间个数;

   怎么算呢,例如求最小值的区间,找出左边有x个比其大的,右边x个比其大的,区间的个数为x*y;

   这个用单调栈可以O(n)处理,注意需要半开半闭;

  

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e6+,M=1e6+,inf=,mod=1e9+;
const LL INF=1e18+,MOD=1e9+; int a[N],d[N];
int zn[N],yn[N];
int zx[N],yx[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
a[]=;
int k=;d[++k]=;
for(int i=;i<=n;i++)
{
while(a[d[k]]>=a[i])k--;
zn[i]=d[k];
d[++k]=i;
}
a[n+]=;
k=;d[++k]=n+;
for(int i=n;i>=;i--)
{
while(a[d[k]]>a[i])k--;
yn[i]=d[k];
d[++k]=i;
}
a[]=1e6+;
k=;d[++k]=;
for(int i=;i<=n;i++)
{
while(a[d[k]]<=a[i])k--;
zx[i]=d[k];
d[++k]=i;
}
a[n+]=1e6+;
k=;d[++k]=n+;
for(int i=n;i>=;i--)
{
while(a[d[k]]<a[i])k--;
yx[i]=d[k];
d[++k]=i;
}
LL ans=;
for(int i=;i<=n;i++)
{
ans+=1LL*(yx[i]-i)*(i-zx[i])*a[i];
ans-=1LL*(yn[i]-i)*(i-zn[i])*a[i];
}
cout<<ans<<endl;
return ;
}

Educational Codeforces Round 23 D. Imbalanced Array 单调栈的更多相关文章

  1. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  2. Educational Codeforces Round 23 A-F 补题

    A Treasure Hunt 注意负数和0的特殊处理.. 水题.. 然而又被Hack了 吗的智障 #include<bits/stdc++.h> using namespace std; ...

  3. Educational Codeforces Round 23 补题小结

    昨晚听说有教做人场,去补了下玩. 大概我的水平能做个5/6的样子? (不会二进制Trie啊,我真菜) A. 傻逼题.大概可以看成向量加法,判断下就好了. #include<iostream> ...

  4. Educational Codeforces Round 11 A. Co-prime Array 水题

    A. Co-prime Array 题目连接: http://www.codeforces.com/contest/660/problem/A Description You are given an ...

  5. Educational Codeforces Round 23 B. Makes And The Product

    B. Makes And The Product time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. Educational Codeforces Round 23 E. Choosing The Commander trie数

    E. Choosing The Commander time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. Educational Codeforces Round 63 D. Beautiful Array

    D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Educational Codeforces Round 11——A. Co-prime Array(map+vector)

    A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. Educational Codeforces Round 23.C

    C. Really Big Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. Matlab基础部分1

  2. centos 安装MySQL全过程

    1.到chinaunix下载mysql 下载地址: http://download.chinaunix.net/download/0008000/7159.shtml 2.上传到CentOS服务器 本 ...

  3. SQL数据库增量备份还原方式

    SQLSERVER2008的备份还原最基本的方式自然是完整备份,然后完整还原即可. 但是如果遇到数据库文件很大,数据量很大,备份和还原需要花费不少时间的时候, 数据库的差异备份自然就成为考虑的备份方案 ...

  4. 自写Jquery插件 Tab

    原创文章,转载请注明出处,谢谢!https://www.cnblogs.com/GaoAnLee/p/9067017.html 每每看到别人写的Jquery插件,自己也试着学习尝试,终有结果,废话不多 ...

  5. Jquery部分小结

    window.onload 必须等待网页中所有的内容加载完毕后(包括图片)才能执行,如果多个,只会执行最后一个;$(document).ready() 网页中所有DOM结构绘制完毕后就执行,可能DOM ...

  6. MediaCodec在Android视频硬解码组件的应用

    https://yq.aliyun.com/articles/632892 云栖社区> 博客列表> 正文 MediaCodec在Android视频硬解码组件的应用   cheenc 201 ...

  7. Nginx和PHP上传文件大小的限制

    Nginx client_max_body_size 80m; PHP php.ini file_uploads = on ; 是否允许通过HTTP上传文件的开关.默认为ON即是开 upload_tm ...

  8. 3、pandas的loc和iloc数据筛选

    选择列: 选择一列: 选择多列(选择的内容变成list,也就是要两个方括号): 选择一行或多行(loc函数): 选择连续的行(以索引标签为选择参数): 选择非连续的行(以索引标签为选择参数): 选择包 ...

  9. ssh-keygen 不是内部或外部命令

    如何处理呢? 1.找到Git/usr/bin目录下的ssh-keygen.exe(如果找不到,可以在计算机全局搜索) 2.属性-->高级系统设置-->环境变量-->系统变量,找到Pa ...

  10. vue 起步

    vue 官网 目前最火的前端框架当属Vue.js了,很多使用过vue的程序员这样评价它,“vue.js兼具angular.js和react.js的优点,并剔除了它们的缺点”.授予了这么高的评价的vue ...