File tree Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < cstdio>
2
+ #include < cmath>
3
+
4
+ using namespace std ;
5
+
6
+ int n, m;
7
+ int st[1000010 ][21 ], pow2[21 ];
8
+
9
+ inline int max (int x, int y)
10
+ {
11
+ return x > y ? x : y;
12
+ }
13
+
14
+ inline int _log2 (int x)
15
+ {
16
+ int res = 0 ;
17
+ while (x > 1 )
18
+ {
19
+ x >>= 1 ;
20
+ res++;
21
+ }
22
+ return res;
23
+ }
24
+
25
+ void init_pow2 ()
26
+ {
27
+ pow2[0 ] = 1 ;
28
+ for (int i = 1 ; i <= 20 ; i++)
29
+ {
30
+ pow2[i] = pow2[i - 1 ] * 2 ;
31
+ }
32
+ }
33
+
34
+ void build_st ()
35
+ {
36
+ init_pow2 ();
37
+ for (int i = 1 ; i <= 20 ; i++)
38
+ {
39
+ for (int j = 0 ; j < n; j++)
40
+ {
41
+ st[j][i] = max (st[j][i - 1 ], st[j + pow2[i - 1 ]][i - 1 ]);
42
+ }
43
+ }
44
+ }
45
+
46
+ int get_max (int l, int r)
47
+ {
48
+ int x = _log2 (r - l + 1 );
49
+ return max (st[l][x], st[r - pow2[x] + 1 ][x]);
50
+ }
51
+
52
+ int main ()
53
+ {
54
+ scanf (" %d%d" , &n, &m);
55
+ for (int i = 0 ; i < n; i++)
56
+ {
57
+ scanf (" %d" , &st[i][0 ]);
58
+ }
59
+ build_st ();
60
+ for (int i = 0 ; i < m; i++)
61
+ {
62
+ int a, b;
63
+ scanf (" %d%d" , &a, &b);
64
+ printf (" %d\n " , get_max (a - 1 , b - 1 ));
65
+ }
66
+
67
+ return 0 ;
68
+ }
You can’t perform that action at this time.
0 commit comments