杭电1754

#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
const int maxn=200000+100;

int a[maxn];
struct Node{
    int data;
    int left,right;
};
Node b[maxn*10];

int max(int x,int y){
	return x>y ? x:y;
}

//建立二叉树
void BuidTree(int root,int first,int last){
    b[root].left=first;
    b[root].right=last;
    if(first==last){
        b[root].data=a[first];
		return;
	}
    int temp=(first+last)/2;
    BuidTree(root*2,first,temp);
    BuidTree(root*2+1,temp+1,last);
    b[root].data=max(b[root*2].data,b[root*2+1].data);
}


//查找节点
int look(int root,int first,int last){
    if(last<b[root].left || first >b[root].right)
        return 0;
    if(first<=b[root].left && last>=b[root].right)
        return b[root].data;
    int p1=look(root*2,first,last);
    int p2=look(root*2+1,first,last);
    return max(p1,p2);
}

//节点的更新
void update(int root ,int i,int x){
    if (i < b[root].left || b[root].right < i)
        return ;
	if(b[root].left == b[root].right){
		b[root].data=x;
		return;
	}
	int t=(b[root].left+b[root].right)/2;
	if(i<=t)
        update(root*2,i,x);
    else
        update(root*2+1,i,x);
    b[root].data=max(b[root*2].data,b[root*2+1].data);
}

int main(){
    int N,M;
    while(scanf("%d%d",&N,&M)!=EOF){
        for(int i=1;i<=N;i++)
            scanf("%d",&a[i]);
        BuidTree(1,1,N);
        while(M--){
            getchar();
            char c;
            int A,B;
            scanf("%c%d%d",&c,&A,&B);
            if(c=='Q')
                printf("%d\n",look(1,A,B));
            else
                update(1,A,B);
        }
    }
    return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值