zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    在JavaScript的数组中进行数组元素查找和替换(JS的indexOf等)
    GNU/Linux Distribution Timeline v12.10
    makefile编写差异
    java快速排序1000万无序数组JVM-Xmx=256M 耗时2s
    Quartz cron表达式
    hdu
    action中实现对批量文件上传的封装
    MyGui笔记(1)建立第一个工程
    Jenkins参数化构建
    最完美的xslt数值函数与字符串函数(转)
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228836.html
Copyright © 2011-2022 走看看