题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754
题意:单点修改, 求区间最值。
解题思路:很明显,这题树状数组和线段树都能做,这里先用线段树搞吧,树状数组的也不难,只要把update和sum改一下就行了,就不给具体代码了。这题也是基本的线段树的应用,只要把单点修改,区间求和的update和query稍微改一下就行了。详见代码
线段树基础知识详见:线段树之入门篇
AC代码:
#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 222222;
int MAX[maxn<<2];
void PushUP(int rt) { //这个也变成了两者的最大值,而不是求和了
MAX[rt] = max(MAX[rt<<1] , MAX[rt<<1|1]); // rt<<1|1 是 rt*2+1 的意思
}
void build(int l,int r,int rt) {
if (l == r) {
scanf("%d",&MAX[rt]);
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUP(rt);
}
void update(int p,int sc,int l,int r,int rt) {
if (l == r) {
MAX[rt] = sc;
return ;
}
int m = (l + r) >> 1;
if (p <= m) update(p , sc , lson);
else update(p , sc , rson);
PushUP(rt);
}
int query(int L,int R,int l,int r,int rt) {
if (L <= l && r <= R) {
return MAX[rt];
}
int m = (l + r) >> 1;
int ret = 0;
if (L <= m) ret = max(ret , query(L , R , lson)); //这里变成了求两者的最大值
if (R > m) ret = max(ret , query(L , R , rson));
return ret;
}
int main() {
int n , m;
while (~scanf("%d%d",&n,&m)) {
build(1 , n , 1);
while (m --) {
char op[2];
int a , b;
scanf("%s%d%d",op,&a,&b);
if (op[0] == 'Q') printf("%d\n",query(a , b , 1 , n , 1));
else update(a , b , 1 , n , 1);
}
}
return 0;
}
HDU 1754 I Hate It (线段树 & 树状数组)
原文:http://blog.csdn.net/u013446688/article/details/39814525