Just a Hook (tree-like array)

Source: Internet
Author: User

In 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 Input

11021 5 25) 9 3

Sample Output

Case 1:the total value of the hook is 24.
Test instructions: A segment is made up of n small line segments, each operation turns a small segment into one of gold and silver (the value of gold is 3, silver is 2, copper is 1), initially it can be used as all copper; Finally, the total value of this line segment is calculated.
Puzzle: The interval update of a simple line segment tree.
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <iostream>5#include <algorithm>6 using namespacestd;7 Const intmaxn=2e5+Ten;8typedefLong Longll;9 #defineLson l,m,i<<1Ten #defineRson m+1,r,i<<1|1 OnetypedefstructNode A { - ll L,r; - ll Mid () the     { -         return(L+R)/2.0; -     } - ll value; + } Node; -Node node[maxn<<2]; +ll sum[maxn<<2];//is used to record the value (interval and) of each node All add[maxn<<2];//delay Tag Array at voidpush_up (ll i) - { -sum[i]=sum[i<<1]+sum[i<<1|1];//The sum of the nodes equals the left son plus the child and - } - voidBuild (ll l,ll r,ll i) - { inNode[i].l=l; -Node[i].r=R; toNode[i].value=0; +sum[i]=0; -add[i]=0; the     if(l==R) *     { $sum[i]=1;Panax NotoginsengNode[i].value=1; -         return ; the     } +ll m=Node[i].mid (); A Build (Lson);//Build Left sub-tree the Build (Rson);//Build Right sub-tree + push_up (i); - } $ ll M; $ voidPush_down (ll i,ll len)//delay mark down pressure - { -     if(Add[i]) the     { -add[i<<1]=add[i];//assigns the node's mark to his left son .
Wuyiadd[i<<1|1]=add[i];//assigns the node's mark to his right son . thesum[i<<1]=add[i]* (len-(len>>1) ;//update the left son -sum[i<<1|1]=add[i]* (len>>1)//To update the right son Wuadd[i]=0;//delete the delay tag of the node - } About } $ - voidUpdate (ll l,ll r,ll I,ll v) - { - if(node[i].r==r&&node[i].l==l) A { +add[i]=v;//Delay Flag thesum[i]=v* (r-l+1);//Assign a value to the node - return; $ } thell m=Node[i].mid (); thePush_down (i,node[i].r-node[i].l+1);//To determine if the node has a delay tag, if any, update the if(r<=m) theUpdate (l,r,i<<1, v);//query interval on the left sub-tree of the node
- Else in { the if(l>m) theUpdate (l,r,i<<1|1, v);///The query interval is on the right subtree of the node About the Else the { theUpdate (l,m,i<<1, v);//query interval on the two trees of the node +Update (m+1,r,i<<1|1, v); - } the }Bayi push_up (i);//Return to the first step, update up the } the intMain () - { - ll M,n,a,b,t,c; thell flag=0; thescanf"%lld",&T); the while(t--) the { -sum[0]=0; the thescanf"%lld%lld",&m,&n); theBuild (1M1);//Achievements94flag++; the while(n--) the { the 98scanf"%lld%lld%lld",&a,&b,&c); AboutUpdate (A, B,1, C);//update Tree - }101printf"Case%lld:the Total value of the hook is%lld.\n", flag,sum[1]);//sum[1], which indicates the value of the root node is the interval and102 }103 return 0;104}

Just a Hook (tree-like array)

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.