HDU 1166 enemy deployment (tree array)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1166

Analysis: The first time I came into contact with the line segment tree, I read some information online and found that this question can be solved by replacing the line segment tree with a tree array. It seems that it is more convenient to use, as you can see from the tree array, here is a tree array of data that I extracted from the Internet. The tree array is not very complete, but it is enough for beginners. There are basic templatesCode.

This is a Chinese question. The question is very simple. It is a typical tree array and line segment tree question.

Tree array Solution

# Include <iostream> # Include <Cstdio> # Include <Cstring> # Include <Algorithm> # Define Maxn 50005 Using   Namespace  STD;  Int A [maxn], C [maxn];  Int  N;  //  Tree array template  Int Low_bit ( Int  X ){  Return X &(- X );}  Void Modify ( Int N, Int  Value ){  While (N <=N) {C [N] + = Value; n + = Low_bit (n );}}  Int Sum ( Int  N ){  Int Sum = 0  ;  While (N> 0  ) {Sum + = C [N]; n -= Low_bit (n );} Return  SUM ;}  Int Query ( Int I, Int  J ){  Return Sum (j)-sum (I- 1  );}  Int  Main (){  Int  T, x, y; scanf (  "  % D  " ,&T );  Char Order [ 10  ];  For ( Int I = 1 ; I <= T; I ++ ) {Printf (  "  Case % d: \ n  "  , I); scanf (  "  % D  " ,&N); memset (,  0 , Sizeof  (A); memset (C,  0 , Sizeof  (C ));  For ( Int J = 1 ; J <= N; j ++ ) {Scanf (  "  % D  " ,&A [J]); Modify (J, a [J]);}  While (Scanf ( "  % S  " , Order) & strcmp (order, "  End  " )! = 0  ) {Scanf (  "  % D  " , & X ,& Y );  If (Strcmp (order, "  Query  " ) = 0  ) Printf (  "  % D \ n  "  , Query (x, y ));  Else   If (Strcmp (order, "  Add  " ) = 0 ) Modify (x, y );  Else   If (Strcmp (order, "  Sub  " ) = 0  ) Modify (X, - Y );}}  Return   0  ;}

Line Segment tree solution:
# Include <iostream> # Include <Cstdio> # Include <Cstring> # Include <Algorithm> # Define Maxn 50005 Using   Namespace  STD; typedef  Struct  Node {  Int LD, RD; //  Left and right boundary      Struct Node * LC, * RC; //  Left and right subtree      Int Value;} node, tree;  Int  Num [maxn];  Int  N; tree * Create ( Int A, Int  B ){  Int  Mid; Node * Node = (node *) malloc ( Sizeof  (Node); Node -> LD = A; Node -> RD = B; If (B-A> = 1  ) {Mid = (A + B )/ 2  ; Node -> Lc = Create (A, mid); Node -> Rc = create (Mid + 1  , B); Node -> Value = node-> LC-> value + node-> RC-> Value ;}  Else Node-> value = num [node-> ld]; //  You can also write node-> value = A [node-> RD];     Return  Node ;}  Int Query (node * node, Int X, Int  Y ){  If (Node-> LD = x & node-> RD = y) Return Node-> Value;  Int Mid = (node-> LD + node-> rd )/ 2  ;  If (Mid> = y) Return Query (node-> LC, x, y );  Else   If (Mid <X) Return Query (node-> RC, x, y );  Else   Return Query (node-> LC, X, mid) + query (node-> RC, Mid + 1  , Y );}  Void Modify (node * node, Int X, Int  V ){  If (Node-> LD = node-> Rd) {Node -> Value + = V;  Return  ;}  If (Node-> LC-> RD> = x) Modify (node-> LC, X, V );  Else   If (Node-> RC-> LD <= x) Modify (node-> RC, X, V); Node -> Value = node-> LC-> value + node-> RC-> Value ;}  Int  Main (){ Int  T, x, y, value; Node * Root; scanf (  "  % D  " ,& T );  Char Order [ 10  ];  For ( Int I = 1 ; I <= T; I ++ ) {Printf (  " Case % d: \ n  "  , I); scanf (  "  % D  " ,& N );  For ( Int J = 1 ; J <= N; j ++ ) Scanf (  "  % D  " ,& Num [J]); root = Create (1  , N );  While (Scanf ( "  % S  " , Order) & strcmp (order, "  End  " )! = 0  ) {Scanf (  "  % D  " , & X ,& Y ); If (Strcmp (order, "  Query  " ) = 0  ) Printf (  "  % D \ n  "  , Query (root, x, y ));  Else   If (Strcmp (order, "  Add  " ) = 0 ) Modify (root, x, y );  Else   If (Strcmp (order, "  Sub  " ) = 0  ) Modify (root, X, - Y );}}  Return   0  ;} 
 
 

 

 
 

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.