zoukankan      html  css  js  c++  java
  • 【模板】st表

    吐个槽,我现在线段树敲得贼熟练,但是树状数组和st表这么强大的东西竟然基本不会!!!啊啊啊,我太菜了!

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define duke(i,a,n) for(int i = a;i <= n;i++)
    #define lv(i,a,n) for(int i = a;i >= n;i--)
    #define clean(a) memset(a,0,sizeof(a))
    const int INF = 1 << 30;
    const int N = 300005;
    typedef long long ll;
    typedef double db;
    template <class T>
    void read(T &x)
    {
        char c;
        bool op = 0;
        while(c = getchar(), c < '0' || c > '9')
            if(c == '-') op = 1;
        x = c - '0';
        while(c = getchar(), c >= '0' && c <= '9')
            x = x * 10 + c - '0';
        if(op) x = -x;
    }
    template <class T>
    void write(T x)
    {
        if(x < 0) putchar('-'), x = -x;
        if(x >= 10) write(x / 10);
        putchar('0' + x % 10);
    }
    int st[1000005][23],a[1000005],n,m;
    void build()
    {
        duke(i,1,n)
        st[i][0] = a[i];
        duke(i,1,21)
        {
            for(int j = 1;j + (1 << i) - 1 <= n;j++)
            {
                st[j][i] = max(st[j][i - 1],st[j + (1 << (i - 1))][i - 1]);
            }
        }
    }
    void query(int l,int r)
    {
        int y = log2(r - l + 1);
        int maxn = 0;
        maxn = max(st[l][y],st[r - (1 << y) + 1][y]);
        printf("%d
    ",maxn);
    }
    int main()
    {
        read(n);read(m);
        duke(i,1,n)
            read(a[i]);
        build();
        while(m--)
        {
            int l,r;
            read(l);read(r);
            query(l,r);
        }
        return 0;
    }
  • 相关阅读:
    git 常用命令
    PHP打印日志类
    如何从总账获取分类账信息
    AP -> GL 数据流动
    JDeveloper 速度慢问题
    JDeveloper 滚轮不受控制
    MyBatis 环境搭建
    初识 MyBatis
    Linux 中设置 MySQL 字符集为 UTF-8
    Linux 安装 MySQL 详解(rpm 包)
  • 原文地址:https://www.cnblogs.com/DukeLv/p/9745636.html
Copyright © 2011-2022 走看看