解题思路:标准的线段树单点修改入门题~
AC代码:
#include <iostream>
#include <cstdio>
using namespace std;
#define maxn 200005
//
int score[maxn];
char ch;
struct NODE
{
int l,r,val;
} node[4*maxn];
int build(int root, int l, int r)
{
node[root].l = l;
node[root].r = r;
if(l==r)
return node[root].val = score[l];
int mid = (l+r)>>1;
int a = build(root<<1, l, mid);
int b = build(root<<1 | 1 ,mid+1, r);
return node[root].val = max(a,b);
}
int update(int root, int pos, int val)
{
if (pos < node[root].l || pos > node[root].r)
return node[root].val;
if (node[root].l == pos && node[root].r == pos)
return node[root].val = val;
int a = update (root<<1, pos, val);
int b = update (root<<1 | 1 , pos, val);
node[root].val = max(a,b);
return node[root].val;
}
int query(int root, int l, int r)
{
if(l>node[root].r || r<node[root].l)//无交集
return 0;
if(l<=node[root].l && node[root].r<=r)//此区间包含root所管理的区间
return node[root].val;
int a = query(root<<1, l, r);//部分相交
int b = query(root<<1 | 1 ,l, r);
return max(a,b);
}
int main()
{
int n,m,i;
while(~scanf("%d%d",&n,&m))
{
for(i=1; i<=n; i++)
{
scanf("%d",&score[i]);
}
build(1,1,n);
int a,b;
for(i=1; i<=m; i++)
{
getchar();
scanf("%c%d%d",&ch,&a,&b);
if(ch=='U')
{
score[a] = b;
update(1,a,b);
}
else
{
printf("%d\n",query(1,a,b));
}
}
}
//cout << "Hello world!" << endl;
return 0;
}