线段树模板题,复健。
题目
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=200005;
struct Tree{
int l,r,w;
}t[maxn<<2];
void update(int now)
{
t[now].w=max(max(t[now<<1].w,t[now<<1|1].w),t[now].w);
}
void build(int now,int l,int r)
{
t[now].l=l;
t[now].r=r;
t[now].w=0;
if(l==r)
{
scanf("%d",&t[now].w);
return;
}
int mid=(t[now].l+t[now].r)>>1;
build(now<<1,l,mid);
build(now<<1|1,mid+1,r);
update(now);
}
void add(int now,int pos,int k)
{
if(t[now].l==t[now].r)
{
t[now].w=k;
return ;
}
int mid=(t[now].l+t[now].r)>>1;
if(pos<=mid) add(now<<1,pos,k);
else add(now<<1|1,pos,k);
update(now);
}
int query(int now,int L,int R)
{
if(L<=t[now].l&&t[now].r<=R)
{
return t[now].w;
}
int mid=(t[now].l+t[now].r)>>1;
int ans=-INF;
if(L<=mid) ans=max(ans,query(now<<1,L,R));
if(mid<R) ans=max(ans,query(now<<1|1,L,R));
return ans;
}
int main(void)
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,1,n);
for(int i=0;i<m;i++)
{
getchar();
char op;
int a,b;
scanf("%c%d%d",&op,&a,&b);
if(op=='Q')
{
printf("%d\n",query(1,a,b));
}else{
add(1,a,b);
}
}
memset(t,0,sizeof(t));
}
return 0;
}