码迷,mamicode.com
首页 > 其他好文 > 详细

ST表

时间:2020-03-26 13:45:36      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:int   定义   printf   blog   ++   dig   turn   nbsp   def   

https://www.cnblogs.com/bestcoder-Yurnero/p/11011868.html

#include<bits/stdc++.h>
#define ri register int
#define ll long long
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == -) f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - 0;
    return k * f;
}
int n, m, maxn[100005][22];
int query(int l, int r){
    int k = log2(r - l + 1);
    return max(maxn[l][k], maxn[r - (1 << k) + 1][k]);
}
int main(){
    n = read();
    m = read();
    for(ri i = 1; i <= n; ++i){
        maxn[i][0] = read();
    }
    for(ri j = 1; j <= 21; ++j)
        for(ri i = 1; i + (1 << j) <= n + 1; ++i)// i, j反着定义有一丝小难受- - 
            maxn[i][j] = max(maxn[i][j - 1], maxn[i + (1 << (j - 1))][j - 1]);
    for(ri i = 1; i <= m; ++i){
        int l = read(), r = read();
        printf("%d\n", query(l, r));
    }
    return 0;
}

 

ST表

标签:int   定义   printf   blog   ++   dig   turn   nbsp   def   

原文地址:https://www.cnblogs.com/kojoker/p/12573763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!