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 ...
随机推荐
- ES6语法知识
let/const(常用) let,const用于声明变量,用来替代老语法的var关键字,与var不同的是,let/const会创建一个块级作用域(通俗讲就是一个花括号内是一个新的作用域) 这里外部的 ...
- Delphi 10-10.2.2启动提示JS错误的解决办法
HKEY_CURRENT_USER\SOFTWARE\Embarcadero\BDS\18.0\Known IDE Packages\ $(BDS)\Bin\CommunityToolbar240.b ...
- repos配置
- Ubuntu 14.10 下安装Ambari
安装ambari有两种方式,一是自己下载源码编译,另外一个是使用公共仓库 1 使用Public Respositories Step1: Download the Ambari repository ...
- Android之MainActivity类
一.MainActivity: 1.每个种语言都有一个程序入库(如:C#main函数),而Android程序的入口就是Mani Actiivty函数. 2.Activity是Android的核心类(a ...
- 快速傅立叶变换(FFT)算法
已知多项式f(x)=a0+a1x+a2x2+...+am-1xm-1, g(x)=b0+b1x+b2x2+...+bn-1xn-1.利用卷积的蛮力算法,得到h(x)=f(x)g(x),这一过程的时间复 ...
- FPGA editor 的使用之一 ------ Probe探针
做FPGA设计经常会用到FPGA editor工具,今天开始总结下使用FPGA editor工具的经验. 1.添加probes 在debug时,经常要分析设计中的某一个信号的状态变化,需要观测这个信号 ...
- Python 爬虫 Vimeo视频下载链接
python vimeo_d.py https://vimeo.com/228013581 在https://vimeo.com/上看到稀罕的视频 按照上面加上视频的观看地址运行即可获得视频下载链接 ...
- Eclipse Debug不能热部署解决
摘要: Eclipse Debug不能热部署代码may be out-of-synch 问题描述:Project通过ant工具编译部署到JBoss下,在使用Eclipse远程调试功... ...
- tomcat advanced (RUNNING)
1. 1. tomcat