cogs 58. peaks stretches ST table

58. stretches of mountain

★★ Input file: climb.in   Output file: climb.out   a simple comparison of
the time limit: 1 s memory limit: 512 MB
Title Description

There is a continuous stretches, ups and downs of the mountain, the lowest elevation of 0, the highest elevation of not more than 8848 meters, went to the other end of the process, from one end of the mountain, every 1 meters above sea level will increase or decrease one meter . Q climbers have a plan in different sections of climbing the mountain, when they climbed to the highest peak in each section, will plug pennants. You are to write a program to find out their flags height.
 
Input File
 
Line 1, an integer N (N <= 10 ^ 6), represents the span ends of the mountain.
Next, N + 1 lines, each the Hi a non-negative integer, indicates the altitude of the location, where H0 = Hn = 0.
Is a positive integer and Q (Q <= 7000), indicates the number of climbers.
Next Q lines of two numbers Ai, Bi, denotes the i th segment climbers to climb [Ai, Bi], where 0 <= Ai <= Bi <= N.
 
Output file
 
Q lines, each behavior integer, denotes the i th climbers flags height.
 
Sample input
10
0
1
2
3
2
3
4
3
2
1
0
5
0 10
2 4
3 7
7 9
8 8
 
Sample Output
4
3
4
3
2
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=1<<20|31;
int n,m,st[21][maxn],pre[maxn];
int main(){
    freopen("climb.in","r",stdin);freopen("climb.out","w",stdout);
    scanf("%d",&n);int i,k,s,t;
    for(i=0 ; <= n-I; I ++) Scanf ( " % D " , & ST [ 0 ] [I]);
     for (I = 0 ; I < 21 is ; I ++) pre [ . 1 << I] = I;
     for (I = . 1 ; <= n-I; I ++) IF (pre [I]) pre [I] = pre [I-! . 1 ]; // this is a pre-treatment in advance, such as tips pre [16] = 4 then the pre [ 17] = pre [18] = pre [19] = pre [20] = ...... = 4 behind this way do not have to ask the individual! ! !
    for (K = . 1 ; ( . 1 << (- K- . 1 )) <n-; K ++ )
         for (I = . 1 ; I <= n-; I ++ ) 
            ST [K] [I] = max (ST [- K- . 1][i],st[k-1][i+(1<<(k-1))]);
    scanf("%d",&m);
    while(m--){
        scanf("%d%d",&s,&t),s=max(1,s),k=pre[t-s+1];
        i=max(st[k][s],st[k][t-(1<<k)+1]);
        printf("%d\n",i);
    }
}

 

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11309995.html