CodeForces 601B 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的更多相关文章
- Codeforces 601B. Lipshitz Sequence(单调栈)
Codeforces 601B. Lipshitz Sequence 题意:,q个询问,每次询问给出l,r,求a数组[l,r]中所有子区间的L值的和. 思路:首先要观察到,斜率最大值只会出现在相邻两点 ...
- Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分
B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...
- 【codeforces 602D】Lipshitz Sequence
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈
题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, ...
- Codeforces 626A Robot Sequence(模拟)
A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Curious Array Codeforces - 407C(高阶差分(?)) || sequence
https://codeforces.com/problemset/problem/407/C (自用,勿看) 手模一下找一找规律,可以发现,对于一个修改(l,r,k),相当于在[l,r]内各位分别加 ...
- codeforces 622A Infinite Sequence
A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 626A Robot Sequence
A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 466d Increase Sequence
D. Increase Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- 利用dockerfile 安装一个nginx-1.14.1
FROM docker.io/centos MAINTAINER jim 107420988@qq.com ENV TZ "Asia/Shanghai" #ENV TERM xte ...
- JAVA-Spring AOP五大通知类型
一.前置通知 在目标方法执行之前执行的通知 在前置通知方法,可以没有参数,也可以额外接收一个JoinPoint,Spring会自动将该对象传入,代表当前的连接点,通过该对象可以获取目标对象和目标方法相 ...
- Kibana对数据的可视化
基于上一篇的操作,我们已经获得了数据,接下来我们就要处理数据,因此选用了Kibana 先来介绍一下, Kibana是一个针对Elasticsearch的开源分析及可视化平台,用来搜索.查看交互存储在E ...
- S2:ArrayList
1.ArrayList ArrayList非常类似于数组,也有人称它为数组列表,ArrayList可以动态维护. 因为数组的长度是固定的,而SArrayList的容量可以根据需要自动扩充. Arr ...
- ibatis 核心原理解析!
关注下方公众号,可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料. 最近查找一个生产问题的原因,需要深入研究 ibatis 框架的源码.虽然最后证明问题的原因与 ibat ...
- 转载 | Sublime Text3 安装以及初次配置
本文引自:http://blog.csdn.net/u011272513/article/details/52088800 工具:官网下载:Sublime Text3 安装:直接运行安装.http:/ ...
- java Timer工具类实现定时器任务
第一 schedule 方法 三个参数 按照顺序 (执行的任务方法,开始执行时间,多少时间后循环去执行) 代码可用 public class TestScheedule { public stati ...
- 从原理层面掌握@ModelAttribute的使用(核心原理篇)【一起学Spring MVC】
每篇一句 我们应该做一个:胸中有蓝图,脚底有计划的人 前言 Spring MVC提供的基于注释的编程模型,极大的简化了web应用的开发,我们都是受益者.比如我们在@RestController标注的C ...
- 重学计算机组成原理(六)- 函数调用怎么突然Stack Overflow了!
用Google搜异常信息,肯定都访问过Stack Overflow网站 全球最大的程序员问答网站,名字来自于一个常见的报错,就是栈溢出(stack overflow) 从函数调用开始,在计算机指令层面 ...
- if else 深度优化
一. if else表达式过于复杂 if ((condition1 && condition2 ) || ((condition2 || condition3) && ...