#include <stdio.h>
#define MAXN 800002
int arr[MAXN];
void build(int left,int right,int root)
{
int mid;
if(left==right)
{
scanf("%d",&arr[root]);
return;
}
mid=(left+right)>>1;
build(left,mid,root<<1);
build(mid+1,right,root<<1|1);
arr[root]=arr[root<<1]>arr[root<<1|1]?arr[root<<1]:arr[root<<1|1];
}
void update(int x,int val,int left,int right,int root)
{
int mid;
if(left==right)
{
arr[root]=val;
return;
}
mid=(left+right)>>1;
if(x<=mid)
update(x,val,left,mid,root<<1);
else
update(x,val,mid+1,right,root<<1|1);
arr[root]=arr[root<<1]>arr[root<<1|1]?arr[root<<1]:arr[root<<1|1];
}
int query(int st,int end,int left,int right,int root)
{
int mid,ret=0,lx=0,rx=0;
if(st<=left&&end>=right)
return arr[root];
mid=(left+right)>>1;
if(st<=mid)
lx=query(st,end,left,mid,root<<1);
if(end>mid)
rx=query(st,end,mid+1,right,root<<1|1);
ret=lx>rx?lx:rx;
return ret;
}
int main()
{
int n,m,x,y;
char cmd[3];
while(scanf("%d %d",&n,&m)!=EOF)
{
build(1,n,1);
while(m--)
{
scanf("%s %d %d",cmd,&x,&y);
if(cmd[0]=='Q')
printf("%d\n",query(x,y,1,n,1));
else
update(x,y,1,n,1);
}
}
return 0;
}
hdu1754 I Hate It (线段树)
最新推荐文章于 2020-07-22 02:49:27 发布