求RMQ(其实就是怎么ST表和线段树。。。)

我们在退役前再学一遍ST表23333:

首先,ST表,我们要知道它是干什么用的:它是用来求RMQ(区间最值)问题的。但是它最好是求静态(不修改)的区间最值,而别是求动态的,如果是动态的,那就需要用线段树来做了。(动态的ST会感觉很膈应。。。详见https://www.tuicool.com/articles/Ere2qyy

  首先先来讲一下静态的做法(ST表):在这里说一说ST表其实应该叫做ST,全名为SparseTable,即稀疏表,它一般是用来求静态RMQ问题的,它的预处理时间复杂度为O(nlogn)的,查询则是O(1)的,我们先以区间最大值来说。

  其实也很简单,ST就是利用倍增的思想,每一个区间都可以被分割为两个小区间,直至到单个点即为MAX[i][0]中存储的数)。我们可以设Max[i][j]为以i为起点,长度为2^j的区间中的最大值,所以它可以一段一段的被处理,就可以了。

  而有人会问了,你又不是能保证【L,R】的区间长度一定是二的倍数,是滴,但是我们先可以求出两段区间的长度为2^k并使这个长度为仅小于r-l+1的2的倍数,即为k=log2(r-l+1),因为C++中的除法一般都是下取整的(起码这个是),所以就如下图啦:

    从l到l+2^k-1这段区间和从r-2^k+1到r这两段是一定可以把这个区间都覆盖掉的(嗯!就是这样!喵!)。

 还有要注意的是maxn的第二维开到25就够了,因为2^25为33554432,再开反正也过不了了,那就别开了,并且我们可以把maxn[100500][25]改成maxn[25][100500],因为ZAY说过这样会快的!并且这样的话就不用像我下面那样这么膈应的循环了。这样我们就可以成功的预处理出来啦!

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 using namespace std;
 6 int maxn[100500][25],m,n,l,r;
 7 int emm(int a,int b){
 8     int len=log2(b-a+1);
 9     return max(maxn[a][len],maxn[b-(1<<len)+1][len]);
10 }
11 int main(){
12     scanf("%d%d",&n,&m);
13     for(int i=1;i<=n;i++)
14     scanf("%d",&maxn[i][0]);
15     for(int i=1;i<=21;i++){//len
16         for(int j=1;j+(1<<i)-1<=n;j++){
17             maxn[j][i]=max(maxn[j][i-1],maxn[j+(1<<(i-1))][i-1]);
18         }
19     }
20     for(int i=1;i<=m;i++){
21     scanf("%d%d",&l,&r);
22     printf("%d\n",emm(l,r));
23     }
24     return 0;
25 }

 至于线段树的动态做法,我还是直接弄段代码吧,累死我惹,233。。。。见下:

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 #define MAXN 100
 6 #define MAXIND 256 //线段树节点个数
 7 
 8 //构建线段树,目的:得到M数组.
 9 void initialize(int node, int b, int e, int M[], int A[])
10 {
11     if (b == e)
12         M[node] = b; //只有一个元素,只有一个下标
13     else
14     {
15     //递归实现左孩子和右孩子
16         initialize(2 * node, b, (b + e) / 2, M, A);
17         initialize(2 * node + 1, (b + e) / 2 + 1, e, M, A);
18     //search for the minimum value in the first and
19     //second half of the interval
20     if (A[M[2 * node]] <= A[M[2 * node + 1]])
21         M[node] = M[2 * node];
22     else
23         M[node] = M[2 * node + 1];
24     }
25 }
26 
27 //找出区间 [i, j] 上的最小值的索引
28 int query(int node, int b, int e, int M[], int A[], int i, int j)
29 {
30     int p1, p2;
31 
32 
33     //查询区间和要求的区间没有交集
34     if (i > e || j < b)
35         return -1;
36 
37     //if the current interval is included in
38     //the query interval return M[node]
39     if (b >= i && e <= j)
40         return M[node];
41 
42     //compute the minimum position in the
43     //left and right part of the interval
44     p1 = query(2 * node, b, (b + e) / 2, M, A, i, j);
45     p2 = query(2 * node + 1, (b + e) / 2 + 1, e, M, A, i, j);
46 
47     //return the position where the overall
48     //minimum is
49     if (p1 == -1)
50         return M[node] = p2;
51     if (p2 == -1)
52         return M[node] = p1;
53     if (A[p1] <= A[p2])
54         return M[node] = p1;
55     return M[node] = p2;
56 
57 }
58 
59 
60 int main()
61 {
62     int M[MAXIND]; //下标1起才有意义,保存下标编号节点对应区间最小值的下标.
63     memset(M,-1,sizeof(M));
64     int a[]={3,1,5,7,2,9,0,3,4,5};
65     initialize(1, 0, sizeof(a)/sizeof(a[0])-1, M, a);
66     cout<<query(1, 0, sizeof(a)/sizeof(a[0])-1, M, a, 0, 5)<<endl;
67     return 0;
68 }

资料参考来源(http://www.cnblogs.com/zwfymqz/p/8581995.htmlhttps://www.cnblogs.com/ECJTUACM-873284962/p/6613342.html

猜你喜欢

转载自www.cnblogs.com/stars-cry/p/9715116.html
今日推荐