HDU (1698), Segment tree interval update

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1698

Interval updates focus on lazy tagging.

When you update the interval is the entire interval, the direct sum[rt] = c* (r-l+1); col[rt] = C; The following sub-range is no matter, when you next update a certain interval, the COL[RT] from the top down (nor pushed to the end), pushed to the appropriate position, Just this position is the sub-range of the interval (which may have been crossed) that I want to update to stop.

Thanks to the online Daniel, thank Jiege, Brother bin. I can AC.

Just a Hook

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

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 Input11021 5 9 3 Sample Outputcase 1:the total value of the hooks is 24. Source2008 "Sunline Cup" National Invitational Contest
#include <stdio.h>#include<algorithm>using namespacestd;#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1Const intMAXN =100010;intsum[maxn<<2];intcol[maxn<<2];voidPushup (intRT) {Sum[rt]= sum[rt<<1] + sum[rt<<1|1];}voidPushdown (intRtintm) {    if(Col[rt]) {col[rt<<1] = col[rt<<1|1] =COL[RT];//push down .sum[rt<<1] = (M (m>>1)) *col[rt];//Sub-interval updatesum[rt<<1|1] = (m>>1)*Col[rt]; COL[RT]=0;//the point has been pushed    }}voidBuildintLintRintRT) {Col[rt]=0; SUM[RT]=1; if(L==R)return; intM= (l+r) >>1;    Build (Lson);    Build (Rson); Pushup (RT);}voidUpdateintLintRintCintLintRintRT) {    if(l<=l&&r<=R) {Col[rt]=C; SUM[RT]= c* (r-l+1); return ; } pushdown (Rt,r-l+1); intm = (l+r) >>1; if(l<=m) update (L,r,c,lson); if(r>m) update (L,r,c,rson); Pushup (RT);}intMain () {intt,n,m; scanf ("%d",&T);  for(intcases=1; cases<=t;cases++) {scanf ("%d%d",&n,&m); Build (1N1);  while(m--)        {            intA,b,c; scanf ("%d%d%d",&a,&b,&c); Update (A,B,C,1N1); } printf ("Case%d:the Total value of the hook is%d.\n", cases,sum[1]); }    return 0;}

HDU (1698), Segment tree interval update

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.