Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
int left,right,val;
} c[600005];
void build_tree(int l,int r,int root)
{
c[root].left=l;
c[root].right=r;
if(c[root].left==c[root].right)
{
scanf("%d",&c[root].val);
return ;
}
int mid=(c[root].left+c[root].right)/2;
build_tree(l,mid,root*2);
build_tree(mid+1,r,root*2+1);
c[root].val=max(c[root*2].val,c[root*2+1].val) ;
}
void search_tree(int l,int r,int root,int &maxn)
{
if(c[root].left==l&&c[root].right==r)
{
maxn=c[root].val;
return ;
}
int mid=(c[root].left+c[root].right)/2;
if(mid<l)
{
search_tree(l,r,root*2+1,maxn);
}
else if(mid>=r)
{
search_tree(l,r,root*2,maxn);
}
else
{
int maxn1;
int mid=(c[root].left+c[root].right)/2;
search_tree(l,mid,root*2,maxn1);
search_tree(mid+1,r,root*2+1,maxn);
maxn=max(maxn,maxn1);
}
}
void update_tree(int pos,int root,int x)
{
if(c[root].left==c[root].right&&c[root].right==pos)
{
c[root].val=x;
return ;
}
int mid=(c[root].left+c[root].right)/2;
if(pos>mid)
update_tree(pos,root*2+1,x);
else
update_tree(pos,root*2,x);
c[root].val=max(c[root*2].val,c[root*2+1].val);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(c,0,sizeof(c));
build_tree(1,n,1);
getchar();
for(int i=0; i<m; i++)
{
char str;
scanf("%c",&str);
if(str=='Q')
{
int a,b,maxn;
scanf("%d%d",&a,&b);
getchar();
if(a<b)
search_tree(a,b,1,maxn);
else
search_tree(b,a,1,maxn);
printf("%d\n",maxn);
}
else if(str=='U')
{
int a,b;
scanf("%d%d",&a,&b);
getchar();
update_tree(a,1,b);
}
}
}
return 0;
}