RMQ

区间最值问题

1.ST算法,适合静态区间求值,动态使用线段树

 1 int f[100005][17];
 2 int a[100005];
 3 void RMQ(int n)
 4 {
 5     for(int i=1;i<=n;i++)
 6         f[i][0] = a[i];
 7     for (int j = 1; (1< )
 8     {
 9         for (int i = 1; i+(1<1)<=n;i++)
10         {
11             f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
12         }
13     }
14 }
15 int main(void)
16 {    
17     int N, M;    //N:元素个数 M:询问次数
18     cin >> N >> M;
19     for (int i = 1; i <= N; i++)
20         cin >> a[i];
21     RMQ(N);
22     for (int i = 1; i <= M; i++)
23     {
24         int l, r;
25         cin >> l >> r;
26         int k = log2(r - l + 1);
27         cout << max(f[l][k], f[r - (1<1][k])<<endl;
28     }
29     return 0;
30 }

 

你可能感兴趣的:(RMQ)