TZOJ 4244 Sum(单调栈区间极差)
描述
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(单调栈区间极差)的更多相关文章
- poj 2796 Feel Good 单调栈区间问题
Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...
- TOJ 4244: Sum
4244: Sum Time Limit(Common/Java):3000MS/9000MS Memory Limit:65536KByteTotal Submit: 63 ...
- 【BZOJ4262】Sum 单调栈+线段树
[BZOJ4262]Sum Description Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2. ...
- [Agc005D/At2060] Minimum Sum - 单调栈
鉴于早上那题让我怀疑单调栈白学,特意来复习下单调栈 题意 考虑按照每个元素对答案的贡献来统计,那么我们只需要找到每个元素左边右边第一个比它小的就可 这题给的又是排列,简直不能再良心 #include ...
- C语言中的栈和堆
原文出处<http://blog.csdn.net/xiayufeng520/article/details/45956305#t0> 栈内存由编译器分配和释放,堆内存由程序分配和释放. ...
- 2015暑假多校联合---Assignment(优先队列)
原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...
- [TJOI 2017]异或和
Description 在加里敦中学的小明最近爱上了数学竞赛,很多数学竞赛的题都是与序列的连续和相关的.所以对于一个序列,求出它们所有的连续和来说,小明觉得十分的简单.但今天小明遇到了一个序列和的难题 ...
- java crach 日志解析
在java开发中,或许会出现如下错误,这种错误大多出现在开发中涉及本地代码的地方. ## A fatal error has been detected by the Java Runtime Env ...
- 【splunk】一些查询例子
最重要资料: 入门基础:http://docs.splunk.com/Documentation/Splunk/6.5.2/SearchTutorial/WelcometotheSearchTutor ...
随机推荐
- github/gitee使用办法
github/gitee只要添加SSH公钥都是可以连接上的 比如把某个文件上传gitee 首先肯定要有权限 否则会一直提醒failed伤心心 接下来说常用语句 git config --list ...
- PythonStudy1——Python 值拷贝 浅拷贝 深拷贝
拷贝:对值进行复制的过程 # 值拷贝:应用场景最多 ls = [1, 'abc', [10]] ls1 = ls # ls1直接将ls中存放的地址拿过来 # ls内部的值发生任何变化,ls1都会随 ...
- oracle存储结构
数据库的物理存储结构 select * from v$datafile; 数据库的逻辑存储结构,从表空间开始查起一个数据库对象的逻辑存储结构如下表空间-段-区-块 select * from dba_ ...
- 记录安装 java 环境,部署环境变量遇到的小坑
情况:先安装 jdk 7,再安装 jdk8,发现 java 的环境自动变成了 jdk8 解决: 1.在 系统的环境变量下,多出了一行: C:\Program Files (x86)\Common Fi ...
- eclipse中mybatis自动生成插件使用
对于使用Mybatis的开发者来说, 使用mybatis generator来生成mapper 以及配置文件, 可以大大简化工作, mybatis generator有多种工作方式, eclipse插 ...
- find查找文件的时间问题
很多细节方面的东西没有到真正用的时候,是觉察不出来的,因为这个时间的问题出了问题,现在好好理一下,这个find的时间很容易就搞混了,一段时间不用,也忘了,也反映出来了自己的基础知识不是很牢固啊 f ...
- 后台自动运行,定期记录定位数据(Hbuilder监听 app由前台切换到后台、切换运行环境的 监听方法)
http://ask.dcloud.net.cn/question/28090 https://blog.csdn.net/qq_37508970/article/details/86649703 各 ...
- maven打包时报错:-source 1.5 中不支持 diamond 运算符
报错现象: 解决方法: 在pom文件中加入下面依赖 <build> <plugins> <plugin> <groupId>org.apache.mav ...
- nginx的启动,停止和重启
启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /us ...
- [UE4]Spin Box,数字输入,可拖动
一.Spin Box在Input组下 二.Spin Box的文字样式可以在Spin Box.Display中修改 三.Spin Box事件 1.On Value Changed:值改变时触发 2.On ...