题意: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. wpf 大控件 打印 将控件转成 xps格式 并分页打印

    //PayRollPrintList:要打印的 list 可换成自己要打印的类型 private List<PayRoll> _PayRollPrintList = new List< ...

  2. iOS定位问题解决方案

    在需要用到定位服务时,需在info文件中加入: 1.NSLocationWhenInUseUsageDescription(类型为:string,值为:”我们需要通过您的地理位置信息获取您周边的相关数 ...

  3. WPF 自定义Button控件及样式

    这次通过最近做的小例子说明一下自定义Button控件和样式. 实现的效果为:

  4. PHP算法 《图》 之 理论基础

    转载自:http://www.cnblogs.com/skywang12345/p/3691463.html Ⅰ 图的基本概念 1. 图的定义 定义:图(graph)是由一些点(vertex)和这些点 ...

  5. 浅谈KMP算法及其next[]数组

    KMP算法是众多优秀的模式串匹配算法中较早诞生的一个,也是相对最为人所知的一个. 算法实现简单,运行效率高,时间复杂度为O(n+m)(n和m分别为目标串和模式串的长度) 当字符串长度和字符集大小的比值 ...

  6. iframe中在父窗口打开子页面

    我们在做页面框架的时候,通常会采用一个iframe来显示子页面,但有这么种情况,就是session失效时,登录页面就会显示在iframe中,这不符合常理,一般应该显示在顶部才对. 下面的js代码可以解 ...

  7. %s的用法

    %s 正常输出字符串printf("%s\n", "abcd"); //normal output abcd %8s 最少输出8位长度的字符串,不够在字符串左侧 ...

  8. 查看Oracle是否锁表

    --Oracle数据库操作中,我们有时会用到锁表查询以及解锁和kill进程等操作,那么这些操作是怎么实现的呢?本文我们主要就介绍一下这部分内容. --(1)锁表查询的代码有以下的形式: select ...

  9. windows phone 8学习 - 选择器

    1照相机 CameraCaptureTask cct=new CameraCaptureTask(); cct.Completed+=new EventHandler<PhotoResult&g ...

  10. input placeholder文字垂直居中(Mobile & PC)

    Html5输入框支持placeholder,但是在定义文本框中定义placeholder存在兼容问题 <input type="text" placeholder=" ...