标签:style blog io color os for sp on 2014
代码如下:
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> using namespace std; const int MAXN = 100117; int n,query; int num[MAXN]; int F_Min[MAXN][20],F_Max[MAXN][20]; void Init() { for(int i = 1; i <= n; i++) { F_Min[i][0] = F_Max[i][0] = num[i]; } for(int i = 1; (1<<i) <= n; i++) //按区间长度递增顺序递推 { for(int j = 1; j+(1<<i)-1 <= n; j++) //区间起点 { F_Max[j][i] = max(F_Max[j][i-1],F_Max[j+(1<<(i-1))][i-1]); F_Min[j][i] = min(F_Min[j][i-1],F_Min[j+(1<<(i-1))][i-1]); } } } int Query_max(int l,int r) { int k = (int)(log(double(r-l+1))/log((double)2)); return max(F_Max[l][k], F_Max[r-(1<<k)+1][k]); } int Query_min(int l,int r) { int k = (int)(log(double(r-l+1))/log((double)2)); return min(F_Min[l][k], F_Min[r-(1<<k)+1][k]); } int main() { int a,b; scanf("%d %d",&n,&query); for(int i = 1; i <= n; i++) scanf("%d",&num[i]); Init(); while(query--) { scanf("%d %d",&a,&b); printf("区间%d到%d的最大值为:%d\n",a,b,Query_max(a,b)); printf("区间%d到%d的最小值为:%d\n",a,b,Query_min(a,b)); printf("区间%d到%d的最大值和最小值只差为:%d\n",a,b,Query_max(a,b)-Query_min(a,b)); } return 0; }
标签:style blog io color os for sp on 2014
原文地址:http://blog.csdn.net/u012860063/article/details/40752197