South General Command N soldiers, the soldiers are numbered 1~n, South general often love to take a certain number of the highest number of kills kill the person with the lowest number of people to compare, calculated two people's kill number difference. In this way, one can encourage those who kill a high number of people, there is also a way to criticize the low number of people, has played a very good effect.
Therefore, the South General often asked the military commander of the soldier I to the No. J Soldier. What is the military exploit difference between the person with the highest number of kills and the person with the lowest number of kills?
Now, please write a program, help the handyman to answer every time the South General's inquiry.
Attention. The South General may inquire very many times.
-
-
Input
-
-
There's just one set of test data
The first line is a two integer n,q. n indicates the total number of soldiers.
Q indicates the number of times the South General inquired. (1<n<=100000,1<q<=1000000)
The subsequent line has n integer Vi (0<=vi<100000000), each of which represents the number of kills per individual.
After the Q line, there are two positive positive m,n per line. Indicates that the south general was inquiring about the soldier number M to Soldier No. N.
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <queue> #include <stack>//#define LOWBIT (i) i& (-i)// Lowbit can be define directly. There is no need to define the function using namespace std;//then the value of f (i,0) is determined, the value of the element referred to in the position of I,//At this time we can divide the interval [i,i+2^j-1] evenly into two intervals, because the length of the interval is always even when j>=1. can be divided into intervals [i,i+2^ (J-1)-1] and intervals [i+2^ (j-1) -1,i+2^j-1]. Take two blocks of length 2^ (j-1) instead and update blocks of length 2^j. The minimum value is the minimum value for the minimum value of the two intervals, and the dynamic plan is: F[i,j]=min (f[i,j-1],f[i+2^ (j-1), j-1]).//f[i,j]=max (f[i,j-1],f[i+2^ (j-1), j-1]).//k =ln (j-i+1)/ln2//the same: The maximum value is F[i,j]=max (f[i,j-1],f[i+2^ (j-1), j-1]). F[i,j]=min (f[i,j-1],f[i+2^ (j-1) +1,j-1]);//const int N=100100;int max_sum[20][n],min_sum[20][n];void RMQ (int num) { for (int i=1;i!=20;i++) {for (int j=1;j<=num;j++) {if (j+ (1<<i) -1<=num)//i<<i==2^i{max_sum[i][j]= Max (max_sum[i-1][j],max_sum[i-1][j+ (1<<i>>1)); Min_sum[i][j]=min (min_sum[i-1][j],min_sum[i-1][j+ (1 <<I>>1)]);}}} int main () {int num,query; while (~sCANF ("%d%d", &num,&query)) {for (int i=1;i<=num;i++) {scanf ("%d", &max_sum[0][i]); Min_sum[0][i]=max_sum[0][i]; } RMQ (num); while (query--) {int begin,end; scanf ("%d%d", &begin,&end); int k= (int) (log (end-begin+1.0)/log (2.0)); int Max1=max (max_sum[k][begin],max_sum[k][end-(1<<k) +1]); int Min1=min (min_sum[k][begin],min_sum[k][end-(1<<k) +1]); printf ("%d\n", max1-min1); }}return 0;}