【模板】st表

來源:互聯網
上載者:User

標籤:names   pac   --   main   clu   mes   color   const   size   

吐個槽,我現線上段樹敲得賊熟練,但是樹狀數組和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\n",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;}

 

【模板】st表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.