HDU I Hate It(线段树单点更新,最值查找)

24 篇文章 1 订阅
15 篇文章 0 订阅

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

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

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

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

struct node
{
    int left,right,val;
} c[600005];

void build_tree(int l,int r,int root)
{
    c[root].left=l;
    c[root].right=r;
    if(c[root].left==c[root].right)
    {
        scanf("%d",&c[root].val);
        return ;
    }
    int mid=(c[root].left+c[root].right)/2;
    build_tree(l,mid,root*2);
    build_tree(mid+1,r,root*2+1);
    c[root].val=max(c[root*2].val,c[root*2+1].val) ;
}

void search_tree(int l,int r,int root,int &maxn)
{
    if(c[root].left==l&&c[root].right==r)
    {
        maxn=c[root].val;
        return ;
    }
    int mid=(c[root].left+c[root].right)/2;
    if(mid<l)
    {
        search_tree(l,r,root*2+1,maxn);
    }
    else if(mid>=r)
    {
        search_tree(l,r,root*2,maxn);
    }
    else
    {
        int maxn1;
        int mid=(c[root].left+c[root].right)/2;
        search_tree(l,mid,root*2,maxn1);
        search_tree(mid+1,r,root*2+1,maxn);
        maxn=max(maxn,maxn1);
    }
}

void update_tree(int pos,int root,int x)
{
    if(c[root].left==c[root].right&&c[root].right==pos)
    {
        c[root].val=x;
        return ;
    }
    int mid=(c[root].left+c[root].right)/2;
    if(pos>mid)
        update_tree(pos,root*2+1,x);
    else
        update_tree(pos,root*2,x);
    c[root].val=max(c[root*2].val,c[root*2+1].val);
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(c,0,sizeof(c));
        build_tree(1,n,1);
        getchar();
        for(int i=0; i<m; i++)
        {
            char str;
            scanf("%c",&str);
            if(str=='Q')
            {
                int a,b,maxn;
                scanf("%d%d",&a,&b);
                getchar();
                if(a<b)
                    search_tree(a,b,1,maxn);
                else
                    search_tree(b,a,1,maxn);
                printf("%d\n",maxn);
            }
            else if(str=='U')
            {
                int a,b;
                scanf("%d%d",&a,&b);
                getchar();
                update_tree(a,1,b);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值