Code Segment Tree Introduction,
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <cmath>5 using namespacestd;6 Const intMaxnode=1<< +;7 Const intmax=1000003;8 structnode{9 intLeft,right;Ten intvalue; One }node[maxnode]; A intFather[max]; - voidBuildtree (intIintLeftintRight ) - { thenode[i].left=Left ; -node[i].right=Right ; -Node[i].value=0; - if(left==Right ) { +father[left]=i; - return; + } ABuildtree (i<<1, left, (int) Floor ((right+left)/2.0)); atBuildtree ((i<<1) +1, (int) Floor ((right+left)/2.0) +1, right); - - } - voidUpdatatree (intRI) - { - if(ri==1)return; in intfi=ri/2; - inta=node[fi<<1].value; to intb=node[(fi<<1)+1].value; +Node[fi].value=a>b?a:b; -Updatatree (ri/2); the } * intMAXN; $ voidQueryintIintLintR)Panax Notoginseng { - if(node[i].left==l&&node[i].right==R) { theMaxn= (maxn<node[i].value)?NODE[I].VALUE:MAXN; + return; A } thei=i<<1; + if(l<=node[i].right) - if(r<=node[i].right) $ query (i,l,r); $ Else - query (i,l,node[i].right); -i+=1; the if(r>=node[i].left) - if(l>=node[i].left)Wuyi query (i,l,r); the Else - query (i,node[i].left,r); Wu } - intMain () About { $ intn,m; - while(~SCANF ("%d%d",&n,&m)) { -Buildtree (1,1, n); - intgrade; A for(intI=1; i<=n;++i) { +scanf"%d",&grade); theNode[father[i]].value=grade; - Updatatree (Father[i]); $ } the while(m--){ the intx, y;Charstr[5]; thescanf"%s%d%d",str,&x,&y); the if(str[0]=='U'){ -Node[father[x]].value=y; in Updatatree (father[x]); the } the Else{ Aboutmaxn=0; theQuery1, x, y); theprintf"%d\n", MAXN); the } + } - } the}
HDU 1754 Segment Tree basic operations, achievements, updates, queries