The interval minimum for brushing a question

Source: Internet
Author: User

A sequence of length n and a Q query, each asking for the minimum value of an interval.

Input

First line, an integer n
The second line, n number, represents a array, separated by a space.
Third line, a positive integer q
4th to Q+3 line two positive integer L, R (L<=r), representing an interval, separated by a space.

Output

For each query, output results. Each result occupies one row.

Input example

5
3 2 4) 3 5
3
1 3
2 5
3 4

Output example

2
2
3

Data size: N, Q, ai<=100000

The difficulty of this problem lies in how to do pretreatment. For large tables that are preprocessed, we only need to print one of the 2^n rows, and the remaining rows can be covered in intervals.

1#include <iostream>2 using namespacestd;3 ints[ -][100005],ll=1, L;4 intn[100005];5 intMain ()6 {7     intPl;8scanf"%d",&PL);9      for(intI=0; i<pl;i++) {scanf ("%d", &n[i]); s[0][i]=n[i];}Ten      for(; ll<pl;ll*=2) l++; Onel-=1; A      for(intI=1; i<=l;i++) -          for(intj=0; j<pl;j++) -S[i][j]=min (s[i-1][j],s[i-1][min (j+ (1<< (I-1)), pl-1)]); the     intA; -scanf"%d",&a); -      for(intI=0; i<a;i++) -     { +         intx, y; -scanf"%d%d",&x,&y); +         intcon=y-x+1; A         inttemp=0; atLl=1; -          for(; ll<con;ll*=2) temp++; -         if(temp==0) temp=1; -Ll/=2; -         if(ll==0) ll=1; -printf"%d\n", Min (s[temp-1][x-1],s[temp-1][y-ll])); in     } -     //System ("Pause>nul"); to     return 0; +}
View Code

The interval minimum for brushing a question

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.