HDU 1166 enemy deployment (line segment tree)

Source: Internet
Author: User
Tags tidy

HDU 1166 enemy deployment (line segment tree)
Enemy army deploymentTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 42921 Accepted Submission (s): 18164



Problem DescriptionC's dead enemy country A is conducting military exercises during this time, so Derek and Tidy of country C are busy again. Country A has deployed N barracks along the coastline. Derek and Tidy are responsible for monitoring the activities of these barracks. Some advanced monitoring means have been adopted, so the number of people in each engineer camp is clearly understood by country C. The number of people in each engineer camp may change, and the number of workers may increase or decrease, but none of these can escape the surveillance of country C.
The Central Intelligence Agency needs to study what tactics the enemy actually exercises, so Tidy must report to Derek at any time the total number of people in a continuous barracks. For example, Derek asked: "Tidy, report on the total number of people from 3rd camps to 10th camps!" Tidy is about to calculate the total number of people in this section and report it immediately. However, the number of people in the enemy barracks often changes, and Derek asks for different segments each time. Therefore, Tidy has to go to the camp one by one each time and is exhausted soon, derek is getting less and more dissatisfied with Tidy's computing speed: "You are a fat boy, it's so slow. I'll fry you!" Tidy thought, "you can calculate it yourself. This is really a tiring job! I wish you fired my squid !" In desperation, Tidy had to call Windbreaker, a computer expert, for help. Windbreaker said: "Fat Boy, I want you to do more acm questions and read more algorithm books. Now I have a bitter taste !" Tidy said, "I know the error... "However, Windbreaker is disconnected. Tidy is very upset. In this case, he will actually crash. Smart readers, can you write a program to help him complete the job? However, if your program is not efficient enough, Tidy will still be scolded by Derek.

The first line of Input is an integer T, indicating that T groups of data exist.
The first line of each group of data is a positive integer N (N <= 50000), indicating that the enemy has N barracks, followed by N positive integers, the I positive integer ai represents an ai individual (1 <= ai <= 50) at the beginning of the I barracks ).
Next, each line contains a command in four forms:
(1) Add I j, I and j are positive integers, indicating that j individuals are added to camp I (j cannot exceed 30)
(2) Sub I j, I and j are positive integers, indicating that j individuals are reduced in camp I (j cannot exceed 30 );
(3) Query I j, I and j are positive integers. I <= j indicates the total number of camp I to j;
(4) End indicates the End. This command appears at the End of each group of data;
Each group of data can contain a maximum of 40000 commands.

Output for group I data, first Output "Case I:" And press enter,
For each Query, output an integer and press enter to indicate the total number of the queried segments, which is kept within int.

Sample Input

1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End 

Sample Output
Case 1:63359

AuthorWindbreaker
RecommendEddy | We have carefully selected several similar problems for you: 1394 1698 1754 1542

Apply the template. If you have any misunderstanding, please point out that you are a newbie.
Time and space are both relatively large.
Code:
# Include
 
  
# Include
  
   
# Include
   
    Using namespaceStd
    ;# Define M 50005
    IntA
    [M
    ]; StructNode
    {IntLeft
    ; IntRight
    ; IntCount
    ;}Node
    [4
    *M
    ]; // It is 4 times bigger because there are many nodes after the binary tree is changed. VoidTreeMake
    (IntL
    , IntR
    , IntI
    ) // Create a binary tree. {Node
    [I
    ].Left
    =L
    ; // L is the boundary before the interval.Node
    [I
    ].Right
    =R
    ; // R is the boundary behind the interval. If (L
    =R
    ) // If the Interval Length is only 1, the exact value can be obtained. {Node
    [I
    ].Count
    =A
    [L
    ]; Return;} intM
    = (L
    +R
    )/2
    ; // Otherwise, you need to divide the interval.TreeMake
    (L
    ,M
    ,2
    *I
    );TreeMake
    (M
    +1
    ,R
    ,2
    *I
    +1
    );Node
    [I
    ].Count
    =Node
    [2
    *I
    ].Count
    +Node
    [2
    *I
    +1
    ].Count
    ; // Obtain the sum of the range .} VoidTreeUpdate
    (IntI
    , IntX
    , IntOp
    , IntNum
    ) // Update. {IntL
    =Node
    [I
    ].Left
    ; IntR
    =Node
    [I
    ].Right
    ; IntMid
    = (L
    +R
    )/2
    ; If (X
    =L
    &&X
    =R
    ) // Locate the node to be updated and update it. {If (Op
    =1
    )Node
    [I
    ].Count
    + =Num
    ; ElseNode
    [I
    ].Count
    -=Num
    ; Return;} if (X
    >Mid
    ) // Determine whether the vertex to be updated is in the left or right subtree.TreeUpdate
    (2
    *I
    +1
    ,X
    ,Op
    ,Num
    ); // Enter the left subtree. ElseTreeUpdate
    (2
    *I
    ,X
    ,Op
    ,Num
    ); // Enter the right subtree. If (Op
    =1
    )Node
    [I
    ].Count
    + =Num
    ; // Here, the value is updated for each large interval containing the node. ElseNode
    [I
    ].Count
    -=Num
    ;} IntTreeQuery
    (IntL
    , IntR
    , IntI
    ) // Find the range of the output value. {IntM
    = (Node
    [I
    ].Left
    +Node
    [I
    ].Right
    )/2
    ; If (Node
    [I
    ].Left
    =L
    &&Node
    [I
    ].Right
    =R
    ) ReturnNode
    [I
    ].Count
    ;// If (node [I]. left = node [I]. right) return 0; // you can use this sentence without knowing what it is. If you know what it is, please let us know.
    If (R
    <=M
    ) // If the backend is smaller than the median value, the entire interval is within the left subtree. ReturnTreeQuery
    (L
    ,R
    ,I
    *2
    ); Else if (L
    >M
    ) // Similarly, if the front boundary is greater than the middle value, the entire interval is within the right subtree. ReturnTreeQuery
    (L
    ,R
    ,2
    *I
    +1
    ); Else // If the interval is separated by the median, it is obtained separately and then added. ReturnTreeQuery
    (L
    ,M
    ,2
    *I
    ) +TreeQuery
    (M
    +1
    ,R
    ,2
    *I
    +1
    );} Int main () {intM
    ,I
    ,J
    ,N
    ,Tmp
    ; CharCmd
    [10
    ]; IntX
    ,Y
    ;Scanf
    ("% D"
    ,&M
    ); (I
    =1
    ;I
    <=M
    ;I
    ++ ){Printf
    ("Case % d: \ n"
    ,I
    );Scanf
    ("% D"
    ,&N
    ); (J
    =1
    ;J
    <=N
    ;J
    ++ ){Scanf
    ("% D"
    ,&A
    [J
    ]);}TreeMake
    (1
    ,N
    ,1
    ); While (Scanf
    ("% S"
    ,Cmd
    ) {If (Cmd
    [0
    ] ='E'
    ) Break;Scanf
    ("% D"
    ,&X
    ,&Y
    ); If (Cmd
    [0
    ] ='Q'
    ){Tmp
    =TreeQuery
    (X
    ,Y
    ,1
    );Printf
    ("% D \ n"
    ,Tmp
    );} Else if (Cmd
    [0
    ] ='A'
    ){TreeUpdate
    (1
    ,X
    ,1
    ,Y
    );} Else {TreeUpdate
    (1
    ,X
    ,2
    ,Y
    ) ;}} Return0
    ;}
   
  
 

Related Article

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.