[HDU] 1754 I Hate It

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

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0

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

const int MAXNODE = 1<<19 ; 
const int MAX = 2e6+10 ; 

struct NODE{
    int value ; 
    int left ,  right ; 

}node[MAXNODE];

int father[MAX];

void BuildTree(int i , int left , int right){//×ÔÉÏÍùϸüР
    node[i].left = left ; 
    node[i].right = right ; 
    node[i].value = 0 ; 
    if(left == right){
        father[left] = i ; 
        return ; 
    }
    BuildTree(i<<1,left , (int)(floor(left+right)/2.0));
    BuildTree((i<<1)+1 , (int)(floor(left+right)/2.0)+1 , right);
}

void UpdateTree(int ri){ //×ÔÏÂÍùÉϸüР
    if(ri == 1) return ; 
    int fi = ri/2 ; 
    int a = node[fi<<1].value ; 
    int b = node[(fi<<1)+1].value ; 
    node[fi].value = max(a,b) ; 
    UpdateTree(ri/2) ; 
}

int Max ; 

void Query(int i , int l , int r){
    if(node[i].left == l && node[i].right== r){
        Max = max(Max, node[i].value) ; 
        return ; 
    }
    i = i<<1 ; 
    if(l <= node[i].right){
        if( r <= node[i].right) Query(i , l , r ); 
        else Query(i,l,node[i].right) ;
    }
    i ++ ; 
    if(r >= node[i].left){
        if(l >= node[i].left) Query(i , l , r) ; 
        else Query(i,node[i].left,r);
    }
}

int main(){
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    int n, m , g ; 

    while(cin>>n>>m){
        BuildTree(1,1,n);
        for(int i = 1; i <=n ;i ++){
            cin>>g ; 
            node[father[i]].value = g ; 
            UpdateTree(father[i]);
        }
        string op ; 
        int a, b ; 
        while(m--){
            cin>>op>>a>>b ; 
            if(op[0]=='Q'){
                Max = 0 ;
                Query(1,a,b) ; 
                cout<<Max<<endl; 
            }
            else{
                node[father[a]].value = b ; 
                UpdateTree(father[a]);
            }
        }
    }
    return 0 ; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值