HDU-1754 I Hate It(线段树)

时间:2022-05-11
本文章向大家介绍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): 55851 Accepted Submission(s): 21792

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

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

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

#include <iostream>
#include <math.h>
#include <stdlib.h>

#include <string.h>

using namespace std;

int c[4000001];
int n,m;
char a;
int x,y;
int max(int x,int y)
{
    return x>y?x:y;
}
void PushUp(int node)
{
    c[node]=max(c[node<<1],c[node<<1|1]);
}
void build(int node,int begin,int end)
{
    if(begin==end)
    {
       scanf("%d",&c[node]);
        return;
    }
    int m=(begin+end)>>1;
    build(node<<1,begin,m);
    build(node<<1|1,m+1,end);
    PushUp(node);
}
void Update(int node,int begin,int end,int ind,int num)
{
    if(begin==end)
    {
        c[node]=num;
        return;
    }
    int m=(begin+end)>>1;
    if(ind<=m)
        Update(node<<1,begin,m,ind,num);
    else
        Update(node<<1|1,m+1,end,ind,num);
    PushUp(node);
}
int Query(int node,int begin,int end,int left,int right)
{
    if(left<=begin&&end<=right)
        return c[node];
    int m=(begin+end)>>1;
    int ret=0;
    if(left<=m)
        ret=max(ret,Query(node<<1,begin,m,left,right));
    if(right>m)
        ret=max(ret,Query(node<<1|1,m+1,end,left,right));
    return ret;

}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
       build(1,1,n);

        for(int i=0;i<m;i++)
        {
            getchar();
          scanf("%c",&a);
          scanf("%d%d",&x,&y);
          if(a=='Q')
          {
            printf("%dn",Query(1,1,n,x,y));
          }
          else
          {
            Update(1,1,n,x,y);
          }
        }

    }
    return 0;
}