• 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("%d
    ",Query(1,1,n,x,y));
              }
              else
              {
                Update(1,1,n,x,y);
              }
            }
    
        }
        return 0;
    }
  • 相关阅读:
    C# 桌面软件开发之超精简WinForm无边框方案(可靠边自动分屏)
    C++ Primer Plus学习笔记之开始学习C++
    C++ Primer Plus学习笔记之复合类型(上)
    C# 创建系统右键菜单按钮关联指定程序(无需管理员权限)
    C# 小工具开源分享之本机IP修改器
    JavaScript库hxsfx.ajax之解决动态加载HTML
    解决npm无法装包的问题
    1.node搭配对应版本的npm
    Node npm升级
    vue3(模版语法&指令)
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228836.html
Copyright © 2020-2023  润新知