很水的一道线段树,就不多说了
题意:
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
就是普通的模板线段树,很简单,只需要单点更新就行。
#include<cstdio>
#include<iostream>
using namespace std;
int a[200000<<2];
void build(int l,int r,int root){
if(l==r){
scanf("%d",&a[root]);
return;
}
int mid=(l+r)>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
a[root]=max(a[root<<1],a[root<<1|1]);
}
void update(int l,int r,int root,int p,int data){
if(l==r){
a[root]=data;
return;
}
int mid=(l+r)>>1;
if(p<=mid)update(l,mid,root<<1,p,data);
else update(mid+1,r,root<<1|1,p,data);
a[root]=max(a[root<<1],a[root<<1|1]);
}
int query(int l,int r,int root,int L,int R){
if(L<=l&&r<=R){
return a[root];
}
//printf("%d %d\n",l,r);
int mid=(l+r)>>1;
int res=0;
if(L<=mid)
res=max(res,query(l,mid,root<<1,L,R));
if(R>mid)
res=max(res,query(mid+1,r,root<<1|1,L,R));
return res;
}
int main(){
int n,m,l,r;
char c[2];
while(scanf("%d%d",&n,&m)!=EOF){
build(1,n,1);
while(m--){
scanf("%s%d%d",c,&l,&r);
if(c[0]=='Q'){
printf("%d\n",query(1,n,1,l,r));
}
else{
update(1,n,1,l,r);
}
}
}
return 0;
}