HDU 1754 I Hate It 线段树

                                     I Hate It

                     Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                               Total Submission(s): 97241    Accepted Submission(s): 36717

Problem Description

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

 

简而言之:

       需要一个数据结构,能够查询区间最大值  && 单点更新

                                                                                                                             这便是神奇的——线段树(也不需要lazy)

 

PS:据说这个题HDU上数据水,然而 用#define max(a,b)  会TLE    [  还有别的 #define 的非 常规操作 也会TLE ]

         不过#define LL long long 没有影响,这是为什么?  

         如果你也 TLE 了,而且不是写法本身的问题的话,那就写个max函数  或者  用int存最终答案的可以直接调用< algorithm >中的max,记得加上  using namespace std; 

 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define LL long long

using namespace std;

inline void wprint (LL x){
    if (x>9)    wprint(x/(LL)10);
    putchar(x%10+'0');
} 

inline void print (LL x){
    if (x<0)    x=-x,putchar ('-');
    wprint(x);
    putchar('\n');
}

inline int wread(){
    char c(getchar());int wans(0),flag(1);
    while (c<'0'||c>'9'){if (c=='-') flag=-1;c=getchar();}
    while (c>='0'&&c<='9'){wans=(wans<<3)+(wans<<1)+(c-48);c=getchar();}
    return wans*=flag;
}

inline LL ll_wread(){
    char c(getchar());LL wans((LL)0),flag((LL)1);
    while (c<'0'||c>'9'){if (c=='-') flag=-1;c=getchar();}
    while (c>='0'&&c<='9'){wans=(wans<<3)+(wans<<1)+(c-48);c=getchar();}
    return wans*=flag;    
}

inline LL max(LL a,LL b){
    return a>b?a:b;
}

struct node{int l,r;LL ans;}e[800000];
int n,m;

void make_tre(int a,int l,int r){
    e[a].l=l; e[a].r=r; e[a].ans=0;
    if (l==r)    {e[a].ans=ll_wread(); return ;}
    int mid(l+r>>1),lson(a<<1),rson(a<<1|1);
    make_tre(lson,l,mid); make_tre(rson,mid+1,r);
    e[a].ans=max(e[lson].ans,e[rson].ans);
}

void chan_dian (int a,int pos,LL k){
    if (e[a].l==pos && e[a].r==pos){e[a].ans=k;return ;}
    int mid(e[a].l+e[a].r>>1),lson(a<<1),rson(a<<1|1);
    if (pos<=mid)    chan_dian(lson,pos,k);
    else if (pos>mid)    chan_dian(rson,pos,k);
    e[a].ans=max(e[lson].ans,e[rson].ans);
}

LL fnd_max(int a,int l,int r){
    if (e[a].l>=l && e[a].r<=r){return e[a].ans;}
    int mid(e[a].l+e[a].r>>1),lson(a<<1),rson(a<<1|1);
    if (r<=mid)    return fnd_max(lson,l,r);
    else if (l>mid)    return fnd_max(rson,l,r);
    else return max(fnd_max(lson,l,r),fnd_max(rson,l,r));
}
char s;
int main(){
    while (~scanf ("%d%d",&n,&m)){
        make_tre(1,1,n);
        while (m--){
            scanf ("%c",&s);
            if (s=='Q'){
                int x=wread(),y=wread();
                print(fnd_max(1,x,y));
            }
            else{
                int pos(wread());LL k(ll_wread());
                chan_dian(1,pos,k);
            }
        }        
    }  
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值