hdu 1754 I Hate It 线段树 单点更新 区间最值

线段树功能:update:单点更新 query:区间最值

#include <bits/stdc++.h>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std; const int MAXN = 200008;
int maxs[MAXN<<2]; void build(int l, int r, int rt)
{
if(l == r)
{
scanf("%d", &maxs[rt]);
return;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
maxs[rt] = max(maxs[rt<<1], maxs[rt<<1|1]);
} int query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R) return maxs[rt];
int ret = 0;
int m = (l + r) >> 1;
if(L <= m) ret = max(ret, query(L, R, lson));
if(R > m) ret = max(ret, query(L, R, rson));
return ret;
} void updata(int p, int news, int l, int r, int rt)
{
if(l == r)
{
maxs[rt] = news;
return;
}
int m = (l + r) >> 1;
if(p <= m) updata(p, news, lson);
else updata(p, news, rson);
maxs[rt] = max(maxs[rt<<1], maxs[rt<<1|1]);
} int main()
{
// freopen("in.txt", "r", stdin);
int n,k;
while(~scanf("%d%d", &n, &k))
{
build(1, n, 1);
while(k--)
{
char s[5];
scanf("%s", s);
int a,b;
scanf("%d%d", &a, &b);
if(s[0] == 'Q') printf("%d\n", query(a, b, 1, n, 1));
else updata(a, b, 1, n, 1);
}
}
return 0;
}
上一篇:ECMAScript 6中数组新方法


下一篇:第五章 二叉树(e1)先序遍历