HDU 5443 The Water Problem RMQ/暴力

给一个序列,长度<=1000,给q次查询(q<=1000),查询区间最大值
RMQ算法,用ST(Sparse Table)算法在线处理即可。
学习了一发RMQ算法

代码:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1005
int a[maxn], dp[maxn][20];
void RMQ_ST()
{
	for (int i = 1; i <= maxn; ++i)
		dp[i][0] = a[i];
	for (int j = 1; j < 20; ++j)
		for (int i = 1; i <= maxn; ++i)
		{
			if (i + (1 << j) - 1 <= maxn)
			{
				dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
			}
			else
				break;
		}

}
int query(int l, int r)
{
	int k = log(r - l + 1) / log(2);
	return max(dp[l][k], dp[r - (1 << k) + 1][k]);
}
int main()
{
	//freopen("input.txt", "r", stdin);
	int T, n, q, l, r;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &n);
		for (int i = 1; i <= n; ++i)
			scanf("%d", &a[i]);
		RMQ_ST();
		scanf("%d", &q);
		for (int i = 0; i < q; ++i)
		{
			scanf("%d%d", &l, &r);
			printf("%d\n", query(l, r));
		}
	}
	//while (1);
	//system("pause");
	return 0;
}

你可能感兴趣的:(HDU 5443 The Water Problem RMQ/暴力)