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 ;
}

最新文章

  1. WPF 通过Border来画边框
  2. Python+Google Geocoding
  3. ubuntu14.04下的NVIDIA Tesla K80显卡驱动的安装教程
  4. Some Simple Models of Neurons
  5. &lt; 独立项目 - 文本挖掘 &gt; - 2016/10/25 第一更 - &lt;Linux相关知识准备&gt;
  6. 在CentOS6.7操作系统上编译安装httpd2.4
  7. 【温故知新C/C++/opencv】取址符&amp;||cv::groupRectangles||引用与值传递
  8. POJ 3304 Segments (直线和线段相交判断)
  9. IOS 模仿TableView封装
  10. MVC4.0中下来列表框的,两种使用方法DropDownList
  11. android 登陆案例_sd卡
  12. MYSQL SHOW VARIABLES简介
  13. 【POJ】1035 Spell checker
  14. 《图解CSS3》——笔记(二)
  15. windows同一台电脑设置多个公钥与不同github帐号交互
  16. Python3.2官方文件翻译-工具列表和十进制浮点计算
  17. oracle sql语句跟踪及性能分析工具实现
  18. Windows苹果安卓手机远程桌面客户端推荐
  19. duilib
  20. 编辑技巧分享如何给PDF添加注释

热门文章

  1. 【Intellij】Hot Swap Failed &amp; class reloaded
  2. 大型系列课程之-七夕告白之旅vbs篇
  3. Cobbler 自动安装CentOS7
  4. java虚拟机学习笔记(四)---回收方法区
  5. 【0729 | Day 3】Python基础(一)
  6. C语言编程入门之--第五章C语言基本运算和表达式-part2
  7. xtuils
  8. mybatis基础简介
  9. 纯数据结构Java实现(4/11)(BST)
  10. SpringMVC源码分析3:DispatcherServlet的初始化与请求转发