HDU 1698 Just a Hook 线段树,成段更新

来源:互联网 发布:华为软件开发人员 编辑:程序博客网 时间:2024/05/16 17:35

HDU 1698  Just a Hook 

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. 
Now Pudge wants to do some operations on the hook. 


Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks 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 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. 
You may consider the original hook is 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 are no more than 10 cases. 
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an 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: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind. 
Output
For each case, 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.

题意:在dota游戏中,pudge的钩子是他最厉害的武器,众多的钩子组成了连续的一列,有铜钩子,银钩子,金钩子,分别用1,2,3表示,有两种操作,一种要求你把某一区间的钩子统统变为另一种钩子,第二种操作询问你某一区间钩子的总和

典型的线段树区间更新,区间求和问题,用了lazy—tag思想,对于初学者来说这是线段树比较难的一个地方,在看我看了一遍又一遍的博客后,终于明白点了

在点更新时,如果我们更新一个点,最多影响到 Lgn个点,但是在区间更新时,有可能影响到全部的点,这时如果一直向下跟新下去就会超时,所以假如更新到某个节点的时候,而这个节点的左右段和要更新的最有端点一致的话,就可以仅更新该节点,并在他的子节点上面(两个)加上一个lazy-tag,不再往子节点更新值。如果以后更新到加lazy-tag标记的结点时,则需要把这个标记的值更新下去,并且在他的子节点跟新lazy-tag标记,并取消这个节点的lazy-tag

另外注意区间更新与区间增减的区别
#include<stdio.h>const int MAXN=100005;struct NODE{int l,r;int sum;int lazy,tag;}segTree[4*MAXN];void pushup(int num){segTree[num].sum=segTree[num<<1].sum+segTree[num<<1|1].sum;}void build(int num,int l,int r){segTree[num].l=l;segTree[num].r=r;segTree[num].lazy=0;segTree[num].tag=0;if(l==r){segTree[num].sum=1;return;}int mid=(l+r)>>1;build(num<<1,l,mid);build(num<<1|1,mid+1,r);pushup(num);}void pushdown(int num){segTree[num<<1].sum=(segTree[num<<1].r-segTree[num<<1].l+1)*segTree[num].tag;segTree[num<<1|1].sum=(segTree[num<<1|1].r-segTree[num<<1|1].l+1)*segTree[num].tag;//对子节点添加 lazy—tag标记 segTree[num<<1].lazy=segTree[num<<1|1].lazy=1;segTree[num<<1].tag=segTree[num<<1|1].tag=segTree[num].tag;segTree[num].lazy=segTree[num].tag=0;//取消父节点lazy—tag标记 }void update(int num,int l,int r,int m){if(segTree[num].l==l&&segTree[num].r==r)    {    segTree[num].sum=(r-l+1)*m;    //添加 lazy—tag标记     segTree[num].lazy=1;    segTree[num].tag=m;    //直接返回     return;    }    if(segTree[num].lazy) pushdown(num);    int mid=(segTree[num].l+segTree[num].r)>>1;    if(r<=mid) update(num<<1,l,r,m);    else if(l>mid) update(num<<1|1,l,r,m);    else{    update(num<<1,l,mid,m);    update(num<<1|1,mid+1,r,m);    }    pushup(num);}int main(void){int test;int N,Q;int l,r,m;int Case=0;scanf("%d",&test);while(test--){scanf("%d",&N);build(1,1,N);scanf("%d",&Q);while(Q--){scanf("%d%d%d",&l,&r,&m);update(1,l,r,m);}printf("Case %d: The total value of the hook is %d.\n",++Case,segTree[1].sum);}return 0;}