題目鏈接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66989#problem/B
還以爲是英文題目,點開一看居然是中文題目。給定學生的成績,期間會動態修改某個學生的成績,詢問某個區間的最大分數。
是一個標準的用線段樹動態維護區間最大至問題。
代碼:
#include<iostream>
#include<cstdio>
#include<algorithm>
#define maxn 200010
using namespace std;
struct node{
int l,r,m;//區間的左右端點和該區間的最大值
}T[maxn*3];
int N,M,A[maxn];
int build(int L,int R,int o){
T[o].l=L;T[o].r=R;
if(L==R) return T[o].m=A[L];
return T[o].m=max(build(L,(L+R)/2,o*2),build((L+R)/2+1,R,o*2+1));
}
void update(int k,int val,int o){//更新k的值爲val
if(T[o].l==T[o].r) {T[o].m=val;return;}//k一定在結點o的區間內
T[o].m=max(T[o].m,val);//維護區間的最大值
if(k>=T[o*2+1].l) update(k,val,o*2+1);
else update(k,val,o*2);
}
int query(int L,int R,int o){//返回區間最大值
if(T[o].l>=L && T[o].r<=R) return T[o].m;
if(L>=T[o*2+1].l) return query(L,R,o*2+1);
if(R<=T[o*2].r) return query(L,R,o*2);
return max(query(L,T[o*2].r,o*2),query(T[o*2+1].l,R,o*2+1));
}
int main(){
//freopen("D:\\in.txt","r",stdin);
while(cin>>N){
cin>>M;
for(int i=1;i<=N;i++)
scanf("%d",A+i);
build(1,N,1);
char ch;int a,b;
for(int i=0;i<M;i++){
getchar();
scanf("%c %d %d",&ch,&a,&b);
if(ch=='Q') printf("%d\n",query(a,b,1));
else update(a,b,1);
}
}
return 0;
}