描述

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.9

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  2. PythonStudy——变量 Variable

    变量 变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念.变量可以通过变量名访问.在指令式语言中,变量通常是可变的:但在纯函数式语言(如Haskell)中,变量可能是不可变(immutab ...

  3. 用C自撸apache简易模块,搭建图片处理服务器。

    写C是个撸sir /* ** mod_acthumb.c -- Apache sample acthumb module ** [Autogenerated via ``apxs -n acthumb ...

  4. dependency walker检查dll依赖关系目录设置的问题

    废话少说,直接上图 图中来看,似乎IESHIMS.DLL文件不存在报错,实际是因为没有加载IESHIMS.DLL所在的路径. 在我的电脑里面搜索有两个同名的dll,一个是32位的,一个是64位的. C ...

  5. python3学习笔记七(字典)

    参照http://www.runoob.com/python3/python3-dictionary.html 字典 字典是另一种可变容器模型,且可以存储任意类型对象. dict1 = {key1:v ...

  6. FreeBsd网络性能优化方案sysctl

    以下是阿盛的配置 sysctl net.inet.tcp.msl= sysctl net.inet.tcp.mssdflt= sysctl net.inet.tcp.minmss= sysctl ne ...

  7. windows异常事件对应的ID

    转载地址: Windows 2008 R2查看异常关机或开机事件ID https://blog.csdn.net/hejun1218/article/details/81059327

  8. jmeter分布式、linux运行

    一.jmeter分布式压测(多台电脑一起压测) 1.有多台电脑,每台电脑上都有jmeter,而且这几台电脑都互相能ping通 2.在我的电脑的jmeter,bin目录下,修改jmeter.proper ...

  9. SQL查数据库有哪些触发器,存储过程...

    select name from sysobjects where xtype='TR' --所有触发器select name from sysobjects where xtype='P' --所有 ...

  10. Struts2 xxAction-validation.xml使用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC &quo ...