第九章 高级数据结构

第九章 高级数据结构

1、二维单调队列

2、树状数组

3、带权并查集

4、st表

📖区间最值查询问题


⭐️ST表的实现


📖例题:RMQ最值查询



📚

java 复制代码
import java.util.*;

public class Main {
    final static int N = 100005;
    final static int M = (int)(Math.log(N) / Math.log(2) + 1);
    static int n,m;
    static int[] a = new int[N];
    static int[][] f = new int[N][M];
    static int[] log2 = new int[N];
    // ST表 预处理
    static void init(){
        // 优化:log2的处理
        log2[1] = 0;
        for (int i = 2; i < N; i++) {
            log2[i] = log2[i>>1] + 1;
        }
        for (int j = 0; j < M; j++) { // 指数
            for (int i = 1; i + (1 << j) - 1 <= n; i++) {
                if(j == 0){ // 自己本身
                    f[i][j]  = a[i];
                }else{
                    f[i][j] = Math.max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }
        m = sc.nextInt();
        
        init();

        // ST表 查询
        while (m --> 0) {
            int l = sc.nextInt();
            int r = sc.nextInt();
            int ans = query(l,r);
            System.out.println(ans);
        }
    }

    static int query(int l,int r){
        int len = r - l + 1;
        int k = log2[len];
        return Math.max(f[l][k],f[r-(1<<k)+1][k]);
    }
}

📚区间最大值

5、线段树

相关推荐
Old Uncle Tom15 小时前
OpenClaw 记忆系统 -- 记忆预加载
java·数据结构·算法·agent
会编程的土豆15 小时前
洛谷题单入门1 顺序结构
数据结构·算法·golang
JasmineX-118 小时前
数据结构(笔记)——双向链表
c语言·数据结构·笔记·链表
嘻嘻哈哈樱桃21 小时前
牛客经典101题题解集--动态规划
java·数据结构·python·算法·职场和发展·动态规划
电科一班林耿超21 小时前
第 16 课:动态规划专题(二)—— 子序列与子数组问题:面试最高频的 DP 题型
数据结构·算法·动态规划
hnjzsyjyj21 小时前
洛谷 B3622:枚举子集(递归实现指数型枚举)← DFS
数据结构·dfs
qiqsevenqiqiqiqi1 天前
MT2048三连 暴力→数学推导→O (n) 优化
数据结构·c++·算法
码之气三段.1 天前
十五届山东ccpc省赛补题(update)
数据结构·c++·算法
保持清醒5401 天前
二叉链表实现
数据结构
paeamecium1 天前
【PAT甲级真题】- Recover the Smallest Number (30)
数据结构·算法·pat考试·pat