线段树的一道简单题,详细看代码
#include
#include
#include
using namespace std;
#define MAXN 200010
int n, m;
int a[MAXN];
struct node{
int l, r, max;
}tree[MAXN<<2];
char ch;
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
void build(int k, int l, int r)
{
tree[k].l = l; tree[k].r = r;
if (l == r)
{
tree[k].max = a[l];
return;
}
int mid = (l + r) >> 1;
build(k << 1, l, mid); build(k << 1|1, mid+1, r);
tree[k].max = max(tree[k << 1].max, tree[k << 1 | 1].max);
}
void prework()
{
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
memset(tree, 0, sizeof(tree));
build(1, 1, n);
}
void change(int k, int d, int x)
{
if (tree[k].l == tree[k].r&&tree[k].r == d)
{
tree[k].max = x;
return;
}
int mid = (tree[k].l + tree[k].r) >> 1;
if (d >= tree[k].l&&d <= mid)
change(k << 1, d, x);
else
change(k << 1 | 1, d, x);
tree[k].max = max(tree[k << 1].max, tree[k << 1 | 1].max);
}
int query(int k, int l, int r)
{
int maxnum;
if (tree[k].l == l&&tree[k].r == r)
return tree[k].max;
int mid = (tree[k].l + tree[k].r) >> 1;
if (r <= mid)
maxnum = query(k << 1, l, r);
else if (l >= mid + 1)
maxnum = query(k << 1 | 1, l, r);
else
maxnum = max(query(k << 1, l, mid), query(k << 1 | 1, mid+1, r));
return maxnum;
}
void mainwork()
{
int d, x;
for (int i = 1; i <= m; i++)
{
cin>>ch>>d>>x;
if (ch == 'Q')
printf("%d\n", query(1, d, x));
else
change(1, d, x);
}
}
int main()
{
while (~scanf("%d%d", &n, &m))
{
prework();
mainwork();
}
return 0;
}