题目:
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
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 ;
}