算法笔记————ST表

运用了倍增思想,从小到大处理

1.【模板】ST 表

// Problem: 
//     P3865 【模板】ST 表
//   
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3865
// Memory Limit: 125 MB
// Time Limit: 800 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
const int N=1e5+10;
int d[N][20];
#define endl '\n'

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n,m;cin>>n>>m;
	for(int i=1;i<=n;++i) cin>>d[i][0];
    for(int i=1;i<=19;++i){
    	for(int j=1;j+(1<<i)-1<=n;++j){
    		d[j][i]=max(d[j][i-1],d[j+(1<<(i-1))][i-1]);
    	}
    }	
    while(m--){
    	int a,b;cin>>a>>b;
    	int len=log2(b-a+1);
    	cout<<max(d[a][len],d[b-(1<<len)+1][len])<<endl;
    }
    
    return 0;
}

董晓博客的图解释的比较详细,这里引用

第一个图是预处理,时间复杂度nlogn,第二个图是查询,复杂度是O(1)

遇上大数据的离线处理就只能用ST表

相关推荐

  1. c++算法学习笔记 (21) STL

    2024-04-07 15:38:02       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-07 15:38:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-07 15:38:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-07 15:38:02       87 阅读
  4. Python语言-面向对象

    2024-04-07 15:38:02       96 阅读

热门阅读

  1. 如何设置redis集群

    2024-04-07 15:38:02       36 阅读
  2. 前端面试经验

    2024-04-07 15:38:02       41 阅读
  3. 训练营十四天(二叉树的遍历)

    2024-04-07 15:38:02       145 阅读
  4. 1688详情、搜索、店铺、图搜

    2024-04-07 15:38:02       40 阅读
  5. day19-归并两个有序数组

    2024-04-07 15:38:02       38 阅读
  6. Python基础

    2024-04-07 15:38:02       37 阅读
  7. 加密算法(二)

    2024-04-07 15:38:02       34 阅读
  8. Linux——基础命令

    2024-04-07 15:38:02       41 阅读
  9. 洛谷刷题 快速幂-[P3197]越狱(C++)

    2024-04-07 15:38:02       35 阅读