Hdu 1754 I Hate It 线段树 解题报告

Problem Description

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

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

Input

本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0

Output

对于每一次询问操作,在一行里面输出最高成绩。

Sample Input

5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5

Sample Output

5
6
5
9

思路

很裸的线段树,但是好像卡时间很坑,一直TLE。。。又不擅长卡常数。。。
好像ZKW线段树可以吧。。
传说中的ZKW线段树

代码

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=200000+5; 
int m,n,a[N];
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct node
{
    int vmax;
    node *lson,*rson;
};
struct node dizhi[N],*tail=dizhi,*root;
int max(int x,int y)
{
    if (x>=y) return x;
    else return y;
}
int min(int x,int y)
{
    if (x<=y) return x;
    else return y;
}
node *build(int lf,int rt)
{
    node *nd=++tail;
    if (lf==rt) nd->vmax=a[lf];
    else
    {
        int mid=(lf+rt)/2;
        nd->lson=build(lf,mid);
        nd->rson=build(mid+1,rt);
        nd->vmax=max(nd->lson->vmax,nd->rson->vmax);
    }
    return nd;
}
void modify(node *nd,int lf,int rg,int pos,int val )
{
    if (lf==rg) {nd->vmax=val;return ;}
    int mid=(lf+rg)>>1;
    if (pos<=mid) modify(nd->lson,lf,mid,pos,val);
    else modify(nd->rson,mid+1,rg,pos,val);
    nd->vmax=max(nd->lson->vmax,nd->rson->vmax);
}
int query(node *nd,int lf,int rg,int L,int R)
{
    if (L<=lf&&R>=rg) return nd->vmax;
    int mid=(lf+rg)>>1;
    int res=-INF;
    if (L<=mid) res=max(res,query(nd->lson,lf,mid,L,R));
    if (R>mid) res=max(res,query(nd->rson,mid+1,rg,L,R));
    return res;
}
int main()
{
    n=read();m=read();
    for (register int i=1;i<=n;i++)
    a[i]=read();
    root=build(1,n);
    for (int i=1;i<=m;i++)
    {
        char s[100];
        scanf("%s",s);
        if (s[0]=='U')
        {
            int a,b;
            a=read();b=read();
            modify(root,1,n,a,b);
        }
        else
        {
            int a,b;
            a=read();b=read();
            printf("%d\n",query(root,1,n,a,b));
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值