zoukankan      html  css  js  c++  java
  • ST表(模板)「 查询区间最值 」

    The Water Problem

    HDU - 5443 

    「 第一部分nlogn预处理   第二部分O(1)询问 」

    #include <iostream>
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 1000004;
    int f[maxn][20];
    int a[maxn];
    int n,q;
    void st()
    {
        for(int i = 1; i <= n; i ++) f[i][0] = a[i]; 
        int t = log(n) / log(2) + 1;
        for(int j = 1; j < 20; j ++)
        {
            for(int i = 1; i <= n - (1 << j) + 1; i ++)
            {
                f[i][j] = max(f[i][j-1],f[i + (1 << (j - 1))][j - 1]);
            }
        }
    }
    int query(int x, int y)
    {
        int t = log(abs(y-x + 1))/ log(2);
        int a = f[x][t];
        int b = f[y - (1 << t) + 1][t];
        return max(a,b);
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(int i=1; i<=n; i++) scanf("%d",a+i);
            int l,r;
            st();
            scanf("%d",&q);
            while(q--)
            {
                scanf("%d%d",&l,&r);
                cout<<query(l,r)<<endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    EasyARM-Linux工具
    EasyARM-Linux文件系统
    EasyARM-Linux使用
    公差-PCBA
    novoton-USBDevice使用
    novoton-RTC使用
    novoton-ADC使用
    novoton-I2C使用
    novoton-timer使用
    novoton-usart使用
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139415.html
Copyright © 2011-2022 走看看