HDU 1754 Segment Tree basic operations, achievements, updates, queries

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.