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

别再傻傻分不清!5分钟搞懂dB、dBm、dBc在Wi-Fi、5G信号测试里的区别 本文深入解析了dB、dBm、dBc在Wi-Fi和5G信号测试中的区别与应用。通过生活化类比和实操案例,帮助读者快速理解这些关键指标的含义,掌握信号强度测量、干扰排查及网络优化的实用技巧,提升无线通信质量。 阅读详情
题目描述:
 Lipshitz Sequence
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

A function is called Lipschitz continuous if there is a real constantK such that the inequality |f(x) - f(y)| ≤ K·|x - y| holds for all. We'll deal with a more... discrete version of this term.

For an array , we define it's Lipschitz constant as follows:

  • if n < 2,
  • if n ≥ 2, over all1 ≤ i < j ≤ n

In other words, is the smallest non-negative integer such that|h[i] - h[j]| ≤ L·|i - j| holds for all1 ≤ i, j ≤ n.

You are given an array of sizen andq queries of the form[l, r]. For each query, consider the subarray; determine the sum of Lipschitz constants ofall subarrays of.

Input

The first line of the input contains two space-separated integers n and q (2 ≤ n ≤ 100 000 and1 ≤ q ≤ 100) — the number of elements in array and the number of queries respectively.

The second line contains n space-separated integers ().

The following q lines describe queries. Thei-th of those lines contains two space-separated integersli andri (1 ≤ li < ri ≤ n).

Output

Print the answers to all queries in the order in which they are given in the input. For thei-th query, print one line containing a single integer — the sum of Lipschitz constants of all subarrays of.

Sample test(s)
Input
10 4
1 5 2 9 1 3 4 2 1 7
2 4
3 8
7 10
1 9
Output
17
82
23
210
Input
7 6
5 7 7 4 6 6 2
1 2
2 3
2 6
1 7
4 7
3 5
Output
2
0
22
59
16
8
Note

In the first query of the first sample, the Lipschitz constants of subarrays of with length at least2 are:

The answer to the query is their sum.

题目大意:

       规定了一个字符串的利普希茨常数L(h)为这个字符串数组中最大的(a[i]-a[j])/(i-j)的上取整的绝对值。之后给出最多100个询问,每次询问一个区间,问这个区间的每个子串的利普希茨常数之和是多少。

思路:

        观察利普西茨常数的表达式,发现(a[i]-a[j])/i-j 就是 f : i -> a[i] 这个离散函数的(i,a[i] ),( j,a[j]] )两点间连线的斜率,之后可以证明任意一个子串的两点间斜率的最大值,一定是由相邻的两个点产生的,因为如若不是这样,那么形成最大斜率的两点中间一定有其他的点,拿出这三个点来看,如果中间这个点在两点连线的下方,那么这个中间点和右边点的连线斜率会更大,矛盾,如果在上方,左边连线大,矛盾,如果在线上,那就一样大,也是可以由相邻两个点连线产生的。而且加了上取整这个结论一样是对的。

        有了这个简化,这个常数就是一个数组中相邻两点之差最大值了,先将所有相邻两点间之差存进数组,之后对这个正反两次单调栈处理出每个数右边的第一个比他大的数的下标还有左边的第一个比他大的数的下标,没有的就用INF代替,对于给定区间,那么所有的子区间的L( h ) 最大值之和就是计算的这些差值作为最大值的区间个数 * 这个差值再求和。

