其他分享
首页 > 其他分享> > Balanced Lineup

Balanced Lineup

作者:互联网

题目链接:https://www.luogu.com.cn/problem/P2880

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=5e4+10;
 6 int n, q, h[maxn];
 7 int a, b;
 8 int f[maxn][21], g[maxn][21];
 9 int maxh, minh;
10 void st_create(int n)
11 {
12     int k=log2(n);
13     for(int j=1; j<=k; j++)
14     {
15         for(int i=1; i+(1<<j)-1<=n; i++)
16         {
17             f[i][j]=max(f[i][j-1], f[i+(1<<(j-1))][j-1]);
18             g[i][j]=min(g[i][j-1], g[i+(1<<(j-1))][j-1]);
19         }
20     }
21 }
22 int st_max(int l, int r)
23 {
24     int k=log2(r-l+1);
25     return max(f[a][k], f[b-(1<<k)+1][k]);
26 }
27 int st_min(int l, int r)
28 {
29     int k=log2(r-l+1);
30     return min(g[a][k], g[b-(1<<k)+1][k]);
31 }
32 int main()
33 {
34     scanf("%d%d", &n, &q);
35     for(int i=1; i<=n; i++)
36     {
37         scanf("%d", &h[i]);
38         f[i][0]=g[i][0]=h[i];
39     }
40     st_create(n);
41     while(q--)
42     {
43         scanf("%d%d", &a, &b);
44         maxh=st_max(a, b);
45         minh=st_min(a, b);
46         printf("%d\n", maxh-minh);
47     }
48 
49     return 0;
50 }

 

标签:minh,10,include,21,int,maxn,Balanced,Lineup
来源: https://www.cnblogs.com/tflsnoi/p/16149339.html