Describe
Once there was a man named W and N and B, who had a genius memory, and he treasured many treasures. After his death left behind a difficult problem (special test of memory Ah! If anyone can answer this question easily, he can inherit his treasure. The title is this: give you a large number of numbers (numbered 1 to N, size is not necessarily oh!) After you have seen it, it disappears in front of you, then the problem arises, give you M inquiry, every time you ask to give you two number A, a, ask you to say in a moment is a to B the maximum number within the interval. One day, a beautiful sister flew from the sky, to see the problem, it is very interesting (mainly said that the treasure is hidden in a beauty water, drink can make this beautiful sister more charming), so she tried her best to solve the problem. But, every time she failed, because the number is too many! So she asked the genius of you to help him solve. If you help her to solve this problem, but will get a lot of sweetness of Oh!
Format input Format
An integer n indicates the number of digits, followed by the number of N. The third line reads a m, indicating the number of times you need to be asked when you have finished reading that number, followed by M-line with two integers, a, a, a, B.
Output format
Outputs a total of m rows, one per line output.
Sample Input 1
634 1 8 123 3 241 21 53 42 3
Sample Output 1
341231238
RMQ
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <string.h>5#include <math.h>6#include <map>7 #defineINF 100000008 using namespacestd;9 Ten intN; One intf[200001][ A]; A voidRMQ () - { - the for(intj=1; J<=log (n)/log (2); J + +) - { - for(intI=1; i<=n;i++) - { +f[i][j]=f[i][j-1]; - if(I+ (1<< (J-1)) <=N) + { AF[i][j]=max (f[i][j-1],f[i+ (1<< (J-1))][j-1]); at } - } - } - } - intMain () - { in intm,x,y; - inta[200001]; toscanf"%d",&n); + for(intI=1; i<=n;i++) - { thescanf"%d",&a[i]); * } $ for(intI=1; i<=n;i++)Panax Notoginseng { -f[i][0]=A[i]; the } + RMQ (); Ascanf"%d",&m); the for(intI=1; i<=m;i++) + { -scanf"%d%d",&x,&y); $ intK=log (y-x+1)/log (2); $printf"%d\n", Max (f[x][k],f[y-(1<<K) +1][k])); - } - return 0; the}
"Vijos" P1514 Genius's memory