p.s.1.正反处理的时候为了不重复计数,一个是单调减的栈,一个是非严格单调增的栈

      2.数据规模大,要用longlong

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<stack>
#define INF 0x3f3f3f3f
#define MAX 1000005
#define ll long long
#define F(a) for(int i=2;i<=a;i++)
#define F_(a) for(int i=a;i>1;i--)
using namespace std;
stack<int>s;
ll a[MAX],r[MAX],l[MAX];//相邻数差值,右边第一个比他大的数的下标,左边第一个比他大的数的下标
int main()
{
	int n, q,s0,s1;
	scanf("%d%d%d", &n, &q, &s0);
	F(n)
	{
		scanf("%d", &s1);
		a[i] = abs(s1 - s0);
		s0 = s1;
	}
	F(n)
	{
		while (!s.empty() && a[s.top()] < a[i])
		{
			r[s.top()] = i;
			s.pop();
		}
		s.push(i);
	}
	while (!s.empty())
	{
		r[s.top()] = INF;
		s.pop();
	}
	F_(n)
	{
		while (!s.empty() && a[s.top()] <= a[i])
		{
			l[s.top()] = i;
			s.pop();
		}
		s.push(i);
	}
	while (!s.empty())
	{
		l[s.top()] = -INF;
		s.pop();
	}
	while (q--)
	{
		ll st, e,ans=0;
		scanf("%I64d%I64d", &st, &e);
		for (ll i = st + 1; i <= e; i++)
			ans += (min(e + 1, r[i]) - i)*(i - max(st, l[i]))*a[i];
		printf("%I64d\n", ans);
	}
}


Zynq UltraScale+ MPSoC-AMP(linux+裸机) 接着 Zynq UltraScale+ MPSoC-双核裸机AMP继续 平台工具:zcu106,vitis2020.2,petalinux2019.2 文章目录1.cpu1跑裸机2.cpu0跑linux2.1petalinux设置2.2 app3.生成BOOT.bin4.zcu106测试 1.cpu1跑裸机 参照Zynq UltraScale+ MPSoC-双核裸机AMP建立的工程进行修改。BSP中stdin与out修改为uart1,uart1给裸机用,uart0给linux用。 ddr空间裸机与linu 阅读详情

相关推荐

PP-OCRv6 ONNX部署的官方和手搓方案

PP-OCRv6 基于全新设计的 PPLCNetV4 统一骨干网络,提供 tiny、small、medium 三档模型,分别面向端侧/IoT、移动端/桌面端、服务端场景。在内部多场景综合评估集上,PP-OCRv6_medium 相比 PP-OCRv5_server 识别精度提升 5.1%、检测精度提升 4.6%,同时 GPU 推理速度提升 2.37×;以仅 34.5M 参数的规模,精度超越 Qwen3-VL-235B、GPT-5.5 等大型视觉语言模型。

懒人的技术笔记 2029

codeforces 601B Lipshitz Sequence

题目链接 B. Lipshitz Sequence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A function is called Lipschitz c

madaidao的专栏 990

PCIe 6.0实战指南:从链路训练到信号完整性的避坑手册

本文是一份针对PCIe 6.0硬件工程师的实战避坑指南。文章深入解析了从NRZ到PAM4调制带来的信号完整性挑战,详细拆解了链路训练(LTSSM)的复杂过程与调试方法,并提供了从信道仿真到实验室实测(如使用Keysight示波器进行眼图与误码率分析)的完整信号完整性闭环验证策略,帮助工程师解决高速率下的链路不稳定与降速问题。

weixin_29306875的博客 350

【CodeForces - 602D】Lipshitz Sequence(思维,单调栈,斜率单调性)

题干: A functionis called Lipschitz continuous if there is a real constantKsuch that the inequality|f(x) - f(y)| ≤ K·|x - y|holds for all. We'll deal with a more... discrete version of this term....

xuanweiace的博客 542

SAP S/4HANA Fiori权限配置全解析

权限管理是企业级应用的核心功能,尤其在SAP S/4HANA这样的ERP系统中更为关键。Fiori作为SAP的现代化用户界面,其权限配置采用了分层设计理念,通过Catalog、Group、Target Mapping等组件实现业务需求与技术实现的解耦。这种架构既保证了业务部门可以灵活定义角色,又确保IT团队能精准控制权限。在实际应用中,Target Mapping作为连接业务语义与技术实现的关键桥梁,需要特别注意Semantic Object与Fiori应用清单的一致性配置。对于汽车、金融等行业客户,还需处

weixin_26767391的博客 154

《python神经网络编程》程序实现

