HDU_1754_I Hate It

ORZ NOTONLYSUCCESShttp://www.notonlysuccess.com/index.php/segment-tree-complete/

哈哈,再打了很多次线段树模板后,终于裸打成功。虽然有抄袭的嫌疑,但是纪念一下,第一棵线段树

#include<iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<string>

#include<queue>

using namespace std;

#define maxn 200005

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

int MAX[maxn<<2];

void PushUP(int rt)

{

    MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);

}

void build(int l,int r,int rt)

{

    if(l==r)

    {

        scanf("%d",&MAX[rt]);

        return;

    }

    int m=(l+r)>>1;

    build(lson);

    build(rson);

    PushUP(rt);

}

void update(int p,int sc,int l,int r,int rt)

{

    if(l==r)

    {

        MAX[rt]=sc;

        return;

    }

    int m=(l+r)>>1;

    if(p<=m) update(p,sc,lson);

    else update(p,sc,rson);

    PushUP(rt);

}

int query(int L,int R,int l,int r,int rt)

{

    if(L<=l&&r<=R)

    {

        return MAX[rt];

    }

    int m=(l+r)>>1;

    int ret=0;

    if(L<=m) ret=max(ret,query(L,R,lson));

    if(R>m) ret=max(ret,query(L,R,rson));

    return ret;

}

int main()

{

  int n,q,L,R;

  char ch[5];

  while(~scanf("%d%d",&n,&q))

  {

      build(1,n,1);

      while(q--)

      {

       scanf("%s%d%d",ch,&L,&R);

       if(ch[0]=='Q') 

           printf("%d\n",query(L,R,1,n,1));

       else update(L,R,1,n,1);

      }

  }

  return 0;

}

 

 

你可能感兴趣的:(HDU)