File tree 1 file changed +65
-0
lines changed
1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < cstdio>
2
+ #include < iostream>
3
+ #include < string>
4
+ #include < algorithm>
5
+ #include < utility>
6
+ #include < queue>
7
+ #include < stack>
8
+ #include < string>
9
+ #include < cstring>
10
+ #include < cmath>
11
+ #include < map>
12
+ #include < vector>
13
+ #include < array>
14
+ #include < set>
15
+ #include < climits>
16
+ #include < sstream>
17
+ #include < iomanip>
18
+ #include < cassert>
19
+ #include < bitset>
20
+ #include < numeric>
21
+ using namespace std ;
22
+ #define MOD 1000000007
23
+ #define MAXN 100004
24
+ #define K 17
25
+ long long log_table[MAXN+1 ];
26
+ long long sparse_table[MAXN][K];
27
+ long long arr[MAXN];
28
+ long long n , q , qs , qe;
29
+ void pre (void ){ // generating the log table fast
30
+ log_table[1 ] = 0 ;
31
+ for (int i=2 ; i <= MAXN; ++i){
32
+ log_table[i] = log_table[i/2 ]+1 ;
33
+ }
34
+ return ;
35
+ }
36
+ int main (void ){
37
+ ios::sync_with_stdio (false );
38
+ cin.tie (nullptr );
39
+ cout.tie (nullptr );
40
+
41
+ #ifdef HELL_JUDGE
42
+ freopen (" input" ," r" ,stdin);
43
+ freopen (" output" ," w" ,stdout);
44
+ freopen (" error" ," w" ,stderr);
45
+ #endif
46
+ pre ();
47
+ // for range minimum query f(x,y) = min(x,y)
48
+ cin>>n;
49
+ for (int i=0 ;i<n;++i){
50
+ cin>>arr[i];
51
+ sparse_table[i][0 ] = arr[i];
52
+ }
53
+ for (int j=1 ;j<=K;++j){ // 2^j value starting from index i
54
+ for (int i=0 ;i+(1 <<j)<=n;++i){
55
+ sparse_table[i][j] = min (sparse_table[i][j-1 ],sparse_table[i+(1 <<(j-1 ))][j-1 ]);
56
+ }
57
+ }
58
+ cin>>q;
59
+ while (q--){
60
+ cin>>qs>>qe;
61
+ int j= log_table[qe-qs+1 ];
62
+ cout<<min (sparse_table[qs][j],sparse_table[qe-(1 <<j)+1 ][j])<<endl;
63
+ }
64
+ return 0 ;
65
+ }
You can’t perform that action at this time.
0 commit comments