Just a Hook
Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 24416 Accepted Submission (s): 12165
Problem 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.
Print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
1 10 2 1 5 2 5 9 3
Sample Output
Case 1:the total value of the hook is 24.
The idea is still relatively simple problem of interval value change of a segment tree
As a novice I actually this problem is still stuck to me for a long time ~
And how long is it going to be?
Oh da ~
Oh da ~
Oh da ~
Oh da ~
Oh da ~
Oh da ~
Oh da ~
Heheda ~ ~ '
Oh da ~
Until just re-kill the bug, can be considered in Hangzhou electric top See red ~
This hard-won red AC
It's not the first time you've ever had a problem. But it's really exciting.
No Duzi.
Now, let's talk about the problem.
First, let's introduce the graph of the segment tree:
The method of lazy tagging is used to update the interval value.
As to what is lazy mark ~ My understanding is a way of labeling the data in the tree to cover the value from a child node.
Blue continues down child node overwrite value ~
A seemingly simple interval covering problem learning is really painful for a novice ~
Put the code and your own understanding here.
#include <stdio.h>
#include <string.h>
using namespace Std;
#define Lson l,m,rt*2
#define Rson m+1,r,rt*2+1
int tree[100001*4];//Tree
int flag[100001*4];//Lazy Tag Array
void pushup (int rt)
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];//Maintain tree data up
}
void pushdown (int l,int r,int RT)
{
if (Flag[rt])//The nodes on the lazy tag are going to give their child nodes an upper value and also to give their child nodes lazy tags on ~
{
int m= (L+R)/2;
FLAG[RT*2]=FLAG[RT*2+1]=FLAG[RT];
tree[rt*2]= (m-l+1) *flag[rt];//here is to cover a period of value (interval coverage value) So consider the problem of interval length here we hope the reader to savor.
Tree[rt*2+1]= (R (m+1) +1) *flag[rt];
flag[rt]=0;
}
}
void build (int l, int r, int rt)//Simple achievement function
{
if (L = = r)//If the leaf is found recursively
{
tree[rt]=1;//the value of initialization 1
flag[rt]=0;//simultaneous initialization Clears lazy tags
}
Else
{
int m = (l+r) >>1;//m is a penalty point
Build (Lson);//Jianzo
Build (Rson);//Right
Pushup (RT);//to seek intervals and (maintain tree data)
}
}
void update (int l,int r,int c,int l,int r,int RT)//Destination left interval objective to change the value of the right interval.
{
if (l<=l&&r<=r)//This is covered by the interval (the function of reduction (self-realization))
{
tree[rt]=c* ((r-l) +1);//Current node override value
flag[rt]=c;//lazy Flag The current node (Pushdown's operation overrides the value of the interval sub-interval)
return;//no less this step will re
}
Pushdown (L,R,RT);
int m= (L+R)/2;
if (L<=M)//objective to modify the left limit seat of the interval at the current node record
{
Update (L,r,c,lson);//change data in the left subtree ~
}
if (m<r)//purpose to modify the interval
{
Update (L,r,c,rson);//change data in right subtree ~
}
Pushup (RT);
}
int main ()
{
int t;
int kase=0;
scanf ("%d", &t);
while (t--)
{
int n,m;
scanf ("%d%d", &n,&m);
memset (tree,0,sizeof (tree));
memset (flag,0,sizeof (flag));
Build (1,n,1);
while (m--)
{
int x,y,c;
scanf ("%d%d%d", &x,&y,&c);
Update (x,y,c,1,n,1);
}
printf ("Case%d:the total value of the hook is%d.\n", ++kase,tree[1]);
}
}