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 continuous if there is a real constant K 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 all 1 ≤ i < j ≤ n

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

You are given an array  of size n and q queries of the form [l, r]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of .

Input

The first line of the input contains two space-separated integers n and q (2 ≤ n ≤ 100 000 and 1 ≤ 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. The i-th of those lines contains two space-separated integers li and ri (1 ≤ li < ri ≤ n).

Output

Print the answers to all queries in the order in which they are given in the input. For the i-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 least 2 are:

The answer to the query is their sum.


发现L(h)其实是相邻两个数绝对值的最大值,考虑每个值的贡献,然后单调栈预处理就好了
/*======================================================
# Author: whai
# Last modified: 2015-11-25 12:48
# Filename: d1.cpp
======================================================*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>

using namespace std;

#define LL __int64
#define PB push_back
#define P pair<int, int>
#define X first
#define Y second

const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;

int a[N];
int b[N];
int L[N], R[N];

int _abs(int x) {
	if(x < 0) return -x;
	return x;
}

P st[N];
int top = -1;

void predo(int n) {
	for(int i = 1; i < n; ++i) {
		b[i] = _abs(a[i + 1] - a[i]);
	}
	//for(int i = 1; i < n; ++i) {
	//	cout<<b[i]<<' ';
	//}
	//cout<<endl;
	top = -1;
	st[++top] = P(INF, 0);
	for(int i = 1; i < n; ++i) {
		int j;
		for(j = top; j >= 0; --j) {
			if(b[i] <= st[j].X) break;
			else R[st[j].Y] = i - 1;
		}
		L[i] = st[j].Y + 1;
		top = j;
		st[++top] = P(b[i], i);
	}
	for(int i = 0; i <= top; ++i) {
		R[st[i].Y] = n - 1;
	}
	//for(int i = 1; i < n; ++i) {
	//	cout<<L[i]<<' '<<R[i]<<endl;
	//}
}

void gao(int l, int r) {
	LL ans = 0;
	for(int i = l; i < r; ++i) {
		LL left = (i - max(L[i], l) + 1);
		LL right = (min(R[i], r - 1) - i + 1);
		ans += left * right * b[i];
	}
	cout<<ans<<endl;

}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
	}
	predo(n);
	for(int i = 0; i < m; ++i) {
		int u, v;
		scanf("%d%d", &u, &v);
		gao(u, v);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单调栈是一种常用的数据结构,用于解决一类特定的问题,其中最常见的问题是找到数组中每个元素的下一个更大或更小的元素。在Codeforces编程竞赛中,单调栈经常被用于解决一些与数组相关的问题。 下面是单调栈的一般思路: 1. 创建一个空栈。 2. 从左到右遍历数组元素。 3. 对于每个元素,将其与栈顶元素进行比较。 - 如果当前元素小于等于栈顶元素,则将当前元素入栈。 - 如果当前元素大于栈顶元素,则将栈顶元素弹出,并将当前元素入栈。 4. 重复步骤3,直到遍历完所有元素。 这样,最后剩下的栈中元素就是没有下一个更大或更小元素的元素。在使用单调栈求解具体问题时,我们可以根据需要进行一些特定的操作。 例如,如果要找到一个数组中每个元素的下一个更大的元素,可以使用单调递减栈。具体操作如下: 1. 创建一个空栈和一个空结果数组。 2. 从左到右遍历数组元素。 3. 对于每个元素,将其与栈顶元素进行比较。 - 如果当前元素小于等于栈顶元素,则将当前元素入栈。 - 如果当前元素大于栈顶元素,则将栈顶元素弹出,并将其在结果数组中的位置记录为当前元素的下一个更大元素的索引。 4. 将当前元素入栈。 5. 重复步骤3和4,直到遍历完所有元素。 6. 结果数组中没有下一个更大元素的位置,可以设置为-1。 以下是一个使用单调递减栈求解下一个更大元素问题的示例代码: ```cpp #include <iostream> #include <stack> #include <vector> std::vector<int> nextGreaterElement(std::vector<int>& nums) { int n = nums.size(); std::vector<int> result(n, -1); std::stack<int> stack; for (int i = 0; i < n; i++) { while (!stack.empty() && nums[i] > nums[stack.top()]) { result[stack.top()] = i; stack.pop(); } stack.push(i); } return result; } int main() { std::vector<int> nums = {1,3, 2, 4, 5, 1}; std::vector<int> result = nextGreaterElement(nums); for (int i = 0; i < result.size(); i++) { std::cout << "Next greater element for " << nums[i] << ": "; if (result[i] != -1) { std::cout << nums[result[i]]; } else { std::cout << "None"; } std::cout << std::endl; } return 0; } ``` 以上代码将输出: ``` Next greater element for 1: 3 Next greater element for 3: 4 Next greater element for 2: 4 Next greater element for 4: 5 Next greater element for 5: None Next greater element for 1: None ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值