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