简单的线段树题目,单点更新,求区间最大值。
#include<iostream>
using namespace std;
int n,m;
int ans;
struct Node
{
int l,r;
int themax;
}node[200005*3];
int map[200005];
void create(int p,int l,int r)
{
node[p].l=l;
node[p].r=r;
node[p].themax=0;
if(l==r)
{
node[p].themax=map[l];
return;
}
else
{
int mid=(l+r)>>1;
create(p*2,l,mid);
create(p*2+1,mid+1,r);
node[p].themax=node[p*2].themax>node[p*2+1].themax?node[p*2].themax:node[p*2+1].themax;
}
}
void update(int p)
{
int l=1,r=n,mid;
int pos=1;
while(l<r)
{
mid=(l+r)>>1;
if(p<=mid)
{
r=mid;
pos=pos*2;
}
else
{
l=mid+1;
pos=pos*2+1;
}
}
node[pos].themax=map[p];
while(pos>1)
{
pos=pos/2;
node[pos].themax=node[pos*2].themax>node[pos*2+1].themax?node[pos*2].themax:node[pos*2+1].themax;
}
}
void getans(int p,int l,int r)
{
if(node[p].l>=l&&node[p].r<=r)
{
if(node[p].themax>ans)
ans=node[p].themax;
return;
}
else if(node[p].r<l||node[p].l>r)
return;
else
{
getans(p*2,l,r);
getans(p*2+1,l,r);
}
}
int main()
{
int i,x,y;
char a[3];
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d",&map[i]);
create(1,1,n);
for(i=1;i<=m;i++)
{
scanf("%s",a);
scanf("%d%d",&x,&y);
if(a[0]=='U')
{ map[x]=y;
update(x);
}
if(a[0]=='Q')
{
ans=0;
getans(1,x,y);
printf("%d/n",ans);
}
}
}
return 0;
}