月度归档:2016年06月

hdu 5443 The Water Problem(ST表)

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 a1,a2,a3,...,anrepresenting 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 al and ar.

 

Input
First you are given an integer T(T10) indicating the number of test cases. For each test case, there is a number n(0n1000) on a line representing the number of water sources. n integers follow, respectively a1,a2,a3,...,an, and each integer is in {1,...,106}. On the next line, there is a number q(0q1000) representing the number of queries. After that, there will be q lines with two integers l and r(1lrn) 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
裸的st表。

 

poj 3368 Frequent values(SparseTable)

Description

You are given a sequence of n integers a1 , a2 , … , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , … , aj.

Input

The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , … , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, …, n}) separated by spaces. You can assume that for each i ∈ {1, …, n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the
query.

The last test case is followed by a line containing a single 0.

Output

For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

Sample Input

Sample Output

给定一个有序的序列,然后询问一个区间内最多次数出现的数字的次数。

可以用ST表的思路,因为序列是有序的,所以说相同数字一定是在一起的。所以我们可以预先扫一遍这个序列,确定出每个数各是这个数字的第几个,用一个dis数组来存这个信息。然后用这个数组来预处理得到st表,那么我们查询区间的时候,只需要查找区间最大值即可。

但是并没有那么简单,上面那种思路有一个误区,比如:1,1,1,1,1,2,2这样一个序列,如果我要查5-7这样一个区间,那么得到的答案是5而不是2。因为上面那种思路只适用于区间左端点l正好是这个数字的第一号元素。所以正是因为这一点,可以再开一个数组R来记录每一个数的右边界,然后我们判断一下R[l]的情况即可。等于说把查询的区间分为:l-R[l]和R[l]+1-r两段。

然后就可以st表查询了。

 

poj 3264 Balanced Lineup(SparseTable模板题)

Description

For the daily milking, Farmer John’s N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ ABN), representing the range of cows from A to B inclusive.

Output

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

Sample Output

一道模板题,搞出两个spares table分别维护区间最大值和最小值,然后查询的时候找出两者差值即可。