题意:不解释
难点:对于初学线段树的同学来说,还不是太懂线段树,不懂得怎么变换,其实这道题只需要小小的改动一下就可以了,就是将原来的敌兵布阵http://blog.csdn.net/shengweisong/article/details/38276519 的代码改动一下就好了
AC by SWS
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1754
代码:
#include<algorithm> using namespace std; #include<stdio.h> #include<string.h> #define LC l, m, rt<<1 #define RC m+1, r, rt<<1|1 #define maxN 200000+10 int sum[maxN<<2]; void creat(int l, int r, int rt) { if(l == r){ scanf("%d", &sum[rt]); return; } int m = (l+r)>>1; creat(LC); creat(RC); sum[rt] = max(sum[rt<<1], sum[rt<<1|1]); } void update(int p, int num, int l, int r, int rt) { if(l == r){ sum[rt] = num; return ; } int m = (l+r)>>1; if(p<=m) update(p, num, LC); else update(p, num, RC); sum[rt] = max(sum[rt<<1], sum[rt<<1|1]); } int que(int le, int ri, int l, int r, int rt) { if(le <= l&&r<= ri){ return sum[rt]; } int res = 0; int m = (r+l)>>1; if(le <= m) res = max(res, que(le, ri, LC)); //这处改一下 if(ri > m) res = max(res, que(le, ri, RC)); //这处 return res; } int main(){ int n, m, a, b, i, j; while(scanf("%d%d", &n, &m) != EOF){ memset(sum, 0, sizeof(sum)); creat(1, n, 1); char c[10]; while(m --){ scanf("%s %d%d", c, &a, &b); if(c[0] == 'Q') printf("%d\n", que(a, b, 1, n, 1)); else update(a, b, 1, n, 1); } } return 0; }
hdoj 1754 I Hate It 【线段树】,布布扣,bubuko.com
原文地址:http://blog.csdn.net/shengweisong/article/details/38337781