Nyoj-119-Soldier Kill (3)-RMQ algorithm

Source: Internet
Author: User

Soldier Kills (iii) time limit: -Ms | Memory Limit:65535KB Difficulty:5
Descriptive narrative

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.

Output
for each inquiry. Output the difference between the maximum and minimum of all soldiers killed by the number M soldier to the nth soldier.

Example input
5 21 2 6 9 31 22 4
Example output
17
#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 &LT;&LT;I&GT;&GT;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;}


Nyoj-119-Soldier Kill (3)-RMQ algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.