题意:n个点, 坐标已知,其中横坐标为为1~n。 求区间[l, r] 的所有子区间内斜率最大值的和。

首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的。

然后问题就变成了求区间[l, r]内所有子区间最大值的和。

这个问题可以利用单调栈来做。

每次找到当前点左面第一个大于当前值的点, 然后更新答案。 姿势很多。

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.Scanner; public class Main {
static Scanner cin = new Scanner(new BufferedInputStream(System.in));
static PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
static final int maxn = 100005;
public static void main(String[] args) {
int []height = new int[maxn];
while (cin.hasNext()){
int n = cin.nextInt();
int q = cin.nextInt();
height[0] = 0;
for (int i = 1; i <= n; i++){
height[i] = cin.nextInt();
height[i-1] = Math.abs(height[i]-height[i-1]);
}
int []stack = new int[maxn];
int top = -1;
for (int i = 0; i < q; i++){
int l = cin.nextInt();
int r = cin.nextInt();
long res = 0, cur = 0;
top = -1;
for (int j = l; j < r; j++){
while (top >= 0 && height[stack[top]] <= height[j]){
cur -= 1L * height[stack[top]] * (stack[top] - (top==0 ? l-1 : stack[top-1]));
top--;
}
if (top >= 0){
cur += 1L* (j - stack[top]) * height[j];
}else{
cur += 1L * (j - l + 1) * height[j];
}
stack[++top] = j;
res += cur;
} cout.println(res);
}
cout.flush();
}
} }

Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈的更多相关文章

  1. Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分

    B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...

  2. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

  3. Codeforces Round #333 (Div. 2)

    水 A - Two Bases 水题,但是pow的精度不高,应该是转换成long long精度丢失了干脆直接double就可以了.被hack掉了.用long long能存的下 #include < ...

  4. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  5. Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...

  6. Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)

    链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...

  7. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  8. Codeforces Round #353 (Div. 2) A. Infinite Sequence

    Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its fi ...

  9. Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp

    C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

随机推荐

  1. Canvas模糊化处理图片、毛玻璃处理图片之stackblur.js

    Canvas实现毛玻璃效果解决方式1:使用stackblur.js 在Android系统中实现图片的毛玻璃效果比较好用的类库是:Android StackBlur 官方Git地址:https://gi ...

  2. 我的C# - Web - DAL- DBHelper.cs

    其中的部分内容是别人的,我修改过了并加入了详细的注释!!! 一.这个DBHelper的大致块儿如下图: 二.下面是具体的源代码: //命名空间..... using System;using Syst ...

  3. ip接口调用

    <?php header("Content-type: text/html; charset=utf-8"); function getIP(){ if (isset($_S ...

  4. MySQL数据库迁移详细步骤(转)

    ========================================================================================== 一.背景简介 == ...

  5. Git 基础再学习之:git checkout -- file

    首先明白一下基本概念和用法,这段话是从前在看廖雪峰的git教程的时候摘到OneNote的 准备工作: 新建了一个learngit文件夹,在bash中cd进入文件夹,用以下命令创建一个仓库. $ git ...

  6. CListCtrl 的应用

    CMFCListCtrl实现的功能:比较特别的就是通过CImage来扩展每行的高度 CMFCListCtrl m_lisTestResult; 1.插入列(创建表格) m_lisTestResult. ...

  7. SGU 163.Wise King

    一道题目长的水题.... 总结就一句话,给出n个(-3~3)的数,一个数m,取任意个数是使这些数的m次幂之和最大. code #include <iostream> #include &l ...

  8. SGU 178.Chain

    Solution: 一开始做的时候,以为可以将一条长度为n的链分成和n为的任意长度的3部分.结果第二组就Wa了 后来参考了题解,发现是将长度为n的链分成长度为1,x,n-1-x的三条链.再看看题目,不 ...

  9. div浮动框居于浏览器窗口中间

    代码先贴这里,随后再改 <script language="JavaScript"> document.getElementById('divCenter').styl ...

  10. virtualbox 安装 android 经验总结

    装了好多个版本,最终总结一下遇到的问题, 1.直接下载的镜像文件没有找到如何设置分辨率的方法,因此放弃使用 2.在安装过程中,首先创建虚拟机,在virtualbox中创建硬盘的时候一定要选HDD格式, ...