【ST表】洛谷 P3865 【模板】ST表
2021-03-14 10:52:00 # ACM

OI-Wiki

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#pragma GCC optimize("O2")
using namespace std;
#define LL long long
#define ll long long
#define ULL unsigned long long
#define ls rt<<1
#define rs rt<<1|1
#define one first
#define two second
#define MS 100009
#define INF 1e18
#define mod 998244353
#define Pi acos(-1.0)
#define Pair pair<LL,LL>
#define eps 1e-9

LL n,m,k;
LL lg2[MS];
LL p[MS][31];

inline LL read(){
LL x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}

void init_lg2(){
lg2[1] = 0;
for(int i=2;i<=n;i++) lg2[i] = lg2[i/2]+1;
}

int main(){
//ios::sync_with_stdio(false);
n = read();
m = read();
init_lg2();
for(int i=1;i<=n;i++){
p[i][0] = read();
}
for(int i=1;i<=lg2[n];i++){
for(int j=1;j+(1<<(i-1))<=n;j++){
p[j][i] = max(p[j][i-1],p[j+(1<<(i-1))][i-1]);
}
}
while(m--){
LL l,r;
l = read() , r = read();
LL t = lg2[r-l+1];
LL ans = max(p[l][t],p[r-(1<<t)+1][t]);
printf("%lld\n",ans);
}


return 0;
}
Prev
2021-03-14 10:52:00 # ACM
Next