• [Luogu] P3865 【模板】ST表


    题目背景

    这是一道ST表经典题——静态区间最大值

    请注意最大数据时限只有0.8s,数据强度不低,请务必保证你的每次查询复杂度为O(1)

    题目描述

    给定一个长度为 N 的数列,和M次询问,求出每一次询问的区间内数字的最大值。

    题目解析

    st[x][y]为x到x+2^y-1的最值

    nlogn预处理出st,然后O(1)询问。

    因为是最值,所以不需要考虑区间重复的问题。

     

    Code

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    
    const int MAXN = 1e6 + 5;
    
    int n,m;
    int st[MAXN][25];
    
    inline int rd() {
        int x=0,f=1;char ch=getchar();
        while(!isdigit(ch)) {f=ch=='-'?-1:1;ch=getchar();}
        while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    
    inline int query(int x,int y) {
        int k = log2(y-x+1);
        return max(st[x][k],st[y-(1<<k)+1][k]);
    }
    
    int main() {
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= n;i++) st[i][0] = rd();
        for(int j = 1;j <= 21;j++) {
            for(int i = 1;i+(1<<j)-1 <= n;i++) {
                st[i][j] = max(st[i][j-1],st[i+(1<<j-1)][j-1]);
            }
        }
        int x,y;
        for(int i = 1;i <= m;i++) {
            scanf("%d%d",&x,&y);
            printf("%d
    ",query(x,y));
        }
        return 0;
    }
  • 相关阅读:
    如何通过Restful API的方式读取SAP Commerce Cloud的Product图片
    Permute Digits
    Browser
    Jamie and Alarm Snooze
    Perfect Squares
    Color the ball
    Stars
    1086. Tree Traversals Again (25)
    1085. Perfect Sequence (25)
    1083. List Grades (25)
  • 原文地址:https://www.cnblogs.com/floatiy/p/9884307.html
Copyright © 2020-2023  润新知