Segment Tree interval update HDU1698

Source: Internet
Author: User

Just a Hook

Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 9758 Accepted Submission (s): 4776


Problem DescriptionIn The game of DotA, Pudge's Meat hook is actually the most horrible thing for most of the heroes. The hook is made to several consecutive metallic sticks which is of the same length.



Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hooks from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver s Ticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each of the silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
Consider the original hook is a made up of cupreous sticks.

Inputthe input consists of several test cases. The first line of the input is the number of the cases. There is no more than cases.
For each case, the first line contains a integer N, 1<=n<=100,000, which is the number of the sticks of Pudge ' s MEA T Hook and the second line contains a integer Q, 0<=q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=x<=y<=n, Z, 1<=z<=3, which defines an operation:c Hange the sticks numbered from X to Y into the metal kind Z, where z=1 represents the Cupreous kind, z=2 represents the SI Lver Kind and z=3 represents the golden kind.

Outputfor, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

Sample INPUT1 10 2 1 5 2 5 9 3

Sample outputcase 1:the Total value of the hook is 24.****************************************/\*\/*/\*\/************** Time is always not enough, and time is becoming fragmented, so that I have to think about whether I should sleep later, so that there can be a whole lot of time, (at least in the near term just to say, if things more and more late to sleep it) sometimes very silent, on the blog wrote half of the room to close, forced to send up when saved, here to read my blog People say sorry, and so I have time to fill up. The code style of this problem is not imitate the school courseware, because I think Rujia is better. The comments are all written in the code.
1#include <stdio.h>2 Const intmaxn=100050;3 intsumv[maxn<<2];4 intsetv[maxn<<2];5 voidPushup (intRT) {///Update Current Value6sumv[rt]=sumv[rt<<1]+sumv[rt<<1|1];7 }8 voidPushdown (intRtintx) {///When this node changes, this node is updated and passed to the next node9     intlc=rt<<1,rc=rt<<1|1,mid=x>>1;Ten     if(setv[rt]>=0){ Onesetv[rt<<1]=setv[rt<<1|1]=Setv[rt]; Asumv[lc]= (X-MID) *Setv[rt]; -sumv[rc]=mid*Setv[rt]; -setv[rt]=-1; the     } - } - voidCreat (intLintRintRT) {///unique achievements, because the problem is always initialized -setv[rt]=-1; +sumv[rt]=1; -     if(L = = r)return ; +     intMid= (l+r) >>1; ACreat (l,mid,rt<<1); atCreat (mid+1,r,rt<<1|1); -Pushup (RT);///Each node is established in the child node to complete the update - } - voidUpdateintLintRintXintLintRintRT) {///Data Update -     if(L&LT;=L&AMP;&AMP;R&GT;=R) {///The end of the update of the second son tree when the secondary node is completely covered to update the interval part -Setv[rt]=x;///end of update process insumv[rt]=x* (r-l+1); -         return ; to     } +Pushdown (rt,r-l+1);///update data moves down and eliminates the node's -     intMid= (l+r) >>1; the     if(l<=mid) Update (l,r,x,l,mid,rt<<1);///If there is an update on the left, update left *     if(r>mid) Update (l,r,x,mid+1,r,rt<<1|1);///Updates on the right are updated to the right $ Panax NotoginsengPushup (RT);///Update this node - } the intMain () + { A     intT, N,q; the     intk=1; +     inta,b,c; -scanf"%d",&t); $      while(t--){ $scanf"%d%d",&n,&q); -Creat (1N1); -  the          for(intI=0; i<q;i++){ -scanf"%d%d%d",&a,&b,&c);WuyiUpdate (A,B,C,1N1); the           /*for (int j=1;j<20;j++) { - printf ("a%d =%d\n", J,sumv[j]);}*/ Wu         } -printf"Case%d:the Total value of the hook is%d.\n", k++,sumv[1]); About  $     } -}
View Code

Segment Tree interval update HDU1698

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.