Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0
Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input
5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5
Sample Output
5
6
5
9
代码实现
#include<bits/stdc++.h>
using namespace std;
#define maxn 200007
struct node
{
int left;
int right;
int value;
}tree[maxn<<2];
int num[maxn];
int create(int root ,int l,int r)
{
tree[root].left=l;
tree[root].right=r;
if(l==r)
return tree[root].value=num[l];
int mid=(l+r)/2;
int a=create(root*2,l,mid);
int b=create(root*2+1,mid+1,r);
return tree[root].value=max(a,b);
}
int update(int root,int pos,int val)
{
if(pos<tree[root].left||tree[root].right<pos)
return tree[root].value;
if(tree[root].left==tree[root].right&&tree[root].left==pos)
return tree[root].value=val;
int a=update(root*2,pos,val);
int b=update(root*2+1,pos,val);
return tree[root].value=max(a,b);
}
int query(int root,int l,int r)
{
if(tree[root].left>r||tree[root].right<l)
return 0;
if(l<=tree[root].left&&tree[root].right<=r)
return tree[root].value;
int a=query(root*2,l,r);
int b=query(root*2+1,l,r);
return max(a,b);
}
int main()
{
int n,m,a,b;
char ch;
while(~scanf("%d %d",&n,&m))
{
for(int i=1;i<=n;i++)
scanf("%d%*c",&num[i]);
create(1,1,n);
for(int i=0;i<m;i++)
{
scanf("%c %d %d%*c",&ch,&a,&b);
if(ch=='Q')
printf("%d\n",query(1,a,b));
else
{
num[a]=b;
update(1,a,b);
}
}
}
return 0;
}