描述

Given a sequence, we define the seqence's value equals the difference between the largest element and the smallest element in the sequence. As an example, the value of sequence (3, 1, 7, 2) = 7-1 = 6.

Now, given a sequence S, output the sum of all value of consecutive subsequences.

输入

The first line has an integer N (2 ≤ N ≤ 300000) indicating the number of elements of the sequence. Then follows N lines, each line has a positive integer no larger than 108 indicating an element of the sequence.

输出

Output the requested sum.

样例输入

4

3

1

7

2

样例输出

31

提示

The consecutive subsequence of the sequence (3, 1, 7, 2) has:
(3, 1), (1, 7), (7, 2), (3, 1, 7), (1, 7, 2), (3, 1, 7, 2)

The sum of all values equals 2+6+5+6+6+6=31

题意

求所有区间极差和

题解

N很大,考虑单调栈维护max和min,那么就是求ans=Σmax-Σmin

sum1代表最大值前缀和

sum2代表最小值前缀和

max单调栈,d[i]代表下标,b[i]代表值,tail1代表栈顶

min单调栈,e[i]代表下标,c[i]代表值,tail2代表栈顶

考虑加入一个a的贡献,sum1+=(d[tail1]-d[tail1-1])*a-弹出的Σ(d[tail1]-d[tail1-1])*b[tail1],sum2+=(e[tail2]-e[tail2-1])*a-弹出的Σ(e[tail2]-e[tail2-1])*c[tail2],就是计算后的sum1-sum2

代码

#include<stdio.h>
#define ll __int64
const int N=3e5+;
int n,a[N],b[N],c[N],d[N],e[N];
ll ans,sum1,tail1,sum2,tail2;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
while(<=tail1&&b[tail1]<=a[i])
{
sum1-=(d[tail1]-d[tail1-])*1LL*b[tail1];
tail1--;
}
while(<=tail2&&c[tail2]>=a[i])
{
sum2-=(e[tail2]-e[tail2-])*1LL*c[tail2];
tail2--;
}
b[++tail1]=a[i];d[tail1]=i;
c[++tail2]=a[i];e[tail2]=i;
sum1+=(d[tail1]-d[tail1-])*1LL*a[i];
sum2+=(e[tail2]-e[tail2-])*1LL*a[i];
ans+=sum1-sum2;
}
printf("%I64d\n",ans);
return ;
}

TZOJ 4244 Sum(单调栈区间极差)的更多相关文章

  1. poj 2796 Feel Good 单调栈区间问题

    Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...

  2. TOJ 4244: Sum

    4244: Sum   Time Limit(Common/Java):3000MS/9000MS     Memory Limit:65536KByteTotal Submit: 63       ...

  3. 【BZOJ4262】Sum 单调栈+线段树

    [BZOJ4262]Sum Description Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2. ...

  4. [Agc005D/At2060] Minimum Sum - 单调栈

    鉴于早上那题让我怀疑单调栈白学,特意来复习下单调栈 题意 考虑按照每个元素对答案的贡献来统计,那么我们只需要找到每个元素左边右边第一个比它小的就可 这题给的又是排列,简直不能再良心 #include ...

  5. C语言中的栈和堆

    原文出处<http://blog.csdn.net/xiayufeng520/article/details/45956305#t0> 栈内存由编译器分配和释放,堆内存由程序分配和释放. ...

  6. 2015暑假多校联合---Assignment(优先队列)

    原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...

  7. [TJOI 2017]异或和

    Description 在加里敦中学的小明最近爱上了数学竞赛,很多数学竞赛的题都是与序列的连续和相关的.所以对于一个序列,求出它们所有的连续和来说,小明觉得十分的简单.但今天小明遇到了一个序列和的难题 ...

  8. java crach 日志解析

    在java开发中,或许会出现如下错误,这种错误大多出现在开发中涉及本地代码的地方. ## A fatal error has been detected by the Java Runtime Env ...

  9. 【splunk】一些查询例子

    最重要资料: 入门基础:http://docs.splunk.com/Documentation/Splunk/6.5.2/SearchTutorial/WelcometotheSearchTutor ...

随机推荐

  1. 《DSP using MATLAB》Problem 7.3

  2. 使用deb 打包开发的postgres extension 另外一种方法

    已经写过一个deb 包打包的方法,我们同时也可以使用dpkg-deb 命令 安装依赖工具包 推荐安装全点的 sudo apt-get install build-essential autoconf ...

  3. day45 jQuery

    在用js写代码时,会遇到一些问题: window.onload 事件有事件覆盖的问题,因此只能写一个事件. 代码容错性差. 浏览器兼容性问题. 书写很繁琐,代码量多. 代码很乱,各个页面到处都是. 动 ...

  4. Java day1

    1. 学习java,首先是jdk的安装,JDK是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+ ...

  5. ubuntu server资料

    2.改变键盘布局 sudo dpkg-reconfigure keyboard-configuration 或sudo vim /etc/default/keyboard,修改XKBLAYOUT变量的 ...

  6. G2 绘制混合图例 demo

    G2 绘制混合图例 demo import G2 from '@antv/g2'; import DataSet from '@antv/data-set'; // G2 对数据源格式的要求,仅仅是 ...

  7. Ali流量控制中间件Sentinel

    原文链接: https://blog.csdn.net/u012190514/article/details/81383698 Sentinel 是什么 随着微服务的流行,服务和服务之间的稳定性变得越 ...

  8. HTTP请求中 request payload 和 formData 区别?

    原文地址: http://www.cnblogs.com/tugenhua0707/p/8975615.html FormData和Payload是浏览器传输给接口的两种格式,这两种方式浏览器是通过C ...

  9. 自然语言处理NLP-云端API汇总

    Google Google Cloud:https://cloud.google.com/natural-language/ ParallelDots ParallelDots, Inc. 无需训练, ...

  10. 红外NEC协议

    注意: 用示波器在接收头抓的电平看起来和NEC协议刚好相反, 那是因为:HS0038B 这个红外一体化接收头,当收到有载波的信号的时候,会输出一个低电平,空闲的时候会输出高电平. 具体情况,具体分析. ...