|
| 1 | +#include<bits/stdc++.h> |
| 2 | +#include<ext/pb_ds/assoc_container.hpp> |
| 3 | +#include<ext/pb_ds/tree_policy.hpp> |
| 4 | +#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); |
| 5 | +#define Fileio freopen("output.txt","w",stdout);freopen("input.txt","r",stdin); |
| 6 | +#define all(v) v.begin(),v.end() |
| 7 | +#define rall(v) v.rbegin(),v.rend() |
| 8 | +#define MEM(a,x) memset(a,x,sizeof(a)) |
| 9 | +#define SZ(v) v.size() |
| 10 | +#define nl "\n" |
| 11 | +#define bug cout<<"bug"<<nl; |
| 12 | +#define pi acos(-1.0) |
| 13 | +#define ll long long |
| 14 | +#define pb push_back |
| 15 | +#define mp make_pair |
| 16 | +#define pii pair< int,int > |
| 17 | +#define pll pair< ll,ll > |
| 18 | +#define vii vector< int > |
| 19 | +#define vll vector< ll > |
| 20 | +#define vpi vector< pii > |
| 21 | +#define vpl vector<pll> |
| 22 | +#define MX 100005 |
| 23 | +#define EPS 1e-12 |
| 24 | +#define ss second |
| 25 | +#define ff first |
| 26 | +using namespace std; |
| 27 | +using namespace __gnu_pbds; |
| 28 | + |
| 29 | +template<typename T> |
| 30 | +using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; |
| 31 | +template<typename T> |
| 32 | +using ordered_multiset=tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>; |
| 33 | + |
| 34 | +inline ll powr(int a,int b){ll res=1;while(b){if(b&1) res*=a;a*=a;b/=2;}return res;} |
| 35 | +int cases=1; |
| 36 | + |
| 37 | +#ifdef ppqq |
| 38 | + #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) |
| 39 | + template < typename Arg1 > |
| 40 | + void __f(const char* name, Arg1&& arg1){ |
| 41 | + cerr << name << " is " << arg1 << std::endl; |
| 42 | + } |
| 43 | + template < typename Arg1, typename... Args> |
| 44 | + void __f(const char* names, Arg1&& arg1, Args&&... args){ |
| 45 | + const char* comma = strchr(names+1, ','); |
| 46 | + cerr.write(names, comma - names) << " is " << arg1 <<" "; |
| 47 | + __f(comma+1, args...); |
| 48 | + } |
| 49 | +#else |
| 50 | + #define debug(...) |
| 51 | +#endif |
| 52 | + |
| 53 | +///******************************************START****************************************** |
| 54 | + |
| 55 | +///Problem Link : https://www.spoj.com/problems/RMQSQ/ |
| 56 | + |
| 57 | +const int LOG=20; |
| 58 | +int maxPower[MX],ara[MX],sparse[MX][LOG]; |
| 59 | + |
| 60 | +void makingSparseTable(int n) |
| 61 | +{ |
| 62 | + for(int i=0;i<n;i++) sparse[i][0]=ara[i]; |
| 63 | + |
| 64 | + for(int i=1;i<LOG;i++) |
| 65 | + { |
| 66 | + for(int j=0;j+(1<<i)-1<n;j++) |
| 67 | + { |
| 68 | + sparse[j][i]=min(sparse[j][i-1],sparse[j+(1<<(i-1))][i-1]); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | +} |
| 73 | +void preprocess(int n) |
| 74 | +{ |
| 75 | + maxPower[1]=0; |
| 76 | + for(int i=2;i<=n;i++) maxPower[i]=maxPower[i/2]+1; |
| 77 | + |
| 78 | + makingSparseTable(n); |
| 79 | +} |
| 80 | + |
| 81 | +int RMQ(int L,int R) |
| 82 | +{ |
| 83 | + int len=R-L+1; |
| 84 | + int maxLogValue=maxPower[len]; |
| 85 | + |
| 86 | + return min(sparse[L][maxLogValue] , sparse[R-(1<<maxLogValue)+1][maxLogValue]); |
| 87 | +} |
| 88 | +int main() |
| 89 | +{ |
| 90 | + FIO; |
| 91 | + |
| 92 | + int n,q; |
| 93 | + cin>>n; |
| 94 | + for(int i=0;i<n;i++) cin>>ara[i]; |
| 95 | + |
| 96 | + preprocess(n); |
| 97 | + |
| 98 | + cin>>q; |
| 99 | + |
| 100 | + while(q--) |
| 101 | + { |
| 102 | + int L,R; |
| 103 | + cin>>L>>R; |
| 104 | + |
| 105 | + cout<<RMQ(L,R)<<nl; |
| 106 | + } |
| 107 | +} |
0 commit comments