《python神经网络编程》程序实现 通过对这本书的学习,结合这本书的内容: 完成了以下代码: 其中NN.py实现了一个类,这个类可以完成神经网络的初始化,训练以及输入数据并得出结果。 而run.py则是对神经网络类的使用 NN.py: import numpy import scipy.special #神经网络类的实现 class neuralNetwork: #构造函数初始化类 def __init__(self,inputnodes,hiddennodes,outputnodes

ms8617484的博客 623

Codeforces 601B. Lipshitz Sequence(单调栈)

Codeforces 601B. Lipshitz Sequence 题意:,q个询问,每次询问给出l,r,求a数组[l,r]中所有子区间的L值的和。 思路:首先要观察到,斜率最大值只会出现在相邻两点上,因此可以处理出d数组,d[i]=a[i]-a[i-1]。则问题转化为求d数组在指定区间内,所有子区间的最大值的和。枚举子区间的复杂度是平方级别,显然是不能接受的,所以可以用单调栈预处理出每个d[...

weixin_30883311的博客 188

从放弃到再入门之拉格朗日对偶问题推导

普通同学的解法 无约束条件:求导就可以了 等式约束:代入消元,再求导 不等式约束:分情况讨论(在边界上和不在边界上),分别对应1,2的情况 然而发现,有些情况消元特别复杂,甚至不能求解 聪明同学的解法 发现:在最优点的情况下,约束曲面的法向量和目标函数的梯度反向必相同或相反 拉格朗日乘子法如何理解? (在2维里可以形象的解释为z=f(x,y)在约束条件g(x,y)=c中,极值...

qq_34564612的博客 1万+

【Codeforces Round 333 (Div 2)D】【线段树 or ST-RMQ 初始化78msAC】Lipshitz Sequence 若干区间询问所有子区间的答案和

D. Lipshitz Sequence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A function  is called Lipschitz continu

snowy_smile的博客 1100

RV1126平台JD9366触摸屏驱动移植实战:从源码到烧录的完整指南

本文详细介绍了在RV1126平台上移植JD9366触摸屏驱动的完整流程,从环境准备、源码获取到驱动配置、内核编译、设备树设置以及固件烧录与测试验证。通过实战经验分享和常见问题排查,帮助开发者高效完成触摸屏驱动移植,适用于嵌入式Linux开发场景。

weixin_30872337的博客 414

【第48例】BLM模型之市场洞察-五看工具篇(一)

BLM市场洞察

产品线负责人 2126

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

&#13; 链接:&#13; http://codeforces.com/contest/601/problem/B&#13; 题意:&#13; For an array , we define it's Lipschitz constant  as follows:&#13; &#13; if n &lt; 2, &#13; if n ≥ 2,  over all 1 ≤ ...

花海 173

如何让 Mac mini 24小时不休眠运行 OpenClaw

如果 OpenClaw 每 5-10 分钟自动退出,launchd 连续 4 次快速重启后会判定为崩溃循环并卸载服务。:方案一(pmset)+ 方案三(launchd)组合使用,既防止系统休眠,又保证 OpenClaw 开机自启和崩溃自动恢复。:caffeinate 只在进程运行期间有效,终端关闭或进程结束后系统恢复正常睡眠策略。这条命令会自动创建 launchd plist 并启用服务,是最推荐的方式。替换为你的实际用户名路径,Node.js 路径也要改成你自己的。确保 plist 中有。

Maynor的博客 2260

Codeforces Round #333 D. Lipshitz Sequence (单调栈)

题意: 求一个序列的所有子序列的L(h)的和求一个序列的所有子序列的L(h)的和 分析: 我们可以把序列中所有的元素两两连边,我们发现相邻的两个元素一定是最大斜率贡献者之一,也就是L(h)我们可以把序列中所有的元素两两连边, 我们发现相邻的两个元素一定是最大斜率贡献者之一, 也就是L(h) 然后我们用单调栈维护相邻元素差值作为最大值,左右能维护到的距离Li,Ri然后我们用单调栈维护相邻

TaoSama's Microcosm 512

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Codeforces Round #333 (Div. 2)-D Lipshitz Sequence(单调栈)

D. Lipshitz Sequence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A function  is called Lipschitz conti

信仰.的博客 477

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Codeforces Round #333 (Div. 2) D. Lipshitz Sequence (单调栈)

D. Lipshitz Sequence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A function  is called Lipschitz conti

LzyRapX 634

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Codeforces Round #333 (Div. 2) 602D. Lipshitz Sequence 单调栈

题意: 给n,q和n个数,q个询问 每个询问有l,r代表一个区间[l,r],问这个区间包含的所有子区间的L(h)值,L(h)代表区间内任选两个位置i#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<queue> #include<stack>

humeay的博客 453

Codeforces Round #333 Lipshitz Sequence 优先队列

题目地址:http://codeforces.com/contest/602/problem/D 题意:给定n,q。n代表有n个点,横坐标是1到n。纵坐标由输入给定,之后有q(q 题解:很容易发现,斜率绝对值的最大值一定是由相邻的两个点提供,每一个相邻两个点的斜率肯定有其管辖的范围(少说是它本身)。则用优先队列找到相邻两个点的斜率的向左管辖范围和向右管辖范围。则这个斜率提供的值为 abs(斜率

fange121的博客 635

codeforces 602 D. Lipshitz Sequence (单调栈)

D. Lipshitz Sequence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A function  is called Lipschitz conti

whai的专栏 1023

[CF601B]Lipshitz Sequence

Codeforces数据结构

YveH 946

cf 602/problem/D Lipshitz Sequence

Description  根据利普希茨条件定义一个长度为n的序列h的L(h)值:  n n>=2,(1<=i< j 现给出一长度为n的序列a,给出q次查询,每次查询给出一个区间[l,r],求序列s={a[l],…,a[r]}的所有子序列L值之和  Input  第一行为两个整数n和q表示序列长度和查询次数,第二行n个整数ai表示这个序列,之后q行每行两个整数l和r表示查询区间(2

c_czl的博客 266

cf div2 #333 D. Lipshitz Sequence (单调栈)

题目:http://codeforces.com/contest/602/problem/D 题意:给定n(n)个整数的数组h[i],现在定义一个函数 For an array , we define it's Lipschitz constant  as follows: if n ,  if n ≥ 2,  over all 1 ≤ i j ≤ n 然后有

w20810的专栏 679

CodeForces 601 B.Lipshitz Sequence(单调栈)

Description 根据利普希茨条件定义一个长度为n的序列h的L(h)值: n&lt;2,L(h)=0; n&gt;=2,(1&lt;=i&lt; j&lt;=n) 现给出一长度为n的序列a,给出q次查询,每次查询给出一个区间[l,r],求序列s={a[l],…,a[r]}的所有子序列L值之和 Input 第一行为两个整数n和q表示序列长度和查询次数,第二行n个整数ai表示这个序列...

ZSQ 1110

Codeforces 602D Lipshitz Sequence【思维+斜率单调栈】

D. Lipshitz Sequence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A function  is called Lipschitz conti

mengxiang000000 704

CF 602 D. Lipshitz Sequence 数学 + 单调栈 + 优化

http://codeforces.com/contest/602/problem/D 这题需要注意到的是,对于三个点(x1, y1)和(x2, y2)和(x3, y3)。如果要算出区间[1, 3]的L(h)函数的最大值,则一定不会是 通过(y3 - y1) / (x3 - x1)算出。因为很简单,通过(x2, y2)作为辅助点,数值将会更加大。 然后设dis[i]表示abs(a[i + 1...

weixin_30892037的博客 230

CF 601B(Lipshitz Sequence-贪心)

给一个序列长度为n(<=100,000) ,q(<=100)个询问 定义一个串的常数L(h) if n < 2, L(h)=0L(h)=0 if n ≥ 2, max⌈∣∣hj−hij−i∣∣⌉max{\left\lceil \left| \frac {h_j-h_i} {j-i} \right|\right\rceil } over all 1 ≤ i < j ≤ n 对每个询问(l,r

nike0good |Oier&ACMer | 熟能生巧 1010
上一篇: Hello CSDN!
下一篇: hdu 5514 Frog(容斥原理)
chat_c
博客等级 码龄12年 10粉丝 45原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值