HDU I Hate It

來源:互聯網
上載者:User

標籤:style   os   io   for   ar   問題   amp   size   

 題目意思很簡單,很裸的線段樹。

有是一道單點更新問題,是單點更新+區間最大值。


#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;#define L(x) (x<<1)#define R(x) (x<<1|1)#define lson lft,mid,rt << 1#define rson mid+1,rht,rt << 1 | 1#define MID(a,b) (a+((b-a)>>1))#define INF 1<<30const int MAXN = 200005;struct Node{   int lft,rht,mx;   int mid(){return MID(lft,rht);}};Node tree[4*MAXN];int y[MAXN],n,m;class Segtree{public:    void Build(int lft,int rht,int rt){        tree[rt].lft = lft; tree[rt].rht = rht;        tree[rt].mx = -INF;        if(lft == rht)           tree[rt].mx = y[lft];        else{            int mid = tree[rt].mid();            Build(lson);            Build(rson);            tree[rt].mx = max(tree[L(rt)].mx,tree[R(rt)].mx);        }    }    void Update(int pos,int rt,int val){        int lft = tree[rt].lft,rht = tree[rt].rht;        if(lft == rht){            tree[rt].mx = val;        }        else{            int mid = tree[rt].mid();            if(pos <= mid) Update(pos,L(rt),val);            else Update(pos,R(rt),val);            tree[rt].mx = max(tree[L(rt)].mx,tree[R(rt)].mx);        }   }   int Query(int st,int ed,int rt){       int lft = tree[rt].lft,rht = tree[rt].rht;       if(st <= lft&&rht <= ed) return tree[rt].mx;       else{            int mid = tree[rt].mid();            int mx1 = -INF,mx2 = -INF;            if(st <= mid) mx1 = Query(st,ed,L(rt));            if(ed > mid) mx2 = Query(st,ed,R(rt));            return max(mx1,mx2);       }   }};int main(){    while(~scanf("%d%d",&n,&m))    {        Segtree seg;        char op[5];        int a,b;        for(int i = 1;i <= n;++i)            scanf("%d",&y[i]);        seg.Build(1,n,1);        while(m--){            scanf("%s%d%d",op,&a,&b);            if(op[0] == 'Q')               printf("%d\n",seg.Query(a,b,1));            else               seg.Update(a,1,b);        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.