Http://acm.hdu.edu.cn/showproblem.php? PID = 1779 line segment tree for Interval Maximum node update

Source: Internet
Author: User

 

# Include <iostream> <br/> # include <cstdio> <br/> # define LL (x) <1) <br/> # define RR (x) <1 | 1) <br/> using namespace STD; <br/> const int n = 100005; <br/> struct seg_tree {<br/> int L, R; <br/> int val; // the cumulative value of this range <br/> int maxval; // record the maximum value of the subinterval <br/> int loction; // the maximum position of the subinterval <br/> int mid () {<br/> return (L + r)> 1; <br/>}< br/>} tree [4 * n]; <br/> bool readint (Int & RET) {<br/> int SGN; <br/> char C; <br/> C = getchar (); <br/> If (C = EOF) <br/> return true; <br/> while (C! = '-' & C <'0' | C> '9') <br/> C = getchar (); <br/> SGN = (C = '-')? -1: 1; <br/> ret = (C = '-')? 0: (C-'0'); <br/> while (C = getchar ()> = '0' & C <= '9 ') <br/> ret = RET * 10 + (c-'0'); <br/> RET * = SGN; <br/> return false; <br/>}< br/> void build (INT node, int L, int R) {<br/> tree [node]. L = L; <br/> tree [node]. R = r; <br/> tree [node]. val = 0; <br/> tree [node]. maxval = 0; <br/> tree [node]. loction = L; <br/> If (L = r) {<br/> return; <br/>}< br/> int mid = (L + r)> 1; <br/> build (LL (node), L, Mid); <br/> build (RR (node), Mid + 1, R); <br/>}< br/> int Update (INT node, int l, int R, int Val) {<br/> If (tree [node]. L = L & tree [node]. R = r) {<br/> tree [node]. val + = val; <br/> return tree [node]. maxval + tree [node]. val; <br/>}< br/> int mid = tree [node]. mid (); <br/> If (L <= mid) {<br/> If (r <= mid) {<br/> int temp = Update (LL (node), L, R, Val); <br/> If (temp> = tree [RR (node)]. maxval + tree [rr (Node)]. val) {<br/> tree [node]. maxval = temp; <br/> tree [node]. loction = tree [LL (node)]. loction; <br/>}else {<br/> tree [node]. maxval = tree [RR (node)]. maxval + tree [RR (node)]. val; <br/> tree [node]. loction = tree [RR (node)]. loction; <br/>}< br/>} else {<br/> tree [node]. maxval = Update (LL (node), L, mid, Val); <br/> tree [node]. loction = tree [LL (node)]. loction; <br/> int temp = Update (RR (node), Mid + 1, R, Val); <br/> If (temp> tree [node]. maxval) {<br/> tree [node]. maxval = temp; <br/> tree [node]. loction = tree [RR (node)]. loction; <br/>}< br/>} else {<br/> int temp = Update (RR (node), L, R, Val ); <br/> If (temp> tree [LL (node)]. val + tree [LL (node)]. maxval) {<br/> tree [node]. maxval = temp; <br/> tree [node]. loction = tree [RR (node)]. loction; <br/>}else {<br/> tree [node]. maxval = tree [LL (node)]. val + Tree [LL (node)]. maxval; <br/> tree [node]. loction = tree [LL (node)]. loction; <br/>}< br/> return tree [node]. maxval + tree [node]. val; <br/>}< br/> int query (INT node, int L, int R, Int & LOC) {<br/> If (L = tree [node]. L & tree [node]. R = r) {<br/> loc = tree [node]. loction; <br/> return tree [node]. maxval + tree [node]. val; <br/>}< br/> int mid = tree [node]. mid (); <br/> int temp1, temp2; <br/> If (L <= Mid) {<br/> If (r <= mid) {<br/> temp1 = query (LL (node), L, R, Loc ); <br/>} else {<br/> int loc2; <br/> temp1 = query (LL (node), L, mid, Loc ); <br/> temp2 = query (RR (node), Mid + 1, R, loc2); <br/> If (temp2> temp1) {<br/> temp1 = temp2; <br/> loc = loc2; <br/>}< br/>} else {<br/> temp1 = query (RR (node), L, R, Loc ); <br/>}< br/> return temp1 + tree [node]. val; <br/>}< br/> int main () {<br/> int n, M; <br/> while (scanf ("% d", & N, & M )! = EOF & M + n) {<br/> char STR [2]; <br/> int A, B, C; <br/> build (1, 1, n); <br/> while (M --) {<br/> scanf ("% s", STR ); <br/> If (STR [0] = 'I') {<br/> // scanf ("% d", & A, & B, & C); <br/> readint (a); <br/> readint (B); <br/> readint (c); <br/> Update (1, a, B, C); <br/>}else {<br/> // scanf ("% d", & A, & B ); <br/> readint (a); <br/> readint (B); <br/> int loc = 0; <br/> int ans = query (1,, b, Loc); <br/> printf ("% D/N", ANS); <br/> If (loc! = 0) {<br/> Update (1, Loc, Loc,-ans ); <br/>}< br/>The question is not released, but it is indeed a good question. It is worth writing a description first: Happy Children's Day
Children's Day is coming. in this day, children will get a lot of candies. in Max city, people develop an automatic candy Management (ACM) system. ACM can manage n piles of candies. the system can carry out two operations.
1 ). I A B C(1 <= A <= B <= N, 0 <C <= 100), ACM will add each pile numbered from A to B with C candies.
2 ). C A B(1 <= A <= B <= N), ACM will choose the pile (from A to B) with the most candies, and give all the candies in that pile to one child. if two or more piles have the most candies, then the pile with smaller ID will be chosen.
Given a series of instructions, for each operation c a B, please help the people to find out the number of candies a child can get.
Input:
Input may contain several test data sets.
For each data set, the first line contains two integers n, m (0 <n, m <= 10 ^ 5) where n indicates the number of piles, and M indicates the number of operations that follows.
Each of the next M lines describes one operation.
Input will be ended by n = 0, m = 0, which shoshould not be processed.
Note: At the beginning of each data set, all of the N piles have 0 candies.
Output:
For each operation c a B, print the number of candies the child can get in a line.
Sample input:
5 4
I 1 5 1
C 2 3
I 2 2 4
C 2 3
0 0
Sample output:
1
4

 

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.