基本通上一篇
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
const int maxn=200010;
const int INF=1000000000;
int A[maxn],n,m;
struct IntevalTree
{
int sum[maxn<<3];
void maintain(int o)
{
sum[o]=max(sum[o*2],sum[o*2+1]);
}
void build(int o,int l,int r)
{
if(l==r){sum[o]=A[l];return;}
int mid=(l+r)/2;
build(o*2,l,mid);
build(o*2+1,mid+1,r);
maintain(o);
}
void update(int o,int l,int r,int x,int v)
{
if(l==r){sum[o]=v;return;}
int mid=(l+r)/2;
if(x<=mid)update(o*2,l,mid,x,v);
else update(o*2+1,mid+1,r,x,v);
maintain(o);
}
int query(int o,int l,int r,int a,int b)
{
int ans=-1;
if(a<=l&&r<=b)
{
return sum[o];
}
int mid=(l+r)/2;
if(a<=mid)ans=max(ans,query(o*2,l,mid,a,b));
if(b>mid)ans=max(ans,query(o*2+1,mid+1,r,a,b));
return ans;
}
}tree;
int main()
{
//freopen("in.txt","r",stdin);
char x[15];
int a,b;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)scanf("%d",&A[i]);
tree.build(1,1,n);
while(m--)
{
scanf("%s",x);
if(x[0]=='Q')
{
scanf("%d%d",&a,&b);
printf("%d\n",tree.query(1,1,n,a,b));
}
else
{
scanf("%d%d",&a,&b);
tree.update(1,1,n,a,b);
}
}
}
return 0;
}