zoukankan      html  css  js  c++  java
  • 洛谷P3865 ST表

    传送门啦

    思路:

    $ f[i][j] $ 表示从 $ i $ 开始,包含 $ 1<<j $ 个元素的区间的区间最大值;

    转移方程: $ f[i][j]=max_(f[i][j-1],f[i+(1<<j-1)][j-1] $ ;

    查询 $ (l,r) $ :

    $ p=log_2(r-l+1) $ ;

    $ max(l,r)=max(f[l][p],f[r-(1<<p)+1][p]) $ ;

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath> 
    #define re register
    using namespace std ;
    
    int n , m , a[100005] , l , r ;
    int f[10000010][21] ;
    
    inline int read () {
    	int f = 1 , x = 0 ; 
    	char ch = getchar () ;
    	while(ch > '9' || ch < '0') {if(ch == '-') f = -1 ; ch = getchar () ;}
    	while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0' ; ch = getchar () ;}
    	return x * f ;
    }
    
    inline void st(int x) {
    	for(re int i = 1 ; i <= 21 ; ++ i) 
    		for(re int j = 1 ; j + (1 << i) <= x + 1 ; ++ j)
    			f[j][i] = max(f[j][i - 1] , f[j + (1 << (i -1))][i - 1]) ;
    }
    
    inline int query(int l , int r) {
    	int k = log2(r - l + 1) ;
    	return max(f[l][k] , f[r - (1 << k) + 1][k]) ;
    }
    
    int main () {
    	n = read () ; m = read () ;
    	for(re int i = 1 ; i <= n ; ++ i) {
    		f[i][0] = read () ;
    	}
    	st(n) ;
    	for(re int i = 1 ; i <= m ; ++ i) {
    		l = read () ; r = read () ;
    		printf("%d
    " , query(l , r)) ;
    	}
    	return 0 ;
    }
    
  • 相关阅读:
    扩展性很好的一个分页存储过程
    SQL中列转行
    Server.MapPath() 方法(摘自互联网)
    容易遗忘のSQL
    Linq读取XML
    字节流和字符流
    Java中" "和 ' '
    Spring常用基本注解
    finally和return
    JS 深度clone
  • 原文地址:https://www.cnblogs.com/Stephen-F/p/10415769.html
Copyright © 2011-2022 走看看