hdu 5443 The Water Problem

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5443  

The Water Problem

Description

In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with $a_1, a_2, a_3, . . . , a_n$ representing the size of the water source. Given a set of queries each containing $2$ integers $l$ and $r$, please find out the biggest water source between $a_l$ and $a_r$.

Input

First you are given an integer $T (T leq 10)$ indicating the number of test cases. For each test case, there is a number $n (0 leq n leq 1000)$ on a line representing the number of water sources. n integers follow, respectively $a_1, a_2, a_3, . . . , a_n,$ and each integer is in ${1, . . . , 10^6}$. On the next line, there is a number $q (0 leq q leq 1000)$ representing the number of queries. After that, there will be $q$ lines with two integers $l$ and $r (1 leq l leq r leq n)$ indicating the range of which you should find out the biggest water source.

Output

For each query, output an integer representing the size of the biggest water source.

Sample Input

3
1
100
1
1 1
5
1 2 3 4 5
5
1 2
1 3
2 4
3 4
3 5
3
1 999999 1
4
1 1
1 2
2 3
3 3

Sample Output

100
2
3
4
4
5
1
999999
999999
1

裸的rmq问题,st表水之。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
using std::max;
using std::sort;
using std::pair;
using std::swap;
using std::queue;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1010;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
int n, arr[N], st[N + 5][11];
struct SparseTable {
	inline void init() {
		rep(i, n) st[i][0] = arr[i];
		for (int j = 1; (1 << j) <= n; j++) {
			for (int i = 0; i + (1 << j) <= n; i++) {
				st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
			}
		}
	}
	inline int rmq(int a, int b) {
		int k = __builtin_clz((int)1) - __builtin_clz(b - a + 1);
		return max(st[a][k], st[b - (1 << k) + 1][k]);
	}
}go;
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int t, a, b, q;
	scanf("%d", &t);
	while(t--) {
		scanf("%d", &n);
		rep(i, n) scanf("%d", &arr[i]);
        go.init();
        scanf("%d", &q);
        while(q--) {
			scanf("%d %d", &a, &b);
			printf("%d
", go.rmq(a - 1, b - 1));
        }
	}
	return 0;
}
原文地址:https://www.cnblogs.com/GadyPu/p/4814497.html