ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup

题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup

题解:

ST表板子

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #define max(a,b) ((a)>(b)?(a):(b))
 5 #define min(a,b) ((a)<(b)?(a):(b))
 6 using namespace std;
 7 const int maxn=50050,max_log=18,maxlog=17,inf=1<<30;
 8 int Fmx[maxn][max_log],N,Q,Fmn[maxn][max_log],LOG[maxn],A,B;
 9 int main(){
10     scanf("%d%d",&N,&Q);
11     for(int i=0;i<=N;i++)
12         for(int j=0;j<=maxlog;j++)
13             Fmn[i][j]=inf;
14     for(int i=1;i<=N;i++){
15         scanf("%d",&Fmx[i][0]);
16         Fmn[i][0]=Fmx[i][0];
17     }
18     LOG[0]=-1;
19     for(int i=1;i<=N;i++)LOG[i]=LOG[i>>1]+1;
20     for(int j=1;j<=maxlog;j++)//
21         for(int i=1;i+(1<<j)-1<=N;i++){
22             Fmx[i][j]=max(Fmx[i][j-1],Fmx[i+(1<<(j-1))][j-1]);
23             Fmn[i][j]=min(Fmn[i][j-1],Fmn[i+(1<<(j-1))][j-1]);
24         }
25     while(Q--){
26         scanf("%d%d",&A,&B);
27         int x=LOG[B-A+1],ans1,ans2;
28         ans1=max(Fmx[A][x],Fmx[B-(1<<x)+1][x]);
29         ans2=min(Fmn[A][x],Fmn[B-(1<<x)+1][x]);
30         printf("%d\n",ans1-ans2);
31     }
32     return 0;
33 }

By:AlenaNuna

posted @ 2019-03-21 21:37  AlenaNuna  阅读(84)  评论(0编辑  收藏  举报