5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
5 6 5 9HintHuge input,the C function scanf() will work better than cin
#include<cstdio> #include<algorithm> using namespace std; #define lson l, mid, root<<1 #define rson mid+1, r, root<<1|1 const int N = 2000000 + 50; struct node { int l, r, mmax; }a[4*N]; void build_tree(int l, int r, int root) { a[root].l = l; a[root].r = r; if(l == r) { scanf("%d",&a[root].mmax); return ; } int mid = (l + r) >> 1; build_tree(lson); build_tree(rson); a[root].mmax = max(a[root<<1].mmax, a[root<<1|1].mmax); } void update(int l, int r, int root, int k) { if(l == a[root].l && r == a[root].r) { a[root].mmax = k; return; } int mid = (a[root].l + a[root].r) >> 1; if(r <= mid) update(l, r, root<<1, k); else if(l > mid) update(l, r, root<<1|1, k); else { update(lson, k); update(rson, k); } a[root].mmax = max(a[root<<1].mmax, a[root<<1|1].mmax); //更新完单个点之后更新整棵树 } int Query(int l, int r, int root) { if(l == a[root].l && r == a[root].r) return a[root].mmax; int mid = (a[root].l + a[root].r) >> 1; if(r <= mid) return Query(l, r, root<<1); else if(l > mid) return Query(l, r, root<<1|1); else return max(Query(lson), Query(rson)); } int main() { int n, m, a, b; char ch[5]; while(~scanf("%d%d",&n,&m)) { build_tree(1, n, 1); while(m--) { getchar(); scanf("%s%d%d",ch, &a, &b); if(ch[0] == 'Q') printf("%d\n",Query(a, b, 1)); else update(a, a, 1, b); } } return 0; }
hdu 1754 I Hate It(线段树 之 单点更新 求最值),布布扣,bubuko.com
hdu 1754 I Hate It(线段树 之 单点更新 求最值)
原文地址:http://blog.csdn.net/lyhvoyage/article/details/38405075