7_22_N题 I Hate It
题意
给出一些学生的成绩,老师会询问,某一段学生中最高的成绩,或修改某同学的成绩。
思路
求区间最大值和单点更新,线段树
#
#include <bits/stdc++.h>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn = 2e5+10;
using namespace std;
char str[10];
/*------------------------*/
int Tree[maxn*3];
//更新父节点
inline void pushPlus(int rt){
Tree[rt] = max(Tree[rt <<1], Tree[rt<<1|1]);
}
//建树
void buildT(int l,int r,int rt){
if(l == r){
scanf("%d",Tree+rt);
return;
}
int mid = (l+r)>>1;
buildT(lson);
buildT(rson);
pushPlus(rt);
}
int que(int NL,int NR,int l,int r,int rt){
if(NL <= l && r <= NR)
return Tree[rt];
int mid = (l+r)>>1;
int ret = 0;
if(NL <= mid)
ret = max(ret, que(NL,NR,lson));
if(NR > mid)
ret = max(ret,que(NL,NR,rson));
return ret;
}
void update(int p,int c,int l,int r,int rt){
if(l == r){
Tree[rt] = c;
return ;
}
int mid= (l+r) >> 1;
if(p <= mid)
update(p,c,lson);
else
update(p,c,rson);
pushPlus(rt);
}
/*--------------------------------*/
int main(){
int T,n,a,b;
while(~scanf("%d %d",&n,&T)){
buildT(1,n,1);
while(T --){
scanf("%s",str);
scanf("%d %d", &a, &b);
if(str[0] == 'U')
update(a,b,1,n,1);
else if(str[0] == 'Q')
printf("%d\n",que(a,b,1,n,1));
}
}
return 0;
}