线段树(单点更新) HDU 1754 I Hate It

时间:2022-07-14 19:43:50

题目传送门

 /*
线段树基本功能:区间最大值,修改某个值
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1|1 const int MAX_N = + ;
int sum[MAX_N<<];
int a[MAX_N<<]; void pushup(int rt)
{
sum[rt] = sum[rt<<] + sum[rt<<|];
} void maxrt(int rt)
{
sum[rt] = std::max (sum[rt<<], sum[rt<<|]);
} void build(int l, int r, int rt)
{
if (l == r)
{
scanf ("%d", &sum[rt]);
return ;
}
int m = (l + r) >> ;
build (lson);
build (rson);
maxrt (rt);
} void update(int p, int up, int l, int r, int rt)
{
if (l == r)
{
sum[rt] = up;
return ;
}
int m = (l + r) >> ;
if (p <= m) update (p, up, lson);
else update (p, up, rson);
maxrt (rt);
} int query(int ql, int qr, int l, int r, int rt)
{
if (ql <= l && r <= qr)
{
return sum[rt];
}
int m = (l + r) >> ;
int ans = ;
if (ql <= m) ans = std::max(ans, query (ql, qr, lson));
if (qr > m) ans = std::max(ans, query (ql, qr, rson)); return ans;
} int main(void) //HDU 1754 I Hate It
{
//freopen ("inB.txt", "r", stdin);
int n, m, ans, b, c;
char ch[]; while (~scanf ("%d%d", &n, &m))
{
build (, n, );
while (m--)
{
scanf ("%s%d%d", &ch, &b, &c);
if (ch[] == 'Q')
{
ans = query (b, c, , n, );
printf ("%d\n", ans);
}
if (ch[] == 'U')
{
update (b, c, , n, );
}
}
} return ;
}