[USACO07JAN]平衡的阵容Balanced Lineup RMQ模板题

Posted guangheli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO07JAN]平衡的阵容Balanced Lineup RMQ模板题相关的知识,希望对你有一定的参考价值。

Code:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 50000 + 12;
int A[maxn];
int dmax[maxn][40];
int dmin[maxn][40];
void init(int *A, int n){
	for (int i = 1; i <= n; ++i)
		dmin[i][0] = dmax[i][0] = A[i];
	for(int j=1;(1<<j)<=n;++j)
		for (int i = 1; i + (1 << j) - 1 <= n; ++i)
		{
			dmin[i][j] = min(dmin[i][j-1], dmin[i + (1 << (j - 1))][j - 1]);
			dmax[i][j] = max(dmax[i][j-1], dmax[i + (1 << (j - 1))][j - 1]);
		}
}
int RMQ(int L, int R){
	int k = 0;
	while (L + (1 << (k + 1)) - 1 <= R)++k;
	int big = max(dmax[L][k], dmax[R - (1 << k)+1][k]);
	int small = min(dmin[L][k], dmin[R - (1 << k) + 1][k]);
	return big - small;
}
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) scanf("%d", &A[i]);
	init(A, n);
	for (int i = 1; i <= m; ++i)
	{
		int L, R;
		scanf("%d%d", &L, &R);
		printf("%d
", RMQ(L, R));
	}
	return 0;
}

  

以上是关于[USACO07JAN]平衡的阵容Balanced Lineup RMQ模板题的主要内容,如果未能解决你的问题,请参考以下文章

luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解

P2880 [USACO07JAN]平衡的阵容Balanced Lineup(RMQ的倍增模板)

P2880 [USACO07JAN]平衡的阵容Balanced Lineup

洛谷P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)

洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片

[USACO17JAN]Balanced Photo平衡的照片