Lipshitz Sequence

题解:

可以通过观察得到,对于任意一个区间来说, 只有相邻的2个点的差值才会是区间的最大值。

具体观察方法,可以用数学分析, 我是通过画图得到的。

那么基于上面的观察结果。

对于一次询问, 我们可以枚举右端点, 然后, 不断的把右端点往右边移动, 然后把新的值加进来, 更新这个值所管理的区间。

用单调栈去维护每个差值所管辖的区间, 然后在这个值出栈的时候,计算答案。

好久没用单调栈了, 然后就干脆忘了这个东西..... 想用线段树去维护, 显然会T就尬住了。。。。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
int a[N];
int b[N];
int sta[N];
int cnt[N];
LL solve(int l, int r){
LL ret = ;
int top = ;
sta[] = l - ;
for(int i = l; i < r; ++i){
while(top && b[sta[top]] < b[i]){
ret += 1ll * cnt[top] * b[sta[top]] * (i-sta[top]);
top--;
}
sta[++top] = i;
cnt[top] = i - sta[top-];
}
while(top){
ret += 1ll * cnt[top] * b[sta[top]] * (r - sta[top]);
top--;
}
return ret;
}
int main(){
int n, q;
scanf("%d%d", &n, &q);
for(int i = ; i <= n; ++i)
scanf("%d", &a[i]);
for(int i = ; i < n; ++i)
b[i] = abs(a[i+] - a[i]);
int l, r;
for(int i = ; i <= q; ++i){
scanf("%d%d", &l, &r);
printf("%lld\n", solve(l, r));
}
return ;
}

CodeForces 601B Lipshitz Sequence的更多相关文章

  1. Codeforces 601B. Lipshitz Sequence(单调栈)

    Codeforces 601B. Lipshitz Sequence 题意:,q个询问,每次询问给出l,r,求a数组[l,r]中所有子区间的L值的和. 思路:首先要观察到,斜率最大值只会出现在相邻两点 ...

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

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

  3. 【codeforces 602D】Lipshitz Sequence

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈

    题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, ...

  5. Codeforces 626A Robot Sequence(模拟)

    A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  6. Curious Array Codeforces - 407C(高阶差分(?)) || sequence

    https://codeforces.com/problemset/problem/407/C (自用,勿看) 手模一下找一找规律,可以发现,对于一个修改(l,r,k),相当于在[l,r]内各位分别加 ...

  7. codeforces 622A Infinite Sequence

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces 626A Robot Sequence

    A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. codeforces 466d Increase Sequence

    D. Increase Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. 阿里云nas使用记录

    公司买了阿里云的nas服务用来共享存储,多个web服务器共同挂载同一个nas服务.挂载过程中出现如下报错 NAS报错: [root@BJ-SBC fs]# mount -t nfs 10.10.8.1 ...

  2. 基于 Autojs 的 APP、小程序自动化测试 SDK

    原文:https://blog.csdn.net/laobingm/article/details/98317394 autojs sdk基于 Autojs 的 APP.小程序自动化测试 SDK,支持 ...

  3. wpf界面按钮自动点击

    Button Button = new Button();Button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));//在按钮生成时便会自动触 ...

  4. modbus-tcp协议讲解

    MODBUS功能码简介 代码 中文名称 位操作/字操作 操作数量 01h 读线圈状态 位操作 单个或多个 02h 读离散输入状态(只能读到0或1) 位操作 单个或多个 03h 读保持寄存器(保持寄存器 ...

  5. SpringBoot第一天

    一,SpringBoot 介绍 1,如果使用 Spring 开发一个"HelloWorld"的 web 应用: • 创建一个 web 项目并且导入相关 jar 包.SpringMV ...

  6. css实现左边高度自适应右边高度

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. android ——网络编程

    一.WebView 这个View就是一个浏览器,用于展示网页的. 布局文件: <LinearLayout xmlns:android="http://schemas.android.c ...

  8. 开园第一篇---有关tensorflow加载不同模型的问题

    写在前面 今天刚刚开通博客,主要想法跟之前某位博主说的一样,希望通过博客园把每天努力的点滴记录下来,也算一种坚持的动力.我是小白一枚,有啥问题欢迎各位大神指教,鞠躬~~ 换了新工作,目前手头是OCR项 ...

  9. soap天气查询

    public class MainActivity extends AppCompatActivity { private TextView tvContent; @Override protecte ...

  10. Sentry错误日志监控你会用了吗?

    无论作为新手还是老手程序员在程序的开发过程中,代码运行时难免会抛出异常,而且项目在部署到测试.生产环境后,我们便不可能像在开发时那样容易的及时发现处理错误了.一般我们都是在错误发生一段时间后,错误信息 ...