Just a Hook

Source: Internet
Author: User

Just a HookTime limit:2000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 1698

Description

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.

Input

The 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.

Output

For each case, print a number with 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. A hook by a number of metal gold and silver copper stick composition, on a hook to carry out a number of operations (the metal stick replace other types, copper is 1 silver is 2 gold is 3 value, ask the last operation after the total value is how much because of the data too much, we use line segment tree processing, build query update.

Problem Solving Ideas:

Segment Tree Update. Each update to the node will be tle because of a data range problem. The interval update will save a lot of time, for each interval, if the stick inside the interval is the same type, a node variable is used to record the stick type, and if the stick is different type in the interval, the variable is recorded as-1.

#include <iostream>#include<cstdio>using namespacestd;#defineN 100005structnode{intL, R, sum;} tree[4*N];voidBuild (intRootintLintR) {TREE[ROOT].L=l; TREE[ROOT].R=R; Tree[root].sum=1; At the beginning, the sum was 1, which was the first copper metal.if(L = =R)return ; Build (2*root+1, L, (l+r)/2); Build (2*root+2, (L+R)/2+1, R);}voidInsetintRootintLintRintc) {    if(TREE[ROOT].L = = L && TREE[ROOT].R = =r) {tree[root].sum=C; return ; }    if(Tree[root].sum! =-1)//If the root of the tree, left and right two sub-trees on the metal state, but in this tree has to update the number, so the state on the tree is not the same, put tree "root" to 1, the tree's metal state passed {tree[root*2+1].sum = tree[root*2+2].sum =tree[root].sum; Tree[root].sum= -1; }    intMid = (TREE[ROOT].L+TREE[ROOT].R)/2; if(R <=mid) inset (2*root+1, L, R, c); Else if(L >mid) inset (2*root+2, L, R, c); Else{inset (2*root+1, L, Mid, c); Inset (2*root+2, mid+1, R, c); }}intQueryintRootintLintR) {    if(Tree[root].sum! =-1)//If the root of the tree on the left and right two trees on the same metal state directly return the sum of the numbers on the treereturntree[root].sum* (r-l+1); intMid = (TREE[ROOT].L+TREE[ROOT].R)/2; returnQuery2*root+1, L, mid) +query (2*root+2, mid+1, R);}intMain () {intT, Q, N, a, b, C; scanf ("%d", &t);  for(intK =1; K <= T; k++) {scanf ("%d", &N); Build (0,1, N); scanf ("%d", &q);  for(inti =0; i < Q; i++) {scanf ("%d%d%d", &a, &b, &c); Inset (0, A, b, c); } printf ("Case%d:the Total value of the hook is%d.\n", K, query (0,1, N)); }    return 0;}

Http://www.cnblogs.com/alihenaixiao/p/4616575.html

Just a Hook

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.