思路
线段树的基本应用,区间查询,单点更新
代码
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#define lson tree[root].ls
#define rson tree[root].rs
using namespace std;
const int maxn = 200000;
struct node
{
int l, r;
int ls, rs;
int ans;
}tree[maxn*2+10];
int num[maxn+10];
int nCount = 1;
void build(int root, int l, int r)
{
tree[root].l = l;
tree[root].r = r;
if(tree[root].l==tree[root].r)
{
tree[root].ans = num[l];
}
else
{
lson = ++nCount;
build(lson, l, (l+r)/2);
rson = ++nCount;
build(rson, (l+r)/2+1, r);
tree[root].ans = max(tree[lson].ans, tree[rson].ans);
}
}
void modify(int root, int index, int value)
{
if(tree[root].l == tree[root].r)
{
tree[root].ans = value;
}
else
{
int mid = (tree[root].l+tree[root].r)/2;
if(index<=mid) modify(lson, index, value);
else modify(rson, index, value);
tree[root].ans = max(tree[lson].ans, tree[rson].ans);
}
}
int query(int root, int l, int r)
{
if(tree[root].l==l && tree[root].r==r)
{
return tree[root].ans;
}
else
{
int mid = (tree[root].l+tree[root].r)/2;
if(r<=mid) return query(lson, l, r);
else if(l>=(mid+1)) return query(rson, l, r);
else return max(query(lson, l, mid), query(rson, mid+1, r));
}
}
int main()
{
int n, m, a, b;
char op[5];
while(scanf("%d%d", &n, &m) == 2)
{
for(int i=1; i<=n; i++)
scanf("%d", num+i);
nCount = 1;
build(1, 1, n);
for(int i=0; i<m; i++)
{
scanf("%s%d%d", op, &a, &b);
if(op[0]=='Q') printf("%d\n", query(1, a, b));
else modify(1, a, b);
}
}
return 0;